OpenAI Function Calling and API Updates

OpenAI has introduced function calling for the gpt-4-0613 and gpt-3.5-turbo-0613 models, allowing developers to connect GPT's capabilities with external tools and APIs by generating structured JSON outputs. This update enables models to intelligently detect when a function needs to be called and produce arguments that adhere to a specific function signature.

Function Calling Capabilities

Function calling allows developers to receive structured data from the model instead of natural language, which can be used to trigger external actions or retrieve information. The models have been fine-tuned to detect the necessity of a function call based on user input and respond with JSON that matches a provided JSON Schema.

Key use cases include:

  • External Tool Integration: Creating chatbots that answer questions by calling external tools, such as converting a request to "Email Anya to see if she wants to get coffee next Friday" into a send_email(to: string, body: string) call.

  • Natural Language to API/Database Queries: Converting user queries into internal API calls or database queries. For example, "Who are my top ten customers this month?" can be converted to get_customers_by_revenue(start_date: string, end_date: string, limit: int) or a SQL query via sql_query(query: string).

  • Structured Data Extraction: Extracting specific information from text. For example, a function called extract_people_data(people: [{name: string, birthday: string, location: string}]) can be used to extract people's data from a Wikipedia article.

Technical Implementation

These capabilities are enabled by new parameters in the /v1/chat/completions endpoint: functions and function_call. Developers describe their functions to the model using JSON Schema, and can optionally force the model to call a specific function.

Example Workflow

  1. OpenAI API Call: The developer calls the model with the user's input and a list of available functions.
  2. Third-party API Call: The developer uses the model's JSON response to execute a call to a third-party API or internal tool.
  3. OpenAI API Response: The developer sends the result of the tool's output back to the model to generate a final natural language summary for the user.

Security Considerations

OpenAI notes that while function calling improves tool integration, security risks remain. A proof-of-concept exploit has demonstrated that untrusted data from a tool's output can potentially instruct the model to perform unintended actions.

To mitigate these risks, developers are encouraged to:

  • Consume information only from trusted tools.
  • Implement user confirmation steps before performing actions with real-world impact, such as making purchases or sending emails.

Model Versioning and Stability

OpenAI has extended support for gpt-3.5-turbo-0301, gpt-4-0314, and gpt-4-32k-0314 until at least June 13, 2024, following customer feedback.

To ensure application stability, OpenAI allows API users to pin specific model versions (e.g., using gpt-4-0314 instead of the generic gpt-4 pointer). Each pinned model is stable and will not undergo changes that impact outputs. OpenAI also encourages developers to contribute to the OpenAI Evals library to report shortcomings in new model versions.

Sources