Sunday, July 9, 2023

Function Calls in OpenAI

Now our chatbot application can utilize the output of “functions” for executing it with Internet access, enabling the retrieval of up-to-date information to enhance its responses to user queries

If the functions or APIs offer both valuable and referral functionalities, there is a possibility of earning commissions by releasing and monetizing the chatbot applications with more natural interaction with users than normal Internet searching.

With OpenAI’s recent cool feature, their models have been directly fine-tuned to internally understand the format and meaning of external tools and functions. This enables them to generate JSON-based function instructions, eliminating the need for external prompt engineering works and facilitating a seamless run of proper tools during runtime.

Below is function that returns some recent information  

def get_flight_info(fly_from, fly_to, date):

    flight = {"flight_no": "AA1234", "price": 100, "depart_time": date+" 17:23:00"}

    return flight


Next task is to construct the prompt for gpt-3.5-turbo-0613 model which includes the new features we need.


prompt = [{"role": "system", "content": "You are a helpful assistant. Answer as complete as possible."}]

prompt.append({"role": "user", "content": "Show me the cheapest flight from Berlin to New York in 23 Jul"})

    

functions = [

    {

      "name": "get_flight_info",

      "description": "Get the info of the cheapest flight for a given date",

      "parameters": {

        "type": "object",

        "properties": {

          "fly_from": {

            "type": "string",

            "description": "the 3-digit code for departure airport"

          },

          "fly_to": {

            "type": "string",

            "description": "the 3-digit code for arrival airport"

          },

          "date": {

            "type": "string",

            "description": "the dd/mm/yyyy format date for flight search"

          },

        },

        "required": ["fly_from", "fly_to", "date"]

      }

    }

  ]

completion=openai.ChatCompletion.create(

        model="gpt-3.5-turbo-0613",

        messages = prompt,

        functions = functions

)


Compared to the previous usage of OpenAI API, in this new update, we add one new section to the completion API called functions which contains the function’s name, description, and parameters that inform the model of proper function usage.


message=completion.choices[0].message.function_call

print(message)


{'name': 'get_flight_info', 'arguments': '{\n  "fly_from": "BER",\n  "fly_to": "JFK",\n  "date": "23/07/2023"\n}'}


Now with the below, we can easily execute the function and give the result to the user 


import ast


data_dict = message.to_dict()

# Extract the function name and arguments from the dictionary.

function_name = data_dict['name']

arguments = ast.literal_eval(data_dict['arguments'])


# Get a reference to the function object.

function = globals()[function_name]


# Call the function with the arguments.

result = function(**arguments)


print(result)




References:

https://levelup.gitconnected.com/monetize-your-chatbot-by-using-openais-function-calling-and-langchain-157d0d97cd79

No comments:

Post a Comment