Files
archived-ai/examples/ollama/stream-with-token-usage.php
2026-03-17 10:11:34 +01:00

33 lines
950 B
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
require_once dirname(__DIR__).'/bootstrap.php';
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), env('OLLAMA_API_KEY'), httpClient: http_client());
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant.'),
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
);
$result = $platform->invoke(env('OLLAMA_LLM'), $messages, ['stream' => true]);
foreach ($result->asStream() as $word) {
echo $word;
}
echo \PHP_EOL;
print_token_usage($result->getMetadata()->get('token_usage'));