Defining Tools
Tools are defined as arrays following the OpenAI function calling format. Each tool describes a function’s name, purpose, and expected parameters:Making a Tool Call Request
Pass your tool definitions via thetools parameter and control how the model selects tools with toolChoice:
Processing Tool Call Results
The response object provides methods for inspecting whether the model made tool calls and extracting their details:Convenience Accessors
For simple cases where you just need the tool call arguments as data, Polyglot provides shortcut methods:asToolCallJsonData() returns that call’s arguments as an array. When multiple tool calls are returned, it returns an array of all calls.
Controlling Tool Selection
ThetoolChoice parameter controls how the model decides whether to use tools:
Multiple Tools
You can provide multiple tool definitions in a single request. The model will select the most appropriate one based on the user’s message:Using the Fluent API
The fluent builder methodswithTools() and withToolChoice() offer an alternative to passing everything through with():
Provider Support
Tool calling support varies across providers:
You can query tool support programmatically through
DriverCapabilities::supportsToolCalling() and DriverCapabilities::supportsToolChoice().
When to Use Tool Calling
Tool calling is ideal for:- Building agents that interact with external APIs and services
- Creating assistants that retrieve real-time information
- Implementing multi-step workflows where the model orchestrates actions
- Extracting structured data using function schemas (an alternative to JSON Schema mode)
- Giving the model access to specific capabilities like calculations, database queries, or file operations