Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Adding a Second Tool
00:00
So what if I now ask, can you read the contents of the .py? Notice that the number of messages keep increasing because the conversation is getting longer.
00:12
Now, the first thing it did is, it says, can you list_files? You can say it already had this information, right? But it’s saying, well, the .py file, let me actually see what all the .py files are.
00:23
So it’s asking to list_files again. The harness calls a tool, which gives it the same response as before. The harness sends it to the model. The model now has a response.
00:32
I can see that habits/habits.py exists, but I don’t have a tool here to read file contents. So let’s give it another tool. Now, if you allow me here to, in order to be quick, I’m going to go to my tools, you may recall, and I’m going to add another one super quickly.
00:50
And this one is a read_file, read a text file and return its contents. It’s going to accept the path, which is required. Now, this is simply the information we’re going to send to the LLM.
01:04
We actually need to define this tool. So what did I call it? read_file.
01:11
And we said it’s going to accept an argument called path, and it’s simply going to take Path(path).read_text(). We’re using the pathlib module there.
01:22
So that’s all. I’ve added another tool, and I’ve made sure that my LLM knows about it. Now, there was another issue we had. You may recall that in run_tool(), I had hard-coded the name of my function, which I don’t want to do, right?
01:42 So let’s also have another bit of a helper here. So let’s create a mapping between the names and the functions. There are other ways you could do this, but this is good enough.
01:51
And here I’m going to say list_files maps to list_files, and read_file maps to read_file. Because now, this is the only change I’m going to do.
02:06
I can go to my run_tool(), and instead of hard-coding list_files here, I’m going to say from this mapping I’ve just created, TOOL_FUNCTIONS, can you get the function name
02:22
out of there? This makes it more generic. Okay, those are the only changes. So a bit of tidying up in run_tool() so that we don’t hard-code the name of the function.
02:33
And we’ve added one more tool by defining, where is it? Another function and adding it to our list of tools. Let’s see what’s going to happen now. What’s in the habits folder?
02:49
Well, I could have actually skipped this first question, right? But let’s carry on and do what we’ve done before. Okay, read the habits.py file and tell me what’s in it.
03:04
Just in case, otherwise it might read it and not tell me what’s in it. And there we go. Let’s follow again. After I said read the habits.py file and tell me what’s in it.
03:15
Earlier I said, can you read the .py file? And you may recall it actually asked for finding the list of files first. In this case, I actually said read the habits.py file.
03:25
So the next thing it did is to ask, please run read_file because now it’s a bit clearer. The harness called read_file. The tool, the function gave the information back to the harness and the harness sent it to the model along with the rest of the conversation, of course.
03:41
And the model now said, I have a text reply for you. habits.py is a small command-line habit tracker. There we go. Now we know what’s inside habits.py.
03:50 It stores habits and their check-in dates, blah, blah, blah. And you can see there that it’s actually given me the information about it. So it’s not giving me the whole content.
03:59
It’s just telling me there’s load(), save(), check_in(), et cetera. And if I check habits.py, load(), save(), check_in(), streak().
04:06
So it’s given me a summary of what’s in this particular folder. Here’s what you did in this lesson. You’ve added read_file(), a second tool, and described it to the model in the TOOLS list.
04:19
You’ve mapped each tool’s name to its function in TOOL_FUNCTIONS. So run_tool() no longer hard-codes list_files. Now your agent can list files and read them.
04:30 In the next lesson, you’ll see what happens when one tool call leads to another, and you’ll close the agent loop.
Become a Member to join the conversation.