I recently read a great blog post by Thorsten Ball on how simple it is to build coding agents and was inspired to make a python version guide here! We’ll create an LLM that can call tools that allow it to create, edit and read the contents of files and repos!Documentation Index
Fetch the complete documentation index at: https://docs.together.ai/llms.txt
Use this file to discover all available pages before exploring further.
Setup
First, let’s import the necessary libraries. We’ll be using thetogether library to interact with the Together AI API.
Basic Chat Interaction
Let’s start with a simple loop that takes user input, sends it to a language model (LLM) using the Together AI client, and prints the LLM’s response. We’ll maintain amessages_history to keep track of the conversation context.
Tool use by LLMs
Tool use is pretty simple - we tell the model that it has access to certain tools and instruct it to use them when it feels it would help resolve a prompt. As Thorsten say: To summarize, all there is to tools and tool use are two things:- You tell the model what tools are available
- When the model wants to execute the tool, it tells you, you execute the tool and send the response up
tool_use you don’t need to make any code changes - we can simply use the same chat() function above:
get_weather() function if needed and it did. When it did we provided it information it wanted and it followed us by using that information to answer our original question!
This is all function calling/tool-use really is!
Defining Tools for the Agent
To make this workflow of instructing the model to use tools and then running the functions it calls and sending it the response more convenient people have built scaffolding where we can pass in pre-specified tools to LLMs as follows:read_file tool to discover the secret!
Calling Tools
Now we need to run the function that the model has asked for and feed the response back to the model, this can be done by simply checking if the model asked for a tool call and executing the corresponding function and sending the response to the model:- See if the model wanted us to use a tool.
- If so, we used the tool for it.
- We appended the output from the tool back into
messagesand called the model again to make sense of the function response.
More tools: list_files and edit_file
We’ll want our coding agent to be able to see what files exist in a repo and also modify pre-existing files as well so we’ll add two more tools:
list_files Tool: Given a path to a repo, this tool lists the files in that repo.
edit_file Tool: Edit files by adding new content or replacing old content
Incorporating Tools into the Coding Agent
Now we can add all three of these tools into the simple looping chat function we made and call it!congrats.py which you can run!