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.
Splitting Out ask_model() and run_tool()
00:00 So let’s do a bit of refactoring because there are two things that can happen here, right? When we’re dealing with the harness and the model. We’re either asking the model something or we’re running tools.
00:11 We’re starting to become an agent. It’s not quite there, but it’s closer to becoming an agent. The harness can now talk to the model and it can run tools if the model says, can you run this tool for me?
00:22
So let us take a bit of code. So let us create an ask_model(), which is going to send it some messages. So this is where we’re going from harness to model.
00:33
We’re sending our messages with the list of tools, and then we get the reply and we append it. So I’m going to cut it from here and put it there. But we need to do a bit more now because we want to say, let’s go through the tool_calls in our reply and let’s go through them one at a time.
00:56
However, this could be None, right? So let’s give the option here that if there’s nothing, then let’s put the empty list so the for loop doesn’t run.
01:03
Let’s also put a trace() here. So remember, the traces are for us to know where the information is flowing. So now this is flowing from the model because it’s the model that asks for a tool to the harness.
01:16
The model only ever talks to the harness, never to us directly. And here we’re going to say, the model is asking, please run and call.function.name with the following arguments, call.function.arguments.
01:34
So what we’re saying is, well, if the reply has tool calls, well, then let’s have the trace(). And if the reply has content, so it has some text, well, then the trace() is going to be different.
01:50
This is why I removed the trace() earlier. Model sends to the harness, and this is what we had before, right? So this is now a text reply,
01:59
which is however many characters. And ask_model() will return the reply. So what we’ve done is we’ve taken the bit out of the harness where we are dealing with the model.
02:14
But of course, we still need it here. So our reply here is going to be ask_model(messages). This is how we keep our code tidy, right?
02:23
But we also need to be able to call tools. So let us add another function. And this is going to be run_tool(), because that’s what we want it to do, right?
02:32
And there’s going to be one of the calls. So the model is telling us what to do. Let’s get, we need to import json there, because that’s what the model is giving us back.
02:44
So here now we can take the call. If it’s a function, it will have arguments. So the call is going to be whenever the model says, I need you, dear harness, to call a function for me.
02:56
That’s what’s going to be returned from, this will be args, not arg, as we usually use. Let’s put in a trace(). Remember, the traces are just for us to see what’s happening.
03:05 So now the harness is going to call a tool. So the information is now going to go from the harness. The harness is the in-between, sending information here and there.
03:15 Now it’s going to send it to a tool. And we only have one tool, but of course, this could be one of many tools if we had more. And this is going to be the function with the name.
03:25 We only have one in our case. And it’s going to call it with some arguments. So let’s put the arguments there. So this is telling us what function it’s calling with what arguments.
03:34
Let us then call list_files(). Now I’m hard-coding this as list_files() just to make it easy. In practice, we’ll have to use the function name there.
03:46
But let’s just keep it nice and easy here, right? So that we know what’s happening. And unpacking args. Remember, args we’re reading from a JSON.
03:53
This is going to be a dictionary. And therefore, we’re unpacking the dictionary as key equals value. Now we can put another trace(). Remember, the trace again is us seeing what’s happening.
04:04 The tool has done something. So it’s going to send information back to the harness. Remember, everything goes through the harness. And let’s say that we’re going to find how long the result from the tool is, this many characters long.
04:18
In this case, it’s going to be whatever is returned from list_files(). And let’s return our result. So again, the result is going to be whatever the function list_files() gives us.
04:29
We will change this to be more generic, of course, later on. Now we need to run the tool at some point. You’ve moved the round trip to the model into ask_model().
04:39
And it now traces every tool call the model asks for. You’ve written run_tool(), which turns the model’s JSON arguments into a dictionary and calls list_files() with them.
04:50
But the harness doesn’t call run_tool() yet. In the next lesson, you’ll run each tool call and send the result back to the model.
Become a Member to join the conversation.