<?php
use Cognesy\Polyglot\Inference\Inference;
use Cognesy\Polyglot\Inference\Enums\OutputMode;
$inference = new Inference();
// Include the expected structure in the prompt
$prompt = <<<EOT
List the top 3 most populous cities in the world.
Return your answer as a JSON object with the following structure:
{
"cities": [
{
"name": "City name",
"country": "Country name",
"population": population in millions (number)
},
...
]
}
EOT;
$response = $inference->with(
messages: $prompt,
mode: OutputMode::Json
)->asJsonData();
// Process the response
echo "Top cities by population:\n";
foreach ($response['cities'] as $index => $city) {
echo ($index + 1) . ". {$city['name']}, {$city['country']}: {$city['population']} million\n";
}
// @doctest id="0096"