diff --git a/src/Agent.php b/src/Agent.php index f7e0ef2..a950068 100644 --- a/src/Agent.php +++ b/src/Agent.php @@ -49,6 +49,13 @@ class Agent */ protected bool $complete = false; + /** + * Indicates whether the agent should stream the response + * + * @var bool + */ + protected bool $streaming = false; + /** * The host to be used for self host LLM (e.g., GPT model) * @@ -241,6 +248,18 @@ public function complete(): self return $this; } + /** + * Enable streaming mode + * + * @return self + */ + public function withStreaming(): self + { + $this->streaming = true; + + return $this; + } + /** * Execute and get response * @@ -258,13 +277,35 @@ public function send(): mixed */ public function execute(): mixed { + $agent = $this->agentClass::create( + key: $this->key, + model: $this->model, + config: ['host' => $this->host] + )->setMessage($this->messages); + + if ($this->streaming) { + return $agent->stream($this->params); + } + + return $agent->execute($this->params, $this->complete); + } + + /** + * Stream the agent response + * + * @return \Generator + */ + public function stream(): \Generator + { + $this->streaming = true; + return $this->agentClass::create( key: $this->key, model: $this->model, config: ['host' => $this->host] ) ->setMessage($this->messages) - ->execute($this->params, $this->complete); + ->stream($this->params); } /** diff --git a/src/AgentFactory/Agent/Claude.php b/src/AgentFactory/Agent/Claude.php index bd5645d..69fe208 100644 --- a/src/AgentFactory/Agent/Claude.php +++ b/src/AgentFactory/Agent/Claude.php @@ -80,6 +80,42 @@ public function execute(array $params, bool $complete = false, ?string $textInpu return $complete ? $result : $result->asText(); } + /** + * Streams the model response in real-time. + * + * This method enables streaming mode and yields text chunks as they arrive + * from the LLM, allowing for real-time display of the response. + * + * @param array $params Additional parameters (temperature, max_tokens, etc.) + * @param ?string $textInput Optional text input to override messages + * @return \Generator Yields text chunks as strings + * @throws \Exception If streaming fails or platform error occurs + */ + public function stream(array $params, ?string $textInput = null): \Generator + { + $params['stream'] = true; + + try { + $result = $this->platform->invoke( + $this->model, + $textInput ?? $this->messages, + $params + ); + + foreach ($result->asStream() as $chunk) { + if (!empty($chunk)) { + yield $chunk; + } + } + } catch (\Throwable $e) { + throw new \Exception( + "Streaming failed for model {$this->model}: " . $e->getMessage(), + $e->getCode(), + $e + ); + } + } + /** * Converts raw message arrays into a MessageBag. * diff --git a/src/AgentFactory/Agent/Gemini.php b/src/AgentFactory/Agent/Gemini.php index 6d08673..7c7f0e0 100644 --- a/src/AgentFactory/Agent/Gemini.php +++ b/src/AgentFactory/Agent/Gemini.php @@ -80,6 +80,42 @@ public function execute(array $params, bool $complete = false, ?string $textInpu return $complete ? $result : $result->asText(); } + /** + * Streams the model response in real-time. + * + * This method enables streaming mode and yields text chunks as they arrive + * from the LLM, allowing for real-time display of the response. + * + * @param array $params Additional parameters (temperature, max_tokens, etc.) + * @param ?string $textInput Optional text input to override messages + * @return \Generator Yields text chunks as strings + * @throws \Exception If streaming fails or platform error occurs + */ + public function stream(array $params, ?string $textInput = null): \Generator + { + $params['stream'] = true; + + try { + $result = $this->platform->invoke( + $this->model, + $this->messages, + $params + ); + + foreach ($result->asStream() as $chunk) { + if (!empty($chunk)) { + yield $chunk; + } + } + } catch (\Throwable $e) { + throw new \Exception( + "Streaming failed for model {$this->model}: " . $e->getMessage(), + $e->getCode(), + $e + ); + } + } + /** * Converts raw message arrays into a MessageBag. * diff --git a/src/AgentFactory/Agent/OpenAI.php b/src/AgentFactory/Agent/OpenAI.php index 828ab99..19be98f 100644 --- a/src/AgentFactory/Agent/OpenAI.php +++ b/src/AgentFactory/Agent/OpenAI.php @@ -80,6 +80,42 @@ public function execute(array $params, bool $complete = false, ?string $textInpu return $complete ? $result : $result->asText(); } + /** + * Streams the model response in real-time. + * + * This method enables streaming mode and yields text chunks as they arrive + * from the LLM, allowing for real-time display of the response. + * + * @param array $params Additional parameters (temperature, max_tokens, etc.) + * @param ?string $textInput Optional text input to override messages + * @return \Generator Yields text chunks as strings + * @throws \Exception If streaming fails or platform error occurs + */ + public function stream(array $params, ?string $textInput = null): \Generator + { + $params['stream'] = true; + + try { + $result = $this->platform->invoke( + $this->model, + $textInput ?? $this->messages, + $params + ); + + foreach ($result->asStream() as $chunk) { + if (!empty($chunk)) { + yield $chunk; + } + } + } catch (\Throwable $e) { + throw new \Exception( + "Streaming failed for model {$this->model}: " . $e->getMessage(), + $e->getCode(), + $e + ); + } + } + /** * Converts raw message arrays into a MessageBag. * diff --git a/src/AgentFactory/Agent/OpenRouter.php b/src/AgentFactory/Agent/OpenRouter.php index 6e6e8af..3dbc80f 100644 --- a/src/AgentFactory/Agent/OpenRouter.php +++ b/src/AgentFactory/Agent/OpenRouter.php @@ -80,6 +80,42 @@ public function execute(array $params, bool $complete = false, ?string $textInpu return $complete ? $result : $result->asText(); } + /** + * Streams the model response in real-time. + * + * This method enables streaming mode and yields text chunks as they arrive + * from the LLM, allowing for real-time display of the response. + * + * @param array $params Additional parameters (temperature, max_tokens, etc.) + * @param ?string $textInput Optional text input to override messages + * @return \Generator Yields text chunks as strings + * @throws \Exception If streaming fails or platform error occurs + */ + public function stream(array $params, ?string $textInput = null): \Generator + { + $params['stream'] = true; + + try { + $result = $this->platform->invoke( + $this->model, + $textInput ?? $this->messages, + $params + ); + + foreach ($result->asStream() as $chunk) { + if (!empty($chunk)) { + yield $chunk; + } + } + } catch (\Throwable $e) { + throw new \Exception( + "Streaming failed for model {$this->model}: " . $e->getMessage(), + $e->getCode(), + $e + ); + } + } + /** * Converts raw message arrays into a MessageBag. * diff --git a/src/AgentFactory/Agent/SelfHost.php b/src/AgentFactory/Agent/SelfHost.php index 3bb98f5..8c5e4cf 100644 --- a/src/AgentFactory/Agent/SelfHost.php +++ b/src/AgentFactory/Agent/SelfHost.php @@ -105,6 +105,42 @@ public function execute(array $params, bool $complete = false, ?string $textInpu return $complete ? $result : $result->asText(); } + /** + * Streams the model response in real-time. + * + * This method enables streaming mode and yields text chunks as they arrive + * from the LLM, allowing for real-time display of the response. + * + * @param array $params Additional parameters (temperature, max_tokens, etc.) + * @param ?string $textInput Optional text input to override messages + * @return \Generator Yields text chunks as strings + * @throws \Exception If streaming fails or platform error occurs + */ + public function stream(array $params, ?string $textInput = null): \Generator + { + $params['stream'] = true; + + try { + $result = $this->platform->invoke( + $this->model, + $this->messages, + $params + ); + + foreach ($result->asStream() as $chunk) { + if (!empty($chunk)) { + yield $chunk; + } + } + } catch (\Throwable $e) { + throw new \Exception( + "Streaming failed for model {$this->model}: " . $e->getMessage(), + $e->getCode(), + $e + ); + } + } + /** * Converts raw message arrays into a MessageBag. * diff --git a/src/AgentFactory/AgentInterface.php b/src/AgentFactory/AgentInterface.php index 3cee40b..3734f9a 100644 --- a/src/AgentFactory/AgentInterface.php +++ b/src/AgentFactory/AgentInterface.php @@ -34,6 +34,15 @@ public function setMessage(array $messages): mixed; */ public function execute(array $params, bool $complete = false, ?string $textInput = null): mixed; + /** + * Streams the agent's response in real-time using provided parameters. + * + * @param array $params + * @param ?string $textInput + * @return \Generator + */ + public function stream(array $params, ?string $textInput = null): \Generator; + /** * Converts raw message data into a MessageBag instance. *