Structures
Handling dynamic data models
If you want to define the shape of data during runtime, you can use 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.
Defining a shape of data
Use 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.
Following types of fields are currently supported:
Field::bool()
- boolean valueField::int()
- int valueField::string()
- string valueField::float()
- float valueField::enum()
- enum valueField::structure()
- for nesting structures
Optional fields
Fields can be marked as optional with $field->optional()
. By default, all
defined fields are required.
Descriptions for guiding LLM inference
Instructor includes field descriptions in the content of instructions for LLM, so you can use them to provide explanations, detailed specifications or requirements for each field.
You can also provide extra inference instructions for LLM at the structure level with $structure->description(string $description)
Nesting structures
You can use Field::structure()
to nest structures in case you want to define
more complex data shapes.
Validation of structure data
Instructor supports validation of structures.
You can define field validator with:
$field->validator(callable $validator)
- $validator has to return an instance ofValidationResult
$field->validIf(callable $condition, string $message)
- message with be provided to LLM as explanation for self-correction of the next extraction attempt
Let’s add a simple field validation to the example above:
Extracting data
Now, let’s extract the data from the message.
Working with Structure
objects
Structure object properties can be accessed using get()
and set()
methods,
but also directly as properties.