Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Sending Tool Results Back to the Model

00:00 Let’s go to the point where we got a reply. We’ve asked the model, we got a reply. Now, the reply could be either some text or one or more tool calls. Since it could be one or more tool calls, let’s say if reply has tool_calls, let’s create a loop.

00:20 for call in reply.tool_calls, if there’s one, it’s just one, but there could be more. Our result is going to be run_tool(call).

00:31 And that’s the function we’ve just written, right? We’re just going to get the arguments, show us the trace and call the function and return the result. And now we can take our messages and .append().

00:46 Remember, this is what is going to go back. The conversation we’re sending to the LLM is growing. We’re going to append. So now the role is no longer the user, it’s a tool.

00:57 This should be a dictionary, right? My apologies for that. We need to give it tool_call_id, which is the call.id. When we printed it out earlier, as we’ve seen, the chat completion message function had an ID.

01:10 So this is just an identity it’s using internally. And importantly, what’s the result? What did our function return? And there we go. And let’s now finish off by saying that the harness is now sending you, the user or me, whichever we want to do it, the content.

01:30 reply.content. So we’ve done quite a few changes there. We have taken our harness. Asking the model is now a function. What comes back could be a tool call, in which case we’re going to run the tool, add the content from run_tool() to the messages and keep sending it back until the LLM no longer asks for a tool call.

01:54 Therefore, it gives us some content back. If it gives us content, this if is skipped. And that’s where we get our content. So remember, the LLM will give us either content, which is text, or it will request a tool call.

02:07 And we no longer need these lines because they’re already included. Once we’ve gone through all the tool calls, we’ve been appending to the messages, we now need to send it back to the LLM.

02:19 So ask_model(), send the messages. Once we’re back, we can still print the content. I no longer need to print the tool calls now because we’re dealing with them.

02:30 Plenty of things happening, right? We send information to the model. The model can either give us text back, that’s the content, reply.content, or it can request a tool.

02:40 The communication is always between me and the harness, harness and the model. The model always goes to the harness. And if it says, I need a tool call, it’s the harness which calls the tool.

02:52 My while loop here is the harness. Let’s try running this. What’s in the habits folder? We’re sending one message. Now let’s follow this.

03:02 The model says, harness, please run list_files, folder, habits. The harness then calls the tool, list_files, folder, habits.

03:11 The tool is our function, gives a result, which goes to the harness, 31 characters long. And the harness sends it to the model. Now it’s sending three messages.

03:20 My first message, the first response, and now the second message, not mine, the harness’s second message, so the third message overall, with the result of the tool.

03:30 The model looks at that and says, I’m now willing to send the text reply back. And the text reply, harness to me, the habits folder contains habits.py, README.md, and habits.json.

03:43 We now have a system that can ask an LLM. The LLM can say, I would love to call a tool here. Is this one of the tools available? Yes, it is. You have a tool for me that allows me to list files.

03:57 Can I please, please, please, dear harness, have that tool called? The harness, in a general harness, the harness might make some decisions, right? Do I need to ask for permission here?

04:06 Do I need to do something else? But in our simple example here, the harness calls the tool. The tool is a function, deterministic function, gives us a response, goes back to the model.

04:16 The model says, this is what I need. Great. It might have said, okay, now actually I need another tool call. It might keep going with asking for more tools and more results.

04:25 But eventually it says, I now have a response for the user. I think this is what the user wants. Here’s what you did in this lesson. You’ve looped over the model’s tool calls and run each one with run_tool().

04:37 You’ve sent each result back to the model as a "tool" message with the tool call’s ID. Then the harness asks the model again, and the model turns the result into an answer.

04:47 Our agent can now list files. In the next lesson, you’ll find out what happens when you ask it to do something it doesn’t have a tool for.

Become a Member to join the conversation.