|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Symfony\AI\Agent\Agent; |
| 13 | +use Symfony\AI\Agent\Toolbox\AgentProcessor; |
| 14 | +use Symfony\AI\Agent\Toolbox\Tool\Clock; |
| 15 | +use Symfony\AI\Agent\Toolbox\Toolbox; |
| 16 | +use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; |
| 17 | +use Symfony\AI\Platform\Bridge\OpenAi\TokenOutputProcessor; |
| 18 | +use Symfony\AI\Platform\Message\Message; |
| 19 | +use Symfony\AI\Platform\Message\MessageBag; |
| 20 | +use Symfony\AI\Platform\Result\TextChunk; |
| 21 | + |
| 22 | +require_once dirname(__DIR__).'/bootstrap.php'; |
| 23 | + |
| 24 | +$platform = PlatformFactory::create(env('OPENAI_API_KEY'), http_client()); |
| 25 | + |
| 26 | +$clock = new Clock(); |
| 27 | +$openMeteo = new Symfony\AI\Agent\Toolbox\Tool\OpenMeteo(http_client()); |
| 28 | +$toolbox = new Toolbox([$clock, $openMeteo], logger: logger()); |
| 29 | +$processor = new AgentProcessor($toolbox); |
| 30 | + |
| 31 | +$tokenOutputProcessor = new TokenOutputProcessor(); |
| 32 | + |
| 33 | +$agent = new Agent($platform, 'gpt-4o-mini', [$processor], [$processor]); |
| 34 | +$messages = new MessageBag(Message::ofUser(<<<TXT |
| 35 | + Tell me the time and the weather in Dublin. |
| 36 | + TXT)); |
| 37 | +$result = $agent->call($messages, [ |
| 38 | + 'stream' => true, // enable streaming of response text |
| 39 | + 'stream_options' => [ |
| 40 | + 'include_usage' => true, // include usage in the response |
| 41 | + ], |
| 42 | +]); |
| 43 | + |
| 44 | +// Output text chunks |
| 45 | +/** @var TextChunk $textChunk */ |
| 46 | +foreach ($result->getContent() as $textChunk) { |
| 47 | + |
| 48 | + // $textChunk implement \Stringable |
| 49 | + echo $textChunk->getContent(); |
| 50 | + |
| 51 | + // Chunk also contain metadata |
| 52 | + // $textChunk->getMetadata()->get('id'); // Stream id |
| 53 | +} |
| 54 | + |
| 55 | +// Output token usage statistics for each call |
| 56 | +foreach ($result->getMetadata()->get('calls', []) as $call) { |
| 57 | + echo \PHP_EOL.sprintf( |
| 58 | + '%s: %d tokens - Finish reason: [%s]', |
| 59 | + $call['id'], |
| 60 | + $call['usage']['total_tokens'], |
| 61 | + $call['finish_reason'] |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +echo \PHP_EOL; |
0 commit comments