Skip to content

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:

php
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:

php
$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:

  1. An array of parameter definitions
  2. An array of required parameter names

Adding Behavior

To make your function executable, add a callable that implements the function's behavior:

php
$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:

  1. The model sends the function name and arguments
  2. LabsLLM finds the matching function helper
  3. The callable is executed with the provided arguments passed directly as function parameters
  4. The return value from the callable is sent back to the model
  5. 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:

php
$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

  1. Clear descriptions: Provide clear function and parameter descriptions so the model knows when and how to use them
  2. Validate input: Always validate the arguments received from the model
  3. Return informative responses: Return responses that the model can understand and incorporate into its response
  4. Handle errors gracefully: Return clear error messages when arguments are invalid
  5. 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:

php
$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.

Released under the MIT License.