Get weather information with ChatGPT
Yesterday, I showed how to implement image recognition using C# and Flutter.
In this article, I would like to demonstrate how to use ChatGPT to retrieve live weather data based on a user prompt.
For this, I have expanded the project from yesterday and linked it to a separate branch.
Function calling
Since mid-2023, certain GPT-3 and GPT-4 models have been supporting so-called “function calling”.
In this process, in addition to the user’s instruction, a list of one or more functions is passed to the language model, from which ChatGPT can select the appropriate one.
As a response, the client usually receives the function from this list with the arguments that the client should use.
It does not have to be a function that the client has implemented … the client only receives the function name and the list of arguments. What the client does with it is up to it. This way, it retain control.
A request to OpenAI could look like this:
{
"model": "gpt-3.5-turbo-0125",
"functions": [{
"name": "get_weather_of_location",
"description": "Gets weather information by geo location.",
"parameters": {
"type": "object",
"required": ["latitude", "longitude", "location_name", "postcode"],
"properties": {
"latitude": {
"description": "The latitude part of the geo location.",
"type": "number"
},
"longitude": {
"description": "The latitude part of the geo location.",
"type": "number"
},
"location_name": {
"description": "The official name of the location.",
"type": "string"
},
"postcode": {
"description": "The postcode of the location.",
"type": "string"
}
}
}
}],
"function_call": {
"type": "function",
"name": "get_weather_of_location"
},
"messages": [
{
"role": "system",
"content": "You are a helpful assistant that helps the user to get weather information about a location."
},
{
"role": "user",
"content": "Get the weather in Aachen"
}
]
}
Additionally you can add an optional function_call
property with the instruction, if a specific function from the list should be called or not.
On the other hand a response from OpenAI could look like this:
{
"id": "...",
"object": "chat.completion",
"created": 1700000000,
"model": "gpt-3.5-turbo-0125",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": null,
"function_call": {
"name": "get_weather_of_location",
"arguments": "{\"latitude\":50.7753,\"longitude\":6.0839,\"location_name\":\"Aachen\",\"postcode\":\"52076\"}"
}
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 132,
"completion_tokens": 27,
"total_tokens": 159
},
"system_fingerprint": "..."
}
Weather API
To get simple weather information, I am using OpenWeather for the demo.
After signed up, you can generate an API key. Keep in mind, that it takes some minutes or a few hours until the key(s) is/are finally active.
To test the API, you can try a GET request like this:
https://api.openweathermap.org/data/2.5/weather?lat=50.77526500423541&lon=6.132880133839645&appid=<YOUR-API-KEY>
Backend updates
I extended the backend with a WeatherController.
Before you can start the service, you have to add OPEN_WEATHER_MAP_API_KEY
environment variable entry to your .env.local
file in backend folder.
The things the POST handler does is:
- sending a request to OpenAI with user prompt and the introduction to return with a specific function instruction
- calling ‘get current weather’ endpoint at OpenWeather
Maybe for better results, it could be help to switch to a GPT-4 model instead using gpt-3.5-turbo-0125
as in this example.
Conclusion
This example can be extended to many other applications, like live web searches or whatever.
You only need to request ChatGPT to process a user prompt and provide a list of virtual tools to this LLM.
In this case, the backend receives the information it needs to transform historical data, such as the geo-data of a location, which it receives from ChatGPT, into live data that the backend sends to an API service.
Have fun while trying it out! 🎉