From the course: OpenAI API for Python Developers

Define functions and parameters

From the course: OpenAI API for Python Developers

Define functions and parameters

- [Instructor] Now, we have a deep dive at the function calling feature that gives language models the ability to fetch current data and also call APIs. In order to get an extended generated response, that includes training data and also current and real-time information. And this is the starter project, which is a typical helpful assistance and AI powered assistance, which is capable of answering user queries in a human-like fashion. Now, we would like to power up the abilities of this assistance and receive daily weather report from this assistance. And you see that we use here the typical and usual gpt-3.5-turbo model. So we're going to find a list of supported models for this example, so this could be different at the time that you watch this recording. And let's go back up a little bit to find a few common use cases of function calling integration. So with function calling, we could create assistance that can answer questions, user queries by calling external APIs in order to get the current weather information, so that's going to be our next project. And you can also create assistance that can convert natural language into API calls and train the language models with your custom knowledge by allowing the chat models to call internal APIs. So function calling allows to get structured data back from the model. So let's look at a typical integration of this feature with a code example right here below. So that's going to be a usual conversation, so we're going to append here the user input to the list of messages. And we're going to define also here functions, which are called tools. So inside, you're going to find a list of functions with the type function. It has a name, and also, we define the parameters, and you can understand the parameters as the information that you send along with an API request. For example, to get the weather forecast, you need to specify the location, but also, the temperature unit, so that's going to be either in Celsius or in Fahrenheit. So we're going to copy from line 22, right here, all the way down to 48. We're going to add this to our product, so the first step will be to define the functions like this. And we're going to replace here, so in this location, so where you defined the code in order to generate the response from the model, we're going to replace this. And you see that here, the structure is a little bit different, so you're going to send here the list of messages as an input as usual. And also, you're going to define the functions, the tools. And the thing we're going to do as well is to append, so that's going to be messages, and then append the new message from the user. So you see that we define as usual, so the system message first, and then we're going to append the new message and then send it as an input, the whole list of messages. And after that, we're going to return the message object as the response. So let's try that, we're going to make a first example to see how it looks like. So that's going to be message_response, so you see that this is going to be the first step, which is to send the conversation and see the available functions that we have just defined. So let's start the app. Here it is, so this is ready, we can interact with it. So I'm going to ask a question like, what is the weather in Boston, for example. Let's see what is the answer. All right, so I think that we need to start the app again, so no worries. All right, so let me actually clear, and we're going to start the app again just to make sure that it took into account, so the last implementation, so that was the first step. So what is the weather in Boston? All right, so let's see, here it is. So now, we should see an object that includes here the tool_calls. And this is specifying, so the only tool which has been defined, which is get_current_weather. And you have to know as well that you could have multiple functions, and it's going to give you a list of the functions that you have defined. So that was the first step, which is to define the functions and the parameters. And basically, the way it works is that we're going to let the chat models detect and decide if function calling is necessary. And the next step will be to then see how to call the functions.

Contents