DEV Community

Cover image for Advance Function Call With Open AI
Roshan Sanjeewa Wijesena
Roshan Sanjeewa Wijesena

Posted on

Advance Function Call With Open AI

Sometimes you may be wondering can I book a flight using chatGPT or even can I get information about latest flight details for your favourite holiday destination? The simple answer as of today is no. ChatGPT 3.5 has training data up to 2021 sept, hence it does not know any real time informations as is today.

But using openAI APIs we can build an generative AI application call an extra API to get latest information like real time flight details and embedded to our genAI app.

Lets build a GenAI application to get latest flight information from any given destination

  1. You need to acquire openAI API key. - https://platform.openai.com/api-keys

  2. We need python development environment for this application you can use jupyter notebook or my favourite is Google Colab. - https://colab.google/

  3. You need to install below python packages

!pip install openai
Enter fullscreen mode Exit fullscreen mode
  1. Set your OpenAI API Key in to the python application and set function description
import openai
from openai import OpenAI

openAIKey="YOUR_OPENAI_APIKEY"

client = OpenAI(api_key=openAIKey)

function_description = [
    {
        "name": "get_flight_info",
        "description": "Get the next flight from delhi to mumbai",
        "parameters": {
            "type": "object",
            "properties": {
                "from": {"type": "string", "description": "The depature airport e.g DEL"},
                "to": {"type": "string", "description": "The destination airport e.g SYD"},
                "date": {"type": "string"},
            },
            "required": ["from", "to"],
        },
    }
]
Enter fullscreen mode Exit fullscreen mode
  1. Now you can define your prompt, this is the real question that would ask by end-user
user_prompt = "When's the next flight from Colombo to Sydney? "
Enter fullscreen mode Exit fullscreen mode
  1. Now you need to call openAPI to grab origin and destination air ports from user prompt
response2 = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": user_prompt
        }
    ],  
    #Add function calling
    functions=function_description,
    function_call="auto" # specify the function call

)
origin = json.loads(response2.choices[0].message.function_call.arguments).get("from")
destination = json.loads(response2.choices[0].message.function_call.arguments).get("to")
Enter fullscreen mode Exit fullscreen mode
  1. This is the fun part, you can build your own function to call real time flight information API, accepting any parameters. For this example, i would mimic sample response from an API.
import json
from datetime import datetime, timedelta
def get_flight_info(from_airport, to_airport):
  """Get Flight information between two airports"""
  #Example out put

  flight_info = {
      "from_airport": from_airport,
      "to_airport": to_airport,
      "date": str(datetime.now() + timedelta(hours=2)),
      "airline" : "Qantas",
      "flight_number": "QF466"
  }
  return json.dumps(flight_info)
Enter fullscreen mode Exit fullscreen mode
  1. Now you pass this function back to openAI API
response3 = client.chat.completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {
            "role": "user",
            "content": user_prompt
        },
        {
            "role": "function",
            "name": "get_flight_info",
            "content": get_flight_info(origin, destination)
        }
    ],  
    #Add function calling
    functions=function_description,
    function_call="auto" # specify the function call

)

response3.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode
The next flight from Colombo (CMB) to Sydney (SYD) is on May 14, 2024, at 11:30 AM. It is operated by Qantas with flight number QF466.
Enter fullscreen mode Exit fullscreen mode

Here is the code - enjoy
https://github.com/rswijesena/AI/blob/3bffb41787c1a5a0110993e305005b0ff990d362/Advance_OpenAI_Function.ipynb

Top comments (0)