So we've got some function calling working, but it's not fair to call our program an "agent" yet for one simple reason:
It has no feedback loop.
A key part of an "agent," as defined by AI-influencer-hype-bros, is that it can continuously use its tools to iterate on its own results. So we're going to build two things:
A loop that will call the LLM over and over
A list of messages in the "conversation." It will look something like this:
User: "Please fix the bug in the calculator"
Model: "I want to call get_files_info..."
Tool: "Here's the result of get_files_info..."
Model: "I want to call get_file_content..."
Tool: "Here's the result of get_file_content..."
Model: "I want to call run_python_file..."
Tool: "Here's the result of run_python_file..."
Model: "I want to call write_file..."
Tool: "Here's the result of write_file..."
Model: "I want to call run_python_file..."
Tool: "Here's the result of run_python_file..."
Model: "I fixed the bug and then ran the calculator to ensure it's working."
This is a pretty big step; take your time!
Assignment
In generate_content, handle the results of any possible tool use:
(aiagent) wagslane@MacBook-Pro-2 aiagent % uv run main.py "how does the calculator render results to the console?"
- Calling function: get_files_info
- Calling function: get_file_content
Final response:
Alright, I've examined the code in `main.py`. Here's how the calculator renders results to the console:
- **`print(to_print)`:** The core of the output is done using the `print()` function.
- **`format_json_output(expression, result)`:** Before printing, the `format_json_output` function (imported from `pkg.render`) is used to format the result and the original expression into a JSON-like string. This formatted string is then stored in the `to_print` variable.
- **Error handling:** The code includes error handling with `try...except` blocks. If there's an error during the calculation (e.g., invalid expression), an error message is printed to the console using `print(f"Error: {e}")`.
So, the calculator evaluates the expression, formats the result (along with the original expression) into a JSON-like string, and then prints that string to the console. It also prints error messages to the console if any errors occur.
You may or may not need to make adjustments to your system prompt to get the LLM to behave the way you want. You're a prompt engineer now, so act like one!