When dealing with lists of attributes, especially arbitrary properties, it’s crucial to manage
the length of list. You can use prompting and enumeration to limit the list length, ensuring
a manageable set of properties.
<?php
$loader = require 'vendor/autoload.php';
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/');
use Cognesy\Instructor\Features\Validation\Traits\ValidationMixin;
use Cognesy\Instructor\Features\Validation\ValidationResult;
use Cognesy\Instructor\Instructor;
class Property
{
public string $index;
public string $key;
public string $value;
}
class UserDetail
{
use ValidationMixin;
public int $age;
public string $name;
public array $properties;
public function validate() : ValidationResult
{
if (count($this->properties) < 3) {
return ValidationResult::valid();
}
return ValidationResult::fieldError(
field: 'properties',
value: $this->name,
message: "Number of properties must be not more than 2.",
);
}
}
$text = <<<TEXT
Jason is 25 years old. He is a programmer. He has a car. He lives in
a small house in Alamo. He likes to play guitar.
TEXT;
$user = (new Instructor)->respond(
messages: [['role' => 'user', 'content' => $text]],
responseModel: UserDetail::class,
maxRetries: 1
);
dump($user);
assert($user->age === 25);
assert($user->name === "Jason");
assert(count($user->properties) < 3);
?>