|
15 | 15 | use Symfony\AI\Agent\Toolbox\AgentProcessor; |
16 | 16 | use Symfony\AI\Agent\Toolbox\Toolbox; |
17 | 17 | use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory; |
18 | | -use Symfony\AI\Platform\Bridge\OpenAi\TokenOutputProcessor; |
19 | 18 | use Symfony\AI\Platform\Message\Message; |
20 | 19 | use Symfony\AI\Platform\Message\MessageBag; |
21 | 20 | use Symfony\AI\Platform\Result\TextChunk; |
|
29 | 28 | $toolbox = new Toolbox([$clock, $openMeteo], logger: logger()); |
30 | 29 | $processor = new AgentProcessor($toolbox); |
31 | 30 |
|
32 | | -$tokenOutputProcessor = new TokenOutputProcessor(); |
33 | | - |
34 | 31 | $agent = new Agent($platform, 'gpt-4o-mini', [$processor], [$processor]); |
35 | | -$messages = new MessageBag(Message::ofUser(<<<TXT |
36 | | - Tell me the time and the weather in Dublin. |
37 | | - TXT)); |
| 32 | +$messages = new MessageBag(Message::ofUser('Tell me the time and the weather in Dublin.')); |
| 33 | + |
38 | 34 | $result = $agent->call($messages, [ |
39 | 35 | 'stream' => true, // enable streaming of response text |
40 | 36 | 'stream_options' => [ |
41 | 37 | 'include_usage' => true, // include usage in the response |
42 | 38 | ], |
43 | 39 | ]); |
44 | 40 |
|
45 | | -// Output text chunks |
46 | 41 | /** @var TextChunk $textChunk */ |
47 | 42 | foreach ($result->getContent() as $textChunk) { |
48 | | - // $textChunk implement \Stringable |
49 | 43 | echo $textChunk->getContent(); |
50 | | - |
51 | | - // Chunk also contain metadata |
52 | | - // $textChunk->getMetadata()->get('id'); // Stream id |
53 | 44 | } |
54 | 45 |
|
55 | | -// Output token usage statistics for each call |
56 | 46 | foreach ($result->getMetadata()->get('calls', []) as $call) { |
57 | 47 | echo \PHP_EOL.sprintf( |
58 | 48 | '%s: %d tokens - Finish reason: [%s]', |
|
0 commit comments