From the course: OpenAI API and MCP Development
Integrating the weather API
From the course: OpenAI API and MCP Development
Integrating the weather API
With function calling, you can give the language model the ability and the functionality to fetch external and current data from public APIs, databases, or even from your own backend API. In this video, I'm going to show you the steps to integrate an API to enhance and leverage the model responses with external and current data. We're using OpenWeatherMap.org, which is an online service to provide with global weather data. It also provides with an API for developers to integrate current weather data information into their web application. With the free plan, you are authorized to run and send 1,000 API calls which is more than enough to test it out. I recommend that you create an account and then that you follow here this step to create a new API key that you need to add to your projects in the same location, in the .ev file, the same way that you would need an API key for the OpenAI API to be authorized to send API calls. So that's going to be the first step in order to be set up and then we're going to look at the documentation. So this one, the current weather data, I'm going to look at the API doc to show you one API call example and what you need is the latitude and longitude coordinates and also provide with an API key. So these are the three required parameters whenever you define your API call. Then we're going to look below at one response example in JSON format. What we need to look at is the weather description information and also of course the weather data and temperature which is in Kelvin units. Let's go back to our project. I'm going to show you this part. So where we have already set up a few helper functions. So you need to get an API key. So this needs to be set up in the .env file. Below line 14 you have a few helper functions to convert from Kelvin to Celsius and also to Fahrenheit. Then we have a few functions to send API calls. And the first function, line 22, is to get the coordinates, the latitude and longitude information given a location. So we do that actually behind the scene because you're going to see right below that this is the function that we call whenever we run function calling, which is getCurrentWeather. All right, so we give a location, then line 42 we run geocode to get the coordinates to then use line 48 in our API call. Finally, we run the requests. The final step is to create this object, the JSON format, which is a structured type of response that we define then send to use in the next API call to the language models. Let's go back to the main.py. Remember that you need to add to the scope of your project this function, which is getCurrentWeather. We use it to then connect and interact with the current weather API. And we go back to the function calling flow all the way to the bottom. I'm just going to fix a few things because actually here, that's going to be part of the step two. So I'm going to move this to step two. And then step three is going to be the actual function calling when we get the JSON object, including the information of the weather data. And then we use this JSON object information to send another API request to the language model. So that's going to be here. And I'm going to call it extended response. So you can call it the way that you want, final response or extended response. and we want to run a chat completion request to include the messages. All right, so that's gonna be done. Actually, yeah, we should add it also because I think that we're not dealing with this at the top. So we need to make sure that we append also the response, extended response, once we're done with it. All right, so let's do this as well here, step four. If we go back up, going to see that inside call function, you make the language model aware of the response that we get after running function calling. So we append the response. And this is what allows the language models to extend the conversation using the function response. All right. So the function response includes, remember, the weather data. We're going to see it so whenever we print here line 69. So we're going to go back and run another demonstrations before we actually get the extended response. Okay, so let's try that. I'm going to go here and then run again with Python main.py. And I'm going to ask what is the weather in London? Again. Because I just want to show you the flow and also what is printed to understand. so the different steps, that allows us to interact first with the API and then integrate the response from calling the API and send the response to the language model. So here you have the location London, which is part of the request, our prompt. Then we have the temperature information, also the forecast information. So we know that this is overcast clouds. All right, and we're going to use that. be sent to the next request here. But we cannot actually see it. So I need to print it to allow you to read the response, this time from the language model in natural language. So it's going to sound like a response from a chatbot with external data information. Let's try that again. I'm going to run again, python main.py. I'm going to ask again. So this time, I'm going to ask Paris, What is the weather in Paris? All right, so we've got the object. And now we can read that this is giving us, actually, the weather in Fahrenheit. So we're going to actually convert. So it's not pretty accurate, because we would expect, actually, it looks like the unit is null. It can happen, because it's not deterministic. So let's actually try to look up the weather in Paris. Or we're going to convert first Fahrenheit to Celsius to see what is the result. So I'm going to put that here. And it looks like this is 13 degrees, which I think it's about right. We're going to look up Paris weather. And I think it comes down to 12. Yeah, it was closer to 13 before when I checked again. So I think it's fine. It's pretty close. So we know that this is pretty accurate and that it worked as expected. It was able to integrate the information that we collected and fetched by calling the API and that we sent to the language model in order to extend the response.