FunctionHelper
The FunctionHelper
class is used to define functions that can be called by LLMs during execution. It provides a fluent interface for creating function definitions with various parameters and behaviors.
Creating Functions
You can create functions using the static create
method:
use LabsLLM\Helpers\FunctionHelper;
// Create a simple function
$simpleFunction = FunctionHelper::create(
'functionName', // The name of the function
'Description of function' // Description that helps the model understand when to use it
);
Defining Parameters
Functions can have parameters that the model will provide values for:
$withParams = FunctionHelper::create('weatherForecast', 'Get weather forecast for a location')
->withParameter([
new StringParameter('location', 'The city and state, e.g. San Francisco, CA'),
new StringParameter('unit', 'The temperature unit', ['celsius', 'fahrenheit'])
], ['location']); // 'location' is required, 'unit' is optional
The withParameter
method takes two arguments:
- An array of parameter definitions
- An array of required parameter names
Adding Behavior
To make your function executable, add a callable that implements the function's behavior:
$function = FunctionHelper::create('multiply', 'Multiply two numbers')
->withParameter([
new StringParameter('num1', 'First number'),
new StringParameter('num2', 'Second number')
], ['num1', 'num2'])
->callable(function($num1, $num2) {
$num1 = floatval($num1);
$num2 = floatval($num2);
return "The result is: " . ($num1 * $num2);
});
The callable function receives the parameter values directly as individual function arguments in the same order they were defined in withParameter()
.
Function Execution Process
When the LLM decides to call a function, the following happens:
- The model sends the function name and arguments
- LabsLLM finds the matching function helper
- The callable is executed with the provided arguments passed directly as function parameters
- The return value from the callable is sent back to the model
- If
maxSteps
> 1, the model can use this result to generate a response or call another function
Using Functions with LabsLLM
To make your function available to the model:
$execute = LabsLLM::text()
->addTool($function) // Add the function helper
->withMaxSteps(2) // Allow function call + response
->using(new OpenAI('key', 'model'))
->executePrompt('Calculate 25 multiplied by 4');
$response = $execute->getResponseData();
Best Practices
- Clear descriptions: Provide clear function and parameter descriptions so the model knows when and how to use them
- Validate input: Always validate the arguments received from the model
- Return informative responses: Return responses that the model can understand and incorporate into its response
- Handle errors gracefully: Return clear error messages when arguments are invalid
- Keep functions focused: Create functions that do one thing well rather than complex multi-purpose functions
Function Formatting
Internally, FunctionHelper converts your function definition to the format expected by the LLM provider:
$functionAsArray = $function->toArray();
// Result structure:
// [
// 'type' => 'function',
// 'function' => [
// 'name' => 'functionName',
// 'description' => 'Description of function',
// 'parameters' => [
// 'type' => 'object',
// 'properties' => [ ... ],
// 'required' => [ ... ]
// ]
// ]
// ]
For more information about parameter types, see the Parameters section.