Defining function calling

Developing AI Systems with the OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

OpenAI's tools

 

  • Can be used to return more specific information
  • Can define a more precise structure
  • Enhances the capabilities of the API call

An illustration of two people handling tools and a large screen

Developing AI Systems with the OpenAI API

OpenAI's tools

A diagram showing a user message sent to the OpenAI API with a response connected to a smart home

Developing AI Systems with the OpenAI API

OpenAI's tools

A diagram showing a user message sent to the OpenAI API with the tools option, with a response connected to a smart home with a connected lightbulb

Developing AI Systems with the OpenAI API

What is function calling?

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[
   {"role": "user", 
   "content": "Please write down four trees with their scientific names in json."}
 ],
  response_format={"type": "json_object"}
)
print(response.choices[0].message.content)
{"trees": [{"common_name": "Oak", "scientific_name": "Quercus"}, { "common_name": 
"Maple", "scientific_name": "Acer"}, {"common_name": "Pine", "scientific_name": 
"Pinus"}, {"common_name": "Birch", "scientific_name": "Betula"}]}
Developing AI Systems with the OpenAI API

Why use function calling?

 

{"trees": [
           {"common_name": "Oak", 
           "scientific_name": "Quercus"}, 
           { "common_name": "Maple", 
           "scientific_name": "Acer"}, 
           {"common_name": "Pine", 
           "scientific_name": "Pinus"}, 
           {"common_name": "Birch", 
           "scientific_name": "Betula"}
           ]
}

 

{"Oak": "Quercus",
 "Maple": "Acer",
 "Pine": "Pinus", 
 "Birch": "Betula"}
Developing AI Systems with the OpenAI API

Use cases for function calling

 

Going from unstructured to consistent structured output

  An illustration of someone controlling their home's temperature from their phone

Developing AI Systems with the OpenAI API

Use cases for function calling

 

Calling multiple functions to provide complex responses

An illustration of a customer chatting with a customer service bot

Developing AI Systems with the OpenAI API

Use cases for function calling

 

Calling external APIs

A woman next to a screen with weather information for New York

Developing AI Systems with the OpenAI API

Let's practice!

Developing AI Systems with the OpenAI API

Preparing Video For Download...