JsonSchema
class. It is useful when you want to shape the structures during
runtime.
<?php
require 'examples/boot.php';
use Cognesy\Polyglot\Inference\Enums\OutputMode;
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Utils\JsonSchema\JsonSchema;
$schema = JsonSchema::object(
properties: [
JsonSchema::string('name', 'City name'),
JsonSchema::integer('population', 'City population'),
JsonSchema::integer('founded', 'Founding year'),
],
requiredProperties: ['name', 'population', 'founded'],
);
$data = (new Inference)
->using('openai')
->with(
messages: [
['role' => 'user', 'content' => 'What is capital of France? Respond with JSON data.']
],
responseFormat: $schema->toResponseFormat(
schemaName: 'city_data',
schemaDescription: 'City data',
strict: true,
),
options: ['max_tokens' => 64],
mode: OutputMode::JsonSchema,
)
->asJsonData();
echo "USER: What is capital of France\n";
echo "ASSISTANT:\n";
dump($data);
assert(is_array($data));
assert(is_string($data['name']));
assert(is_int($data['population']));
assert(is_int($data['founded']));
?>