<?php
require 'examples/boot.php';
use Cognesy\Http\Events\HttpRequestSent;
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;
}
echo "\nREQUEST:\n";
$runtime = StructuredOutputRuntime::fromProvider(LLMProvider::using('openai'))
->withOutputMode(OutputMode::Json)
->onEvent(HttpRequestSent::class, fn($event) => dump($event));
$user = (new StructuredOutput($runtime))
->withMessages("Our user Jason is 25 years old.")
->withResponseClass(User::class)
->withExamples([
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",
),
])
->get();
echo "\nOUTPUT:\n";
dump($user);
assert($user->name === 'Jason');
assert($user->age === 25);
?>