Tools
LabsLLM allows models to call functions in your PHP code, enabling powerful integrations between AI and your application's business logic. These tools can perform actions, retrieve data, or interact with other systems.
Using Tools/Functions
You can define tools for the model to use in several ways, from simple functions without parameters to complex ones with various parameter types.
Simple Function Without Parameters
The simplest way to define a function is without any parameters:
// Define a function with no parameters
$dateFunction = FunctionHelper::create('getDate', 'Get the current date')
->callable(function() {
return 'Today is ' . date('Y-m-d');
});
// Execute with the tool available
$execute = LabsLLM::text()
->addTool($dateFunction)
->withMaxSteps(1)
->using(new OpenAI('your-api-key', 'gpt-4o'))
->executePrompt('What is today\'s date?');
$response = $execute->getResponseData();
echo "Text response: " . $response->response . PHP_EOL;
Function With Parameters
For more complex use cases, you can define functions with parameters:
// Define a function with parameters
$dateTimeFunction = FunctionHelper::create('getDateOrTime', 'Get data or time from the day')
->withParameter([
new StringParameter('type', 'The type of data to get')
], ['type']) // Second array defines required parameters
->callable(function($type) {
if ($type === 'date') {
return 'Today\'s date is ' . date('Y-m-d');
} else if ($type === 'time') {
return 'Current time is ' . date('H:i:s');
}
return 'Invalid type requested';
});
// Execute with the tool available
$execute = LabsLLM::text()
->addTool($dateTimeFunction)
->withMaxSteps(2)
->using(new OpenAI('your-api-key', 'gpt-4o'))
->executePrompt('What time is it right now?');
$response = $execute->getResponseData();
// Access the complete response data
echo "Text response: " . $response->response . PHP_EOL;
echo "Tools called: " . count($response->called_tools) . PHP_EOL;
// You can inspect which tools were called and their responses
foreach ($response->called_tools as $tool) {
echo "Called: " . $tool['name'] . " with argument type: " . $tool['arguments']['type'] . PHP_EOL;
echo "Tool response: " . $tool['response']['response'] . PHP_EOL;
}
Examining Tool Execution
When a tool is called, you can access information about the call and its results through the response object:
$response = $execute->getResponseData();
// Check if any tools were called
if (!empty($response->called_tools)) {
foreach ($response->called_tools as $tool) {
echo "Tool Name: " . $tool['name'] . PHP_EOL;
echo "Arguments: " . json_encode($tool['arguments']) . PHP_EOL;
echo "Response: " . $tool['response']['response'] . PHP_EOL;
}
}
Understanding maxSteps
The maxSteps
parameter controls how many back-and-forth interactions can occur with tools:
// Allow up to 3 tool calls in sequence
$execute = LabsLLM::text()
->addTool($myFunction)
->withMaxSteps(3)
->using(new OpenAI('your-api-key', 'gpt-4o'))
->executePrompt('Execute the task.');
By default, maxSteps
is set to 1, which means:
- The model will generate a response or call a tool
- If a tool is called, it will be executed
- The execution stops after the tool responds
When maxSteps
is set to 1 (default):
- If the model calls a tool,
$response->response
will be empty $response->called_tools
will contain the tool that was executed and its response- The response from the tool will not be sent back to the model
When maxSteps
is greater than 1:
- After a tool is executed, its result is sent back to the model
- The model can then generate a text response based on the tool result
- Or it can call another tool (chained tool calls)
- This continues until
maxSteps
is reached or the model provides a text response
For more information about creating tools with parameters, see the FunctionHelper and Parameters sections.