<?php
require 'examples/boot.php';
use Cognesy\Http\Events\HttpRequestSent;
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;
}
$provider = LLMProvider::using('openai');
$toolsRuntime = StructuredOutputRuntime::fromProvider($provider)->withOutputMode(OutputMode::Tools);
$jsonRuntime = StructuredOutputRuntime::fromProvider($provider)->withOutputMode(OutputMode::Json);
$mdJsonRuntime = StructuredOutputRuntime::fromProvider($provider)->withOutputMode(OutputMode::MdJson);
print("\n# Request for OutputMode::Tools:\n\n");
$toolsRuntime->onEvent(HttpRequestSent::class, fn(HttpRequestSent $event) => dump($event));
$user = (new StructuredOutput($toolsRuntime))
->with(
messages: "Our user Jason is 25 years old.",
responseModel: User::class,
prompt: "\nYour task is to extract correct and accurate data from the messages using provided tools.\n",
)->get();
echo "\nRESPONSE:\n";
dump($user);
assert($user->name === 'Jason');
assert($user->age === 25);
print("\n# Request for OutputMode::Json:\n\n");
$jsonRuntime->onEvent(HttpRequestSent::class, fn(HttpRequestSent $event) => dump($event));
$user = (new StructuredOutput($jsonRuntime))
->with(
messages: "Our user Jason is 25 years old.",
responseModel: User::class,
prompt: "\nYour task is to respond correctly with JSON object. Response must follow JSONSchema:\n<|json_schema|>\n",
)->get();
echo "\nRESPONSE:\n";
dump($user);
assert($user->name === 'Jason');
assert($user->age === 25);
print("\n# Request for OutputMode::MdJson:\n\n");
$mdJsonRuntime->onEvent(HttpRequestSent::class, fn(HttpRequestSent $event) => dump($event));
$user = (new StructuredOutput($mdJsonRuntime))
->with(
messages: "Our user Jason is 25 years old.",
responseModel: User::class,
prompt: "\nYour task is to respond correctly with strict JSON object containing extracted data within a ```json {} ``` codeblock. Object must validate against this JSONSchema:\n<|json_schema|>\n",
)->get();
echo "\nRESPONSE:\n";
dump($user);
assert($user->name === 'Jason');
assert($user->age === 25);
?>