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

Describing a Tool to the Model

00:00 I need to give my system, my harness, the ability to run a tool. By a tool here, I mean a function. So let me create the function I need. I would like to say, well, how about if you knew how to list files?

00:12 It would be really helpful for me. And I’m going to give you a folder, and I’d like you to return, let’s actually import from pathlib, let’s import Path, which we quite like to use, and I’m going to return, I’ll write this in one go, right, let us join, so a newline between each one, and then I’m going to join path.name for path in Path(folder).iterdir().

00:37 If you’re familiar with the pathlib module, then you can sort of get the path to the folder, .iterdir() means iterate through each item, and we’re simply creating a nice string with a newline joining them all.

00:48 So that’s the tool, it’s a function. But before I can use this tool, I need to do a few things. The harness will use this tool, but the LLM needs to know that this tool exists.

01:00 So, allow me to paste this because there’s a lot of boilerplate code, right, so this is what OpenAI needs our tools to look like. So there’s a list there, and that is the first tool I’m going to create.

01:10 It’s a function, tools are functions, they do something. I need to give it the name, so in this case, the name I’ve called my function was list_files, is that what I called it?

01:19 Yeah, list_files. And I need to give it a description. Why? Because this is how the LLM will know what this tool does, right? It needs to know, “List the files in a folder.”

01:29 The parameters, it’s a type object, it’s a JSON object, they’re always going to be object in this case. What properties does it need?

01:36 So it needs to know that it’s going to need folder, because that’s the argument we need to pass, and this is going to be, we’re giving the LLM information on what this tool does, right?

01:47 It’s a string. And is anything required? Yes, folder is. So if you don’t have folder, well, at the moment, our function won’t work.

01:57 So this is how we’re going to tell the LLM what our tools are. Of course, this is just a variable, so let’s see how to pass it to our LLM. So if I go to where I’m getting my response, where I’m creating this chat completion, what I can do is I can also use the tools and say that’s where my tools are.

02:16 So that’s how we’re passing our list of tools to the LLM. Now, for the time being, let’s leave everything as it is, and all I’m going to do here is to show whether my reply has any tool calls.

02:32 A reminder, reply is what’s coming back, response is what’s coming back from the call to the LLM. When we’re using tools, OpenAI says we need to also say what’s the reasoning_effort, and we can just put "none" here, that’s going to be good enough.

02:48 And let’s also remove the trace() for now, because the trace() will have to now do different things depending whether we’re calling tools or getting messages back.

02:57 So let’s leave it out, and we’ll add it later on. Let’s run again. What’s in the habits folder? We use this question now, and notice how we’re sending one message.

03:10 Now, the harness to me has nothing to say. Why? Because in this case, the content is a tool call. And here it’s telling us there’s a tool call, all of this fun stuff, so the LLM was able to fill in the name of folder from the text we gave it, and it wants to call the function list_files().

03:33 This is great, the LLM has detected a tool, and it told the harness, I need a tool call. You’ve written list_files(), your first tool, and it’s just a Python function.

03:46 You’ve described it to the model in the TOOLS list, and you’ve passed it with tools=TOOLS. Now the model replies with a tool call instead of text, but nothing runs the tool yet.

03:58 In the next lesson, you’ll split out ask_model() and run_tool(), so the harness can run the tool for the model.

Become a Member to join the conversation.