Getting started with OpenAI's function call feature

 

Author: HB of AI Little Rocket

I'm HB from AI Rocket, and I explore and write about all things at the intersection of AI and language, ranging from LLMs, chatbots, voicebots, development frameworks, data-centric latent spaces, and more.

 

example

 

image-20230620170901449

 

initial experience

OpenAI has added a "function call" feature, what is it?

Let's call the API first to experience it.

Below is the JSON document sent to the model. The purpose of this call is to generate a JSON file that can be used to send to the sending email API.

You can see that the function name is , and three parameters are defined, , , and , which are the email body.send_emailto_addresssubjectbody

The user request is:Send Cobus from humanfirst ai an email asking for the monthly report?

curl --location 'https://api.openai.com/v1/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-xxxx' \
--data '{
  "model": "gpt-3.5-turbo-0613",
  "messages": [
    {"role": "user", "content": "Send Cobus from humanfirst ai an email asking for the monthly report?"}
  ],
  "functions": [
    {
      "name": "send_email",
      "description": "Please send an email.",
      "parameters": {
        "type": "object",
        "properties": {
          "to_address": {
            "type": "string",
            "description": "To address for email"
          },          
          "subject": {
            "type": "string",
            "description": "subject of the email"
          },
          "body": {
            "type": "string",
            "description": "Body of the email"
          }
        }
      }
    }
  ]
}'

Below is the returned JSON

{
    "id": "chatcmpl-7TQuwzJpQAY470saQM2RPfxwF6DDE",
    "object": "chat.completion",
    "created": 1687249338,
    "model": "gpt-3.5-turbo-0613",
    "choices": [
        {
            "index": 0,
            "message": {
                "role": "assistant",
                "content": null,
                "function_call": {
                    "name": "send_email",
                    "arguments": "{\n  \"to_address\": \"[email protected]\",\n  \"subject\": \"Request for Monthly Report\",\n  \"body\": \"Hi Cobus,\\n\\nI hope you're doing well. Could you please share the monthly report with me? It would be great to have it before the end of the week.\\n\\nThanks,\\n[Your Name]\"\n}"
                }
            },
            "finish_reason": "function_call"
        }
    ],
    "usage": {
        "prompt_tokens": 86,
        "completion_tokens": 82,
        "total_tokens": 168
    }
}

The GPT model will return the name of the function to be called and the corresponding parameters (in the field).send_emailarguments

{
  "to_address": "[email protected]",
  "subject": "Request for Monthly Report",
  "body": "Hi Cobus,\n\nI hope you're doing well. Could you please share the monthly report with me? It would be great to have it before the end of the week.\n\nThanks,\n[Your Name]"
}

This is very useful. Third-party applications can provide multiple functions/services (similar to plug-ins), and the GPT model can automatically select different functions/services according to user instructions.

 

Now look at the example again, it will be clearer.

image-20230620170901449

 

use

According to the official website documentation , function calls allow you to get structured data from models more reliably. For example, you can:

  • Create chatbots that answer questions by calling external APIs (e.g. ChatGPT plugin)

    • For example, define a function like orsend_email(to: string, body: string)get_current_weather(location: string, unit: 'celsius' | 'fahrenheit')

  • Convert natural language into API calls

    • For example, convert "Who are my top customers?" to and call your internal APIget_customers(min_revenue: int, created_before: string, limit: int)

  • Extract structured data from text

    • For example, define a function named orextract_data(name: string, birthday: string)sql_query(query: string)

 

The basic sequence of steps for a function call is as follows:

  1. Invokes the model with a set of functions defined in the user query and function parameters.

  2. The model may choose to call the function; if so, the content will be a stringified JSON object conforming to the custom schema (note: the model may generate invalid JSON or phantom parameters).

  3. Parses the string as JSON in code and calls the function with the provided arguments (if present).

  4. Call the model again by appending the function response as a new message, and have the model aggregate the results back to the user.

 

AI small rocket

AI Rocket already supports function calls and,,, you can experience it.gpt-3.5-turbo-16kgpt-3.5-turbo-0613gpt-3.5-turbo-16k-0613

 

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/6793601/blog/10084168