Skip to content

Commit 52a10d7

Browse files
OskarStarkclaude
andcommitted
Refactor examples to use print_stream helper function
Extract repeated streaming pattern into reusable print_stream function in examples/bootstrap.php and update all streaming examples to use it. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 30c655a commit 52a10d7

File tree

8 files changed

+15
-29
lines changed

8 files changed

+15
-29
lines changed

examples/anthropic/stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,4 @@
2525
);
2626
$result = $platform->invoke($model, $messages, ['stream' => true]);
2727

28-
foreach ($result->getResult()->getContent() as $word) {
29-
echo $word;
30-
}
31-
echo \PHP_EOL;
28+
print_stream($result);

examples/bootstrap.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,11 @@ function perplexity_print_citations(Metadata $metadata): void
130130
echo \PHP_EOL;
131131
}
132132
}
133+
134+
function print_stream(ResultPromise $result): void
135+
{
136+
foreach ($result->getResult()->getContent() as $word) {
137+
echo $word;
138+
}
139+
echo \PHP_EOL;
140+
}

examples/cerebras/stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@
2727
'stream' => true,
2828
]);
2929

30-
foreach ($result->getResult()->getContent() as $word) {
31-
echo $word;
32-
}
33-
echo \PHP_EOL;
30+
print_stream($result);

examples/gemini/stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@
2727
'stream' => true, // enable streaming of response text
2828
]);
2929

30-
foreach ($result->getResult()->getContent() as $word) {
31-
echo $word;
32-
}
33-
echo \PHP_EOL;
30+
print_stream($result);

examples/mistral/stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,4 @@
2424
'stream' => true,
2525
]);
2626

27-
foreach ($result->getResult()->getContent() as $word) {
28-
echo $word;
29-
}
30-
echo \PHP_EOL;
27+
print_stream($result);

examples/ollama/stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@
2626

2727
$result = $platform->invoke($model, $messages, ['stream' => true]);
2828

29-
foreach ($result->getResult()->getContent() as $word) {
30-
echo $word;
31-
}
32-
echo \PHP_EOL;
29+
print_stream($result);

examples/openai/stream.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,4 @@
2727
'stream' => true, // enable streaming of response text
2828
]);
2929

30-
foreach ($result->getResult()->getContent() as $word) {
31-
echo $word;
32-
}
33-
echo \PHP_EOL;
30+
print_stream($result);

examples/vertexai/stream.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,4 @@
2828
'stream' => true,
2929
]);
3030

31-
foreach ($result->getResult()->getContent() as $word) {
32-
echo $word;
33-
}
34-
35-
echo \PHP_EOL;
31+
print_stream($result);

0 commit comments

Comments
 (0)