<?php
class Person
{
public string $name;
public int $age;
}
function updateUI(Person $person) {
// add newly extracted person to the UI list
$this->ui->appendToList($person);
// remember those objects are not validated yet
}
$text = <<<TEXT
Jason is 25 years old. Jane is 18 yo. John is 30 years old
and Anna is 2 years younger than him.
TEXT;
$stream = (new StructuredOutput)
->withResponseModel(Sequence::of(Person::class))
->with(
messages: [['role' => 'user', 'content' => $text]],
options: ['stream' => true],
)
->stream();
foreach ($stream->sequence() as $sequence) {
updateUI($sequence->last()); // get last added object
}
$list = $stream->finalValue();
// now the list is fully extracted and validated
foreach ($list as $person) {
// do something with each person
$this->db->save($person);
}
// @doctest id="4196"