From the course: OpenAI API for Python Developers
Call functions
- [Instructor] So we did step one, which is to start the conversation and send a user query. Now we want to take care of step two and also step three. So first step two will be to check if the model detects and wants to call a function. So we're going to go back to the documentation and look at the example again. So that's going to be step one, which is already done. And then step two is to check if the model wants to call a function to execute function calling. And basically it's going to check if tool calls exist. So that's going to be an object which is existing, provided in the generated response, and this corresponds to the list of functions that we have defined, functions and parameters. So let's do that. We're going to take all of this from line 49, all the way down to here. But before that, I just want to make a quick demonstration just to show you the difference, actually, what is tool calls. So let's make a quick demonstration. I'm going to show you that first. So what is, I'm going to ask, "What is two and two," which is a basic math question that does not involve anything related to weather. So let's see the output. So you see that here, tool calls is not existing. So this is exactly what the chat model is checking, to detect if function calling is necessary. So we're going to try that again, but this time we're going to define step two and step three. So here I'm going to add this, but actually I'm going to put all of it in its own function. And what I'm going to do, I'm going to go step by step. It's going to be this, we're going to check if message response, like this tool calls, is none. If it's none, we're going to do as usual, which is to just continue and then print the message like this, here we go. We're going to print the message from the bots, all right, just like this. And we need to specify just content to just display the response from the assistant. And then the next step will be to call the function, if the chat model detects that this is necessary. So I'm going to go up here and then define it, actually, define a new function that I'm going to call, call function, like this, and as a parameter, it's going to take tool calls, actually. Tool calls, like this. And for here, I don't need this step because this is actually part of the, so I took the whole thing. I'm going to skip this part. And for this available functions, I'm going to put it, actually, up here. And for now, we haven't defined any functions, so this is going to be another step further. For now, I'm just going to put that this is an empty object. I'm going to do it this way for now, and I don't need to do this step, which is to append the response from the assistant. And we should do that. Let me actually also make a few adjustments up here. Right here, we're going to also remember to append the message response from the assistants, like this, so that's going to be response, and then choices, actually just like this. We're going to make sure that we also append the response from the chat bot this way. All right, so now we are ready for step three. So let me put that, actually all the way down at the bottom. And okay, it's already indicated. So now we know that we can call the function, like this, and pass as a parameter, the tool calls. So if it exists, so that's going to be done here, so if tool calls exist, if it's not known, we're going to call the function. So for now, what we're going to do is to make another quick demo, and this time we're going to print function response, just that. So for now, that's going to generate an error because we haven't done the next step, which is to allow to execute an actual function, after we detect that function calling is necessary. But let's run this demonstration for now. We're going to run Python main.py, and ask the assistant about what is, again, we're going to do what is two and four, just to see that the process is actually to go to, here we go, so we know that it seems like you're asking the sum of two and four, so that's fine. So it took this step and then it stopped right here because it couldn't detect that we wanted to call a function. And the next question will be, "What is the weather in Boston," to see what is the output. And actually, this time, you'll see that this is going to generate a response, but this is normal. We're going to need to do that afterwards to allow to create a function, which is callable. But you can see right here that now the tool calls object is existing. So we're going to take to the next step, which is to allow to call an actual function. And we want to connect to an external API, which is this one, openweathermap.org. So I'm going to invite you here to create a new account. So you're going to see that the interface is very user friendly and the process is easy to create a new account. You can here check out the API documentation. And after you create your accounts, you're going to need to also create an API key in order to be authorized to make API calls. And after that, the next step will be to call functions and connect to external APIs.