<?php
use Cognesy\Polyglot\Inference\Inference;
$inference = new Inference();
$response = $inference->with(
messages: 'Write a short story about a space explorer.',
options: ['stream' => true]
);
// Get a generator that yields partial responses
$stream = $response->stream()->responses();
echo "Story: ";
foreach ($stream as $partialResponse) {
// Output each chunk as it arrives
echo $partialResponse->contentDelta;
// Flush the output buffer to show progress in real-time (for CLI or streaming HTTP responses)
if (ob_get_level() > 0) {
ob_flush();
flush();
}
}
echo "\n";
// @doctest id="b2c0"