How to use OpenAI Function Calling

OpenAI function calling is a new feature that allows you to retrieve structured outputs from the GPT-4 and GPT-3.5 models. This can be used to call remote APIs, create JSON data, or even control other devices. To use OpenAI function calling, you must first declare a set of functions that you want the model to be able to call.

In this article, we will look at how to use OpenAI function calling effectively, and we will walk you through the process step by step.

How Does ChatGPT’s Function Calling Work?

In ChatGPT, OpenAI function calling is the process of generating a response including information about the function to be called and its arguments. It should be noted that AI models such as ChatGPT do not directly execute functions. Instead, they pass along the relevant information to the developers, who subsequently implement the function and provide the output to the model.

Define Functions?

To define functions for ChatGPT, you can utilize the new OpenAI Completion API and provide the necessary parameters. Here is a step-by-step procedure for defining functions:

  1. Specify the Functions Parameter: Include a “functions” parameter in the OpenAI Completion API that comprises a list of dictionaries specifying the possible functions. Each dictionary should represent a single function and contain the following information:
  • “name”: The function’s name.
  • “description”: An explanation of what the function does.
  • “parameters”: A dictionary containing the names and types of the function’s arguments.
  1. Define Function Arguments: Within the “parameters” dictionary, define the function’s arguments. Give each parameter a name and a type. You can utilize simple data types such as string, number, or Boolean, as well as more complicated data types such as lists or objects.
  2. Specify Required Argument: If specific arguments are required for the function to work properly, you can indicate this by adding an extra key-value pair to the argument description. You can, for example, include a “required”: true entry to declare that an argument is required.
  3. Define permissible Argument Values: You can include a “enum” key in the argument specification to limit the permissible values for an argument. The “enum” value should be an array containing all of the possible possibilities.
  4. Configure Function Calling: The “function_call” parameter can be used to activate or disable OpenAI function calling in ChatGPT. Setting it to “auto” instructs ChatGPT to use any function from the specified list. Setting “function_call” to “none” disables function calling entirely.
See also  14 Best October Prime Day 2023 Deals Under $100

How to Install OpenAI?

Follow these simple steps to install OpenAI with pip:

Launch the command line interface.

pip install openai

Run the command line for Open AI. Then Allow the installation process to finish.

Features of Function Calls

  1. Create chatbots that respond to questions by calling external programs (such as ChatGPT Plugins).
  2. Natural language can be converted into API requests or database queries.
  3. Textual structured data extraction

Example: OpenAI Function Calling

OpenAI function calling structures output for machine consumption in the form of an API, as opposed to unstructured natural language consumption by humans. This implies that the output is in a machine-readable format, such as JSON or XML. This makes it easier for developers to integrate OpenAI function calling into their applications.

For example, if you are creating a trip booking application, you might utilize OpenAI function calling to book flights and accommodations. The function call’s result would be in a format that your program could comprehend, such as JSON. This would allow you to schedule trips for your consumers fast and effortlessly.

For example, the function call’s output could be a JSON object containing the following data:

  • A list of available flights, including the airline, flight number, departure and arrival dates and times, and cost.
  • A list of available hotels, including the name, address, and nightly rate.

Your application may then use this information to plan trips for your users. For example, it may show the user a list of flights and hotels and let them choose which ones they wish to book. The program may then use the JSON object to book the user’s travel.

See also  NotebookLM – Google Allows You to Collaborate with Virtual Research Assistant

import json import openai import requests from tenacity import retry, wait_random_exponential, stop_after_attempt from termcolor import colored GPT_MODEL = “gpt-3.5-turbo-0613”

Here are a few additional functions for calling the Chat Completions API and maintaining conversation state:

