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);