From 52a10d772e2bcbc29d5a312508c8593997683f01 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 26 Sep 2025 08:59:34 +0200 Subject: [PATCH] Refactor examples to use print_stream helper function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- examples/anthropic/stream.php | 5 +---- examples/bootstrap.php | 8 ++++++++ examples/cerebras/stream.php | 5 +---- examples/gemini/stream.php | 5 +---- examples/mistral/stream.php | 5 +---- examples/ollama/stream.php | 5 +---- examples/openai/stream.php | 5 +---- examples/vertexai/stream.php | 6 +----- 8 files changed, 15 insertions(+), 29 deletions(-) diff --git a/examples/anthropic/stream.php b/examples/anthropic/stream.php index 5046d6e58..8f7a89e73 100644 --- a/examples/anthropic/stream.php +++ b/examples/anthropic/stream.php @@ -25,7 +25,4 @@ ); $result = $platform->invoke($model, $messages, ['stream' => true]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} -echo \PHP_EOL; +print_stream($result); diff --git a/examples/bootstrap.php b/examples/bootstrap.php index 0966390ed..9f6ab5ba1 100644 --- a/examples/bootstrap.php +++ b/examples/bootstrap.php @@ -130,3 +130,11 @@ function perplexity_print_citations(Metadata $metadata): void echo \PHP_EOL; } } + +function print_stream(ResultPromise $result): void +{ + foreach ($result->getResult()->getContent() as $word) { + echo $word; + } + echo \PHP_EOL; +} diff --git a/examples/cerebras/stream.php b/examples/cerebras/stream.php index b1f254fd9..87aebeadd 100644 --- a/examples/cerebras/stream.php +++ b/examples/cerebras/stream.php @@ -27,7 +27,4 @@ 'stream' => true, ]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} -echo \PHP_EOL; +print_stream($result); diff --git a/examples/gemini/stream.php b/examples/gemini/stream.php index 589f8d4e4..6f7633223 100644 --- a/examples/gemini/stream.php +++ b/examples/gemini/stream.php @@ -27,7 +27,4 @@ 'stream' => true, // enable streaming of response text ]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} -echo \PHP_EOL; +print_stream($result); diff --git a/examples/mistral/stream.php b/examples/mistral/stream.php index a6a5473e8..a2c64ecee 100644 --- a/examples/mistral/stream.php +++ b/examples/mistral/stream.php @@ -24,7 +24,4 @@ 'stream' => true, ]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} -echo \PHP_EOL; +print_stream($result); diff --git a/examples/ollama/stream.php b/examples/ollama/stream.php index a3c162ac9..d12a67a0d 100644 --- a/examples/ollama/stream.php +++ b/examples/ollama/stream.php @@ -26,7 +26,4 @@ $result = $platform->invoke($model, $messages, ['stream' => true]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} -echo \PHP_EOL; +print_stream($result); diff --git a/examples/openai/stream.php b/examples/openai/stream.php index e9cea8e6f..bb36cb69d 100644 --- a/examples/openai/stream.php +++ b/examples/openai/stream.php @@ -27,7 +27,4 @@ 'stream' => true, // enable streaming of response text ]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} -echo \PHP_EOL; +print_stream($result); diff --git a/examples/vertexai/stream.php b/examples/vertexai/stream.php index 0d38feb30..16bda55d3 100644 --- a/examples/vertexai/stream.php +++ b/examples/vertexai/stream.php @@ -28,8 +28,4 @@ 'stream' => true, ]); -foreach ($result->getResult()->getContent() as $word) { - echo $word; -} - -echo \PHP_EOL; +print_stream($result);