Azure OpenAI Function Calling
2K views
Dec 2, 2023
The latest versions of gpt-35-turbo and gpt-4 have been fine-tuned to work with functions and are able to both determine when and how a function should be called. If one or more functions are included in your request, the model will then determine if any of the functions should be called based on the context of the prompt. When the model determines that a function should be called, it will then respond with a JSON object including the arguments for the function.
View Video Transcript
0:00
Welcome to the last and final lab of this course
0:05
So, basically, this lab covers function calling of Azure OpenAI offering
0:16
So, in a nutshell, what this lab does is it takes input from the user
0:24
and if the user tells the query as how is the weather in a particular city or a particular state
0:33
It uses function calling to retrieve the required parameter that is the name of the city or the name of the state
0:41
And then it uses the get weather function to connect to an API, to a real-time weather destination
0:58
determination api and then from that api we fetch the value of or we fetch the uh
1:08
weather parameters of the particular city or the particular area that we are concerned about
1:14
so let's get started with the lab so uh in this lab we also have two files one is for the for
1:21
setting the environment variables and the second one contains the already written code so you don't
1:26
have to do much work okay so the first step would be to set the environment
1:32
variables we need two environment variables one for restoring the open AI
1:37
key and one for storing the open AI pay so yeah navigate to Azure open AI studio
1:45
Go to deployments. Oh, sorry. Navigate to portedloadasio.com and go to your OpenAI resource
1:58
In my case, it is Prabhjot OpenAI. Click on Keys and Endpoint
2:05
Copy this primary key 1 value. Paste it here. Now we need to fetch the base endpoint URL
2:14
navigate again to the same page copy this and point url and paste it here click on ctrl plus s to
2:22
save the changes so okay so let's have a look at the code uh in the first part of my code i am
2:32
importing some very important libraries to just to make sure that my code works very efficiently
2:38
In the second part of the code, I am setting configurations of the openAI model
2:45
So this is another very important part of the code that you should keep in mind The third part of the code contains the function definition in which the name of the function is getWeather Description is retrieves real data
3:07
weather data of a particular location the user is concerned about
3:21
Yeah. So the parameters for this function definition are location, which is a string type variable
3:29
And the description is the exact location whose real time whether it is to be determined
3:34
And the required parameter is location. So this required parameter is to be fetched by Azure OpenAI model
3:42
using its intelligence. So, as soon as I hit my query into the model, this model is going to use its LLM, large language
3:54
model intelligence to fill in this box. Message definition has been written here
4:05
We have two parts, system and user. system content is you are an assistant that helps people retrieve real-time weather data
4:14
slash info and the user content is how is the weather in mumbai okay so initial response i am
4:22
storing initial response is a variable that i have defined to store the response of the
4:27
chat completion api uh engine name should be yeah so uh engine name uh there is
4:38
something important that needs to be known. So basically, function calling is only supported by specific models
4:48
And these specific models include a ChatGPT35Turbo0613 model version, this model, this model
4:59
and this model, GPT35Turbo16K only. So if you pass a GPT35Turbo0301 model
5:08
function calling is not gonna work on and if you want function calling to work
5:14
keep in mind to deploy GTP 3035 turbo 0613 model version and GPT 35 turbo 16k 0613 model version
5:24
only so I already have deployed GPT 35 turbo 0613 version in my deployment step so there is no need
5:34
me to deploy it again so i'm just gonna copy this name right over here and paste it over here yeah
5:43
new deployment uh okay here everything kind of looks fine Okay so Yeah yeah yeah So this is the part where I going to use Open Weather Map API
6:01
So let's see what is Open Weather Map API. Open Weather Map API documentation
6:16
So, open weather map is an open source or is free of cost API which outputs the weather
6:42
condition for a particular area or for whichever location you are interested in knowing about
6:49
So this is the API call URL in which what we require are the latitude and the longitude and
6:56
the API key of our account. And the API response in JSON format looks something like this
7:06
Yeah, it looks something like this. So in the weather array, the description contains how the weather is for an area that we are
7:22
concerned about. So we are going to print description from the weather array in our output. Okay
7:31
But for making a call to this API, what we need is latitude and longitude
7:36
So for fetching the latitude and longitude of a particular location, I have used another API that is of open weather map only
7:45
So open weather map geocoding API. geocoding API. Yeah
7:57
So, this is the geocoding API of OpenWeatherMap in which, by which I can get the latitude
8:08
and corresponding longitude of a particular location by making a call to this specific URL
8:15
in which I need an API key of my account. So, the API response in JSON format looks something like this, where we have the latitude
8:27
and the longitude of the location that we are concerned about. So, and now to generate an API key, just go to your account
8:37
First, create your account in Open Weather Map and go to your account
8:41
Click on My API Keys And these are the corresponding keys so let me generate a new API key new API key okay so a new API key has been generated which I can use in my
9:03
current code so let us copy this okay so now in this URL area I need to input my
9:19
API key okay so with this in this part we are fetching the longitude and the
9:28
latitude of the location that we are concerned about location through the geocoding API of Open Weather Map organization
9:37
And in this part of the code, we are fetching the weather description
9:44
and then printing the weather description correspondingly. So, here also, I need to paste my API key
9:52
Control plus V for pasting the API key. Okay. Okay. I think we are pretty much done for this goal
10:03
control plus s to save the made changes okay so we are ready to run a code let's
10:11
run a code open this folder in an integrated terminal python test dot 5
10:22
yeah so the weather description has been returned as smoke let's do for other
10:31
locations as well. Let's say how is the weather in London. Click on control
10:39
process. Let's run our code again. Light rain. Okay. So the weather description for Mumbai was smoke and the
10:53
weather description for London was light rain as returned by Open Weather Map API. So yes, this code was about using function calling in Azure OpenAI. So you see
11:09
how with the help of Azure OpenAI function calling, you can automate your code processes
11:17
as well, making your code run faster with improved intelligence. So I think of it as a great usage
11:24
I think of it as a very powerful usage, which is going to change the entire coding arena
11:30
So yeah, this was it for this lab and this course as well. Hope you enjoyed the course
11:39
Keep watching, keep learning. Thank you
#Engineering & Technology
#Programming
#Software