Structure
class.
Structures allow you to define and modify arbitrary shape of data to be extracted by
LLM. Classes may not be the best fit for this purpose, as declaring or changing them
during execution is not possible.
With structures, you can define custom data shapes dynamically, for example based
on the user input or context of the processing, to specify the information you need
LLM to infer from the provided text or chat messages.
Structure::define()
to define the structure and pass it to Instructor
as response model.
If Structure
instance has been provided as a response model, Instructor
returns an array in the shape you defined.
Structure::define()
accepts array of Field
objects.
Let’s first define the structure, which is a shape of the data we want to
extract from the message.
Field::bool()
- boolean valueField::int()
- int valueField::string()
- string valueField::float()
- float valueField::enum()
- enum valueField::structure()
- for nesting structures$field->optional()
. By default, all
defined fields are required.
$structure->description(string $description)
Field::structure()
to nest structures in case you want to define
more complex data shapes.
$field->validator(callable $validator)
- $validator has to return an instance of ValidationResult
$field->validIf(callable $condition, string $message)
- message with be provided to LLM as explanation for self-correction of the next extraction attemptStructure
objectsget()
and set()
methods,
but also directly as properties.