From the course: OpenAI API for Python Developers

Challenge: Connect to a public API

From the course: OpenAI API for Python Developers

Challenge: Connect to a public API

(bright music) - [Instructor] The next step will be in the form of a challenge. After defining functions, the challenge for you is to connect to an API, and this will be openweathermap.org, which is an online service that provides global weather data and that also provides a nice API for developers who need to integrate current weather data into their web applications. And this web service is very generous because it allows a thousand calls per day and for free, which is more than enough to test out the different API endpoints that you're going to find right here in order to get, for example, the current weather data. So we're going to check out the documentation right here very quickly. And in order to make an API call to get the current weather data for one location, you need to call this endpoint and specify the latitude and longitude and also an API key. So this is mandatory in order to be authorized to make API calls. So here, after you create an account, you need to go here, my API keys and then create a new one, so in order to be authorized to make API calls to this endpoint. And we're going to look back to the documentation and we're going to see that here you need the latitude and longitude. And for your convenience, you have another endpoint, which is available, also provided by openweathermap that allows you to get the coordinate, the geographical coordinates with one location by providing a location as an input. So let's go back to the starter projects. You're going to find right here one file, which is utils.py. And inside, you're going to find this function that are already defined. So this is already conveniently provided to you. Basically, what you have is this function, which is get current weather. And because we're not going to be naturally giving the information about geographical coordinates, we're going to provide with one location, we're going to use this function, this usual functions, to get the coordinates based on the user inputs. So when we ask about the weather forecast in Paris, it's going to find the coordinates for us. Then return this as a response. And so we can use those two object information. So that's going to be in the form of a string object. And then pass this as a parameter. And of course, you're going to need to define also, an API key. So this is defined in an environment variable, which is available at the same location where you define your key for the OpenAI API. So you must remember also to set that up. So that's going to be required. Let's go back here just a moment to understand how we proceed. So basically, we define this function. So remember that we need to also specify the function that we want to call after we trigger function calling. So we're going to go back to the main file right here. So we're going to make sure that we add this to the scope. So that's going to be from utils and import, get current weather. And like this, we just need to replace, remember that we had put an empty object for now, so I'm going to replace this like this. All right, so this means that whenever we trigger these tool calls, we're going to allow the chat models to trigger the function calling and allow to call this function. So that's going to be the next step for you, which is to write here. So let's go back to the different steps, which is to then send the information on the function call. And basically, let's go back to call functions. Just to explain to you, we're going to wait for the function response and then send it as a new response. So we're going to need this new information in order to allow the chat models to return a structured response by using the information that it could get by calling the external API. So I'm going to leave this to you, so that's going to be the challenge for you. The goals will be to combine the power of the ChatGPT API with the ability to extend the response by calling APIs in order to get the current weather report from the assistance. So good luck to you.

Contents