Skip to content

Commit 33a8c0a

Browse files
committed
Clean example code.
- Remove irrelevant comments. - Enhance readability. - Eliminate unused instantiations.
1 parent 4462ee8 commit 33a8c0a

File tree

1 file changed

+2
-12
lines changed

1 file changed

+2
-12
lines changed

examples/openai/toolcall-stream-with-usage.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\AI\Agent\Toolbox\AgentProcessor;
1616
use Symfony\AI\Agent\Toolbox\Toolbox;
1717
use Symfony\AI\Platform\Bridge\OpenAi\PlatformFactory;
18-
use Symfony\AI\Platform\Bridge\OpenAi\TokenOutputProcessor;
1918
use Symfony\AI\Platform\Message\Message;
2019
use Symfony\AI\Platform\Message\MessageBag;
2120
use Symfony\AI\Platform\Result\TextChunk;
@@ -29,30 +28,21 @@
2928
$toolbox = new Toolbox([$clock, $openMeteo], logger: logger());
3029
$processor = new AgentProcessor($toolbox);
3130

32-
$tokenOutputProcessor = new TokenOutputProcessor();
33-
3431
$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+
3834
$result = $agent->call($messages, [
3935
'stream' => true, // enable streaming of response text
4036
'stream_options' => [
4137
'include_usage' => true, // include usage in the response
4238
],
4339
]);
4440

45-
// Output text chunks
4641
/** @var TextChunk $textChunk */
4742
foreach ($result->getContent() as $textChunk) {
48-
// $textChunk implement \Stringable
4943
echo $textChunk->getContent();
50-
51-
// Chunk also contain metadata
52-
// $textChunk->getMetadata()->get('id'); // Stream id
5344
}
5445

55-
// Output token usage statistics for each call
5646
foreach ($result->getMetadata()->get('calls', []) as $call) {
5747
echo \PHP_EOL.sprintf(
5848
'%s: %d tokens - Finish reason: [%s]',

0 commit comments

Comments
 (0)