Function Calling
Enable LLMs to interact with external services and APIs
Understanding Function Calling
Function calling (also known as tool calling) allows LLMs to request information from external services and APIs. This enables your bot to access real-time data and perform actions that aren’t part of its training data.
For example, you could give your bot the ability to:
- Check current weather conditions
- Look up stock prices
- Query a database
- Control smart home devices
- Schedule appointments
Here’s how it works:
- You define functions the LLM can use and register them to the LLM service used in your pipeline
- When needed, the LLM requests a function call
- Your application executes any corresponding functions
- The result is sent back to the LLM
- The LLM uses this information in its response
Implementation
1. Define Functions
Pipecat provides a standardized FunctionSchema
that works across all supported LLM providers. This makes it easy to define functions once and use them with any provider.
Using the Standard Schema (Recommended)
The ToolsSchema
will be automatically converted to the correct format for your LLM provider through adapters.
Using Provider-Specific Formats (Alternative)
You can also define functions in the provider-specific format if needed:
Provider-Specific Custom Tools
Some providers support unique tools that don’t fit the standard function schema. For these cases, you can add custom tools:
See the provider-specific documentation for details on custom tools and their formats.
2. Register Function Handlers
Register handlers for your functions using the LLM service’s register_function
method:
3. Create the Pipeline
Include your LLM service in your pipeline with the registered functions:
Function Handler Details
Handler Parameters
function_name
: Name of the called functiontool_call_id
: Unique identifier for the function callargs
: Arguments passed by the LLMllm
: Reference to the LLM servicecontext
: Current conversation contextresult_callback
: Async function to return results
Return Values
- Return data through the
result_callback
- Return
None
to ignore the function call - Errors should be handled within your function
Controlling Function Call Behavior (Advanced)
When returning results from a function handler, you can control how the LLM processes those results using a FunctionCallResultProperties
frame.
It can be handy to skip a completion when you have back-to-back function calls. Note, if you skip a completion, then you must manually trigger one from the context.
Properties
Controls whether the LLM should generate a response after the function call:
True
: Run LLM after function call (default if no other function calls in progress)False
: Don’t run LLM after function callNone
: Use default behavior
Optional callback that runs after the function result is added to the context
Example Usage
Next steps
- Check out the function calling examples to see a complete example for specific LLM providers.
- Refer to your LLM providers documentation to learn more about their funciton calling capabilities.