A02_Advanced
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
<?php
require 'examples/boot.php';
use Cognesy\Instructor\Extras\Scalar\Scalar;
use Cognesy\Instructor\StructuredOutput;
enum CitizenshipGroup : string {
case US = "US";
case Canada = "Canada";
case Germany = "Germany";
case Other = "Other";
}
$text = "His name is Jason, he is 28 years old American who lives in Germany.";
$question = "What is user's citizenship?";
echo "\nSOURCE TEXT:\n {$text}\n";
echo "\nQUESTION:\n {$question}\n";
echo "\nTARGET SCALAR:\n CitizenshipGroup enum\n";
$value = StructuredOutput::using('openai')->with(
messages: $text,
prompt: $question,
responseModel: Scalar::enum(CitizenshipGroup::class, name: 'citizenshipGroup'),
)->get();
echo "\nRETURNED SCALAR:\n CitizenshipGroup::{$value->name} ({$value->value})\n";
assert($value instanceof CitizenshipGroup);
assert($value == CitizenshipGroup::US);
?>