@retry(wait=wait_random_exponential(min=1, max=40), stop=stop_after_attempt(3)) def chat_completion_request(messages, functions=None, function_call=None, model=GPT_MODEL): headers = { “Content-Type”: “application/json”, “Authorization”: “Bearer ” + openai.api_key, } json_data = {“model”: model, “messages”: messages} if functions is not None: json_data.update({“functions”: functions}) if function_call is not None: json_data.update({“function_call”: function_call}) try: response = requests.post( “https://api.openai.com/v1/chat/completions”, headers=headers, json=json_data, ) return response except Exception as e: print(“Unable to generate ChatCompletion response”) print(f”Exception: {e}”) return e def pretty_print_conversation(messages): role_to_color = { “system”: “red”, “user”: “green”, “assistant”: “blue”, “function”: “magenta”, } formatted_messages = [] for message in messages: if message[“role”] == “system”: formatted_messages.append(f”system: {message[‘content’]}n”) elif message[“role”] == “user”: formatted_messages.append(f”user: {message[‘content’]}n”) elif message[“role”] == “assistant” and message.get(“function_call”): formatted_messages.append(f”assistant: {message[‘function_call’]}n”) elif message[“role”] == “assistant” and not message.get(“function_call”): formatted_messages.append(f”assistant: {message[‘content’]}n”) elif message[“role”] == “function”: formatted_messages.append(f”function ({message[‘name’]}): {message[‘content’]}n”) for formatted_message in formatted_messages: print( colored( formatted_message, role_to_color[messages[formatted_messages.index(formatted_message)][“role”]], ) )

Let’s create some function specifications to interface with a Travel API. We can then pass these function specifications to the Chat Completions API in order to generate function arguments that adhere to the specification.

For example, the OpenAI function calling output could be a JSON object containing the following data:

  • A list of available flights, including the airline, flight number, departure and arrival dates and times, and cost.
  • A list of available hotels, including the name, address, and nightly rate.

Your application may then use this information to plan trips for your users. For example, it may show the user a list of

{ “model”: “gpt-3.5-turbo-0613”, “messages”: [ {“role”: “user”, “content”: “I need to book a trip from Bonn to Amsterdam for my wife, mother and by two sons and daughter. I will also be joining them. The airline must fly direct.”} ], “functions”: [ { “name”: “book_travel”, “description”: “Book travel”, “parameters”: { “type”: “object”, “properties”: { “destination”: { “type”: “string”, “description”: “Your travel destination.” }, “departute”: { “type”: “string”, “description”: “From where are you traveling” }, “number_people”: { “type”: “string”, “description”: “How many people are traveling” }, “travel_mode”: { “type”: “string”, “description”: “What mode of travel will it be.” } } } } ] }

See also  How Stealth Writer AI Can Help You Create Humanized Content for Your Audience

And here is the output:

{ “id”: “chatcmpl-7R3vsPC6JndweAQZXvCIvSWnaXDMP”, “object”: “chat.completion”, “created”: 1686684328, “model”: “gpt-3.5-turbo-0613”, “choices”: [ { “index”: 0, “message”: { “role”: “assistant”, “content”: null, “function_call”: { “name”: “book_travel”, “arguments”: “{n “destination”: “Amsterdam”,n “departute”: “Bonn”,n “number_people”: “6”,n “travel_mode”: “airline”n}” } }, “finish_reason”: “function_call” } ], “usage”: { “prompt_tokens”: 122, “completion_tokens”: 42, “total_tokens”: 164 } }

Some Examples of Define Functions for ChatGPT:

Example 1 – Send Email

  • Email Hollie to see if she wants to get coffee next Saturday
  • send_email (to: string, body: string)

Example 2 – Weather API

  • What’s the weather like in chennai?
  • get_current_weather (location: string, unit: ‘celsius’ | fahrenheit’)

Example 3 – Natural Language to Function/SQL

  • Who are my top ten customers this month?get_customers_by_revenue(start_date: string, end date: string, limit: int)
  • How many orders did Acme, Inc. place last month?
  • ddhgiuery (query: string)

OpenAI Function Calling Tips and Tricks

  • Make certain that the names of your functions are simple to understand.
  • Give your parameters descriptive names.
  • Specify the type of data that your functions return.
  • Test your functions rigorously before putting them into production.

Also read: How to Use GPT-4 and ChatGPT on Azure OpenAI Service

Conclusion

To utilize OpenAI function calling effectively, it is crucial to understand the available functions, their parameters and correct syntax. Providing appropriate inputs, handling outputs and iterating for improvement will lead to successful implementation. Please feel free to share your thoughts and feedback in the comment section below.