Parameters
LabsLLM supports different parameter types for function definitions. Parameters allow LLMs to pass structured data to your functions when they're called.
Available Parameter Types
Currently, LabsLLM supports the following parameter types:
StringParameter
The StringParameter
is used for text values and is the most commonly used parameter type:
use LabsLLM\Parameters\StringParameter;
// Basic string parameter
$nameParam = new StringParameter(
'name', // Parameter name
'A person\'s name' // Description of the parameter
);
// String parameter with enumerated options
$colorParam = new StringParameter(
'color',
'Choose a color',
['red', 'blue', 'green'] // Allowed values (enum)
);
When you define a string parameter with an enum, the model will be guided to choose only from those options, improving the reliability of function calls.
NumberParameter
The NumberParameter
is used for numeric values (integers and floats):
use LabsLLM\Parameters\NumberParameter;
// Basic number parameter
$ageParam = new NumberParameter(
'age', // Parameter name
'Age of the person' // Description of the parameter
);
BooleanParameter
The BooleanParameter
is used for boolean (true/false) values:
use LabsLLM\Parameters\BooleanParameter;
// Basic boolean parameter
$isPaidParam = new BooleanParameter(
'isPaid', // Parameter name
'If the payment was made' // Description of the parameter
);
ArrayParameter
The ArrayParameter
is used for lists of values:
use LabsLLM\Parameters\ArrayParameter;
use LabsLLM\Parameters\StringParameter;
// Array of strings
$namesParam = new ArrayParameter(
'names', // Parameter name
'Names of the people', // Description of the parameter
items: new StringParameter('name', 'Name of the person')
);
The ArrayParameter
is more complex than other types because it requires defining the type of items the array will contain. In the example above, we defined an array of strings, but you can create arrays of any other parameter type, including arrays of numbers, booleans, or even nested arrays.
ObjectParameter
The ObjectParameter
is used for complex objects with multiple properties:
use LabsLLM\Parameters\ObjectParameter;
use LabsLLM\Parameters\StringParameter;
use LabsLLM\Parameters\NumberParameter;
use LabsLLM\Parameters\ArrayParameter;
// Object parameter with multiple properties
$userParam = new ObjectParameter(
'user', // Parameter name
'User data', // Description of the parameter
[
new StringParameter('name', 'The name of the user'),
new NumberParameter('age', 'The age of the user'),
new ArrayParameter('skills', 'The skills of the user',
new StringParameter('skill', 'A skill of the user'))
],
['name', 'age'] // name and age are required fields
);
The ObjectParameter
allows you to create complex data structures by combining multiple parameter types. You can specify which properties are required by passing their names in the fourth argument.
Important: Some LLM models may have difficulty correctly respecting fields marked as required, especially in nested object structures. If you notice that the model is ignoring the required fields, consider specifying these restrictions directly in the function or parameter description to increase the likelihood of the model correctly following the expected structure.
Using Parameters with FunctionHelper
Parameters are used within FunctionHelper to define the inputs for your callable functions:
$weatherFunction = FunctionHelper::create('getWeather', 'Get weather information')
->withParameter([
new StringParameter('location', 'City name', ['New York', 'London', 'Tokyo']),
new StringParameter('format', 'Response format', ['simple', 'detailed'])
], ['location']) // Only location is required
->callable(function($location, $format = 'simple') {
// Implementation of getting weather data
return "Weather in $location is sunny. Format: $format";
});
Note that parameters are passed directly to the callable function in the same order they were defined. For optional parameters, you can provide default values in the function signature.
Required vs Optional Parameters
When defining parameters with withParameter()
, you specify which parameters are required:
->withParameter([
new StringParameter('param1', 'First parameter'),
new StringParameter('param2', 'Second parameter'),
new StringParameter('param3', 'Third parameter')
], ['param1', 'param2']) // param1 and param2 are required, param3 is optional
Required parameters must be provided by the model when calling the function, while optional parameters can be omitted. For optional parameters, it's a good practice to define default values in your callable function:
->callable(function($param1, $param2, $param3 = 'default value') {
// Function implementation
});
Future Parameter Types
More parameter types will be added in future releases.
Best Practices for Parameters
- Use descriptive names: Choose clear parameter names that indicate their purpose
- Add helpful descriptions: Provide detailed descriptions to guide the model on what to input
- Use enums when possible: Provide a list of valid options to constrain model choices
- Validate inputs: Always validate parameter values in your callable function
- Provide defaults for optional parameters: Handle cases where optional parameters are not provided using default parameter values in the function signature
For more information about using parameters with functions, see the FunctionHelper section.