<?php
require 'examples/boot.php';
use Cognesy\Instructor\Extras\Example\Example;
use Cognesy\Instructor\StructuredOutput;
use Cognesy\Instructor\StructuredOutputRuntime;
use Cognesy\Instructor\Enums\OutputMode;
use Cognesy\Polyglot\Inference\LLMProvider;
class User {
public int $age;
public string $name;
}
$examples = [
new Example(
input: "John is 50 and works as a teacher.",
output: ['name' => 'John', 'age' => 50]
),
new Example(
input: "We have recently hired Ian, who is 27 years old.",
output: ['name' => 'Ian', 'age' => 27],
template: "example input:\n<|input|>\noutput:\n```json\n<|output|>\n```\n",
),
];
$input = 'Our user Jason is 25 years old.';
echo "\nFEW-SHOT EXAMPLES PROVIDED TO THE MODEL:\n";
foreach ($examples as $number => $example) {
$output = json_encode($example->output(), JSON_UNESCAPED_SLASHES);
echo sprintf(" %d. %s\n => %s\n", $number + 1, $example->inputString(), $output);
}
echo "\nNEW INPUT:\n {$input}\n";
$runtime = StructuredOutputRuntime::fromProvider(LLMProvider::using('openai'))
->withOutputMode(OutputMode::Json);
$user = (new StructuredOutput($runtime))
->withMessages($input)
->withResponseClass(User::class)
->withExamples($examples)
->get();
echo "\nSTRUCTURED RESULT FOR THE NEW INPUT:\n";
dump($user);
assert($user->name === 'Jason');
assert($user->age === 25);
?>