In this video, we're going to dig into OpenAI function calling. We'll explore what's happening under...
Video Transcript:
in this video we're going to talk about open aai function calling so the first thing to note is that the name is quite confusing open AI isn't doing any function calling itself instead it's suggesting which function you should call from your code and with which parameters and this functionality exists to make it easier to pull in external data into our llm applications and basically into the prompt to do something with it so let's have a look at how it works so we've basically got four components we've got our functions we've got the application we've got the API this is open AI API and then we've got the llm sort of behind that so let's have a look what happens so from our application we create a bunch of function definitions and a prompt we pass that in we call that to the API the API then takes that and converts it into another prompt uh which has a system prompt containing functions and then the prompt itself and the llms behind that so I the GPT 3. 5 and four have been fin tuned to know what to do when they receive that system prompt and so they will then send back here's the function name and the parameters and the values that go with that and then that will get sent back from the API to our application we then need to take those parameters and values call the function itself in our code get and get back the results we then take those results and put them into the basically into the open AI message history send that back to the API that'll then send it to the llm and then that will then come back with effectively the answer to our prompt so let's see how we do this in code so I've taken the weather example from the open AI docs and then I've just substituted the hard coding for a real weather API so we can see how it works in real life so let's open up this file function calling open A. P and you can see we've got some imports at the top and then we're initializing the openai client we've then got a function called get current weather now that takes in a latitude and a longitude it then is going to make a call to the open weather map API you can see I've got my weather map API key getting pulled out of the environment there and then we're constructing a request URL that contains the latitude the longitude and my key we then make a call to that using requests and then we construct a result and we're just putting in the latitude the longitude and then we're putting in all the values from the main part of that response and then we'll convert that into Jason and send it back if we then come down a little bit more you can see we've got another function called run conversation and that's where the prompt is going to come in we're going to create a me put that into a messages array then if we come down a little bit more we're going to Define our tools now the tools need to have first of all a type so the type is function and then we Define the function so we give it a name so we're going to call it get current weather and then we pass in the parameters so remember those are going to be latitude and longitude and then we give the information about them as well and that's in Json schema format we then make our call to open AI so we're going to use GPT 3.
5 you can see we pass in the messages so that's going to have the prompt and then we pass in those tools so that's going to have the definition of our current weather function we then get back to the response and we pull out the tool calls we're then going to check whether we've got any tool calls we'll put the response that we just got into the messages array we're then going to define a dictionary um called available functions that's going to go from that string get current weather to the actual function itself and then we're going to iterate over the tool calls we'll print out the function name that it think we tried to we want to call the parameters that go with that and then we're going to kind of go through get again pull out the function find the underlying function to call get the arguments and then we're going to make the call to the function I. E it's going to go and call that get the weather from the the underlying API and then we'll print the response that we get we'll then put the response into that messages array along with the the tool call ID and then we're going to make our second call to the API so this time it's going to it's going to be quite similar to the first one but we don't have any tools this time so we just have the messages and we're going to pass that in and then if we come down to the bottom we're then going to say our question is what's the weather like Paris we're going to call the Run conversation with that question and then we'll iterate over the chunks we get back and print them to the screen and you can see it comes back so we get the first thing is the functions let's get current weather you can see the parameters it's got the latitude of Paris the longitude of Paris then the API calls it's got a bunch of information about the weather in Paris right now and then finally we get the llm response so the current weather in Paris is 10.