Tutorial OpenAI API

Functions with OpenAI API

OpenAI API supports functions, JSON-format and external APIs – while offering lower price and longer context.

Teemu Maatta
2 min readJun 14, 2023
Photo by Joshua Aragon on Unsplash

Introduction

OpenAI announced yesterday updates to OpenAI API, which enable passing functions to the API.

The change enables:

  • Easy integration with APIs
  • Simplifies functions usage with the API.

Let’s get started!

Functions

I will start by inserting the required libraries and the usual OpenAI API key.

!pip install openai
import os
import openai
import json
openai.api_key = os.getenv("OPENAI_API_KEY")

Let’s next define a function to use:

def get_current_weather(location, unit="fahrenheit"):
"""Get the current weather in a given location"""
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)

This function is just an example, so you can create any function. The function could be for example an external API endpoint. In this basic example, the function always returns the same value for simplicity. However, a function using an external API would return each time unique response.

So, in short, we will next use OpenAI API to return response in format, which this function is able to use.

Let’s make an API call:

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[{"role": "user", "content": "What's the weather like in Boston?"}],
functions=[
{
"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",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
],
function_call="auto",
)

message = response["choices"][0]["message"]
function_response = get_current_weather(
location=message.get("location"),
unit=message.get("unit"),
)

I can now get the function name with the following code:

print( message["function_call"]["name"])

I can apart get the function data:

print(function_response)

The function response includes data in the desired format, which the function is able to use.

This makes it possible to create flows, which first retrieve user query into format requested by the function, the function is executed and finally a new OpenAI API call will respond an answer in format asked by the user based on the function response.

Conclusions

In this tutorial, I introduced using Functions in OpenAI API.

I believe this new feature makes simpler building chains, where is combined:

  • Memory
  • External APIs
  • Chaining together repeating calls to OpenAI API.

This new announcement from OpenAI extremely useful.

References

[1] OpenAI, 2023. Function calling and other API updates. https://openai.com/blog/function-calling-and-other-api-updates. ( The example used is the one provided by OpenAI and I will gradually update it with my own function and example.

--

--

Teemu Maatta
Teemu Maatta

Written by Teemu Maatta

Author (+200k views) in Artificial General Intelligence. Autonomous Agents. Robotics. Madrid.

Responses (1)