Page cover

Function Calling

Function calling lets models use APIs or functions to perform tasks, get real-time data, and respond intelligently to user input.

Function or tool calling enables models to connect to predefined functions and APIs, allowing them to carry out tasks, retrieve real-time data, and interact with various systems. It’s useful for building more capable assistants, chat applications, and agentic systems that need to take action based on user input.

Before looking at an example, let's define some core concepts:

Core Concepts

Tools

Tools or functions provide a model with access to specific capabilities or data sources. Each tool has a name, description, and parameter schema. When included in a request, the model can choose to when use them.

Tool Calls

A tool call is a response from the model that occurs when it has access to a set of tools and determines that using one of them is necessary to correctly address the question or instructions in the prompt. Here are key parameters to pass when calling the model related to function/tool calling:

  • tools Defines the functions the model can call, including their names, descriptions, and parameter schemas.

  • tool_choice Controls whether the model should decide when to call a tool. The supported value is auto, which lets the model choose based on the prompt

Tool Call Outputs

This is the result of running the tool or function with the arguments the model provided. These are provided to the model, and the model uses it to generate the final answer.

  • tool_callsContains the list of tool calls returned by the model. Each entry includes the tool name, the arguments the model generated, and an ID used to link your tool output back to the call.

  • function.name The name of the tool or function the model selected.

  • function.arguments A JSON string with the arguments the model generated for the tool.

  • id A unique identifier for the tool call, useful when handling multiple calls/requests or in multi-turn settings.


Example: Function Calling with Custom Python Functions

This example demonstrates how to use function calling with an OpenAI-compatible model to fetch real-time financial data using the yfinance library. The model can retrieve stock prices, CEO names, and business summaries for publicly traded companies by invoking custom Python functions.

Before running the example, make sure the following Python packages are installed:

  • httpx is an HTTP client for Python that provides asynchronous support.

  • The [http2] extra enables HTTP/2 support, which improves efficiency in communication with APIs.

Expand for Example Code

Initializing the Client - Optimizing with Httpx

  • http_client=httpx.Client(http2=True):

    • Configures the HTTP client with HTTP/2 support for faster, more efficient communication.

Custom functions for stock research

Register the functions as tools

Step 5: Creating the Initial Model Call (Function Calling)

By setting tool_choice="auto", the model will decide whether to call a tool and which to call, or answer directly.

At this stage, the model responds either with:

  • a function/tool call including arguments, or

  • a normal text response (if no function is needed).

Handling Tool Calls and Returning a Response

After the user prompt is sent, the model may respond with one or more tool calls. This step processes those tool calls, executes the corresponding functions locally, and sends the results back to the model to complete the conversation.

Example Output

Last updated