OpenAI Function Calling — Integrating External Weather API

Zein Ismalingga
3 min readApr 4, 2024

--

Introduction

In today’s digital world, it’s important for apps to use outside data well. OpenAI’s Function Calling helps with this by letting developers easily add other services into their projects. This guide will show you how to use Function Calling to add a weather forecast to your app, making it better for users.

Understanding OpenAI’s Function Calling

OpenAI’s Function Calling is like a shortcut for developers. It lets them run code from other services right in their own apps. This makes it super easy to use external APIs without dealing with lots of setup steps. It saves time and makes development smoother.

Choose a Weather API

When integrating a weather API, it is crucial to choose a reliable provider that offers comprehensive weather data and robust API endpoints. We will be using the OpenWeatherMap. You can register at https://openweathermap.org/.

Let’s break down step by step

  • Import Libraries
from openai import OpenAI
import requests
import json
  • Initialize OpenAI Client. Replace with your OpenAI API key.
client = OpenAI(api_key="sk-123")
  • Define Function to Get Current Weather. Replace the “YOUR_API_KEY” with your OpenWeatherMap API key.
def get_current_weather(location):
API_KEY = "YOUR_API_KEY"
url = f"https://api.openweathermap.org/data/2.5/weather?q={location}&appid={API_KEY}"
response = requests.get(url)
data = response.json()

# Extract the temperature in Kelvin
kelvin_temp = data['main']['temp']

# Convert Kelvin to Celsius
celsius_temp = kelvin_temp - 273.15

return {"location": location, "temperature": round(celsius_temp, 2)}

This function takes a location (city or state) as input and returns the current weather information in Celsius for that location.

  • Define Tools and Messages
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
},
"required": ["location"],
}
}
}
]

messages = [
{"role": "user", "content": "What's the weather like in Jakarta?"}]
  • Invoke OpenAI’s API
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
tools=tools,
tool_choice="auto", # auto is default, but we'll be explicit
)
  • Extract Location from Response
arguments = response.choices[0].message.tool_calls[0].function.arguments

data_json = json.loads(arguments)

location = data_json['location']

If we print the arguments variable will get this response:

{"location":"Jakarta"}
  • Retrieve Weather Data
weather_data = get_current_weather(location)
  • Invoke OpenAI API Again
messages = [
{"role": "system", "content": "You are a helpful assistant. Answer this weather information in celsius."},
{"role": "user", "content": f"Here is the weather in {location}: {weather_data}"},
]
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=messages,
)

print(response.choices[0].message.content)

The response:

The weather in Jakarta is 27.02°C.
  • Check the weather on OpenWeatherMap

As you can see the result from OpenAI Function Calling and OpenWeatherMap is the same.

--

--