diff --git a/composer.json b/composer.json index bb5c4b9..f5fb9cd 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ ], "require": { "php": "^8.1", - "gemini-api-php/client": "^1.3.1", + "gemini-api-php/client": "^1.4.3", "illuminate/support": "^9.0 || ^10.0 || ^11.0", "psr/container": "^1.0 || ^2.0", "psr/http-client": "^1.0" @@ -37,6 +37,9 @@ "phpstan/phpstan": "^1.10.47", "symfony/var-dumper": "^6.4.0|^7.0.1" }, + "suggest": { + "ext-curl": "Required for streaming responses" + }, "autoload": { "psr-4": { "GeminiAPI\\Laravel\\": "src" diff --git a/src/Gemini.php b/src/Gemini.php index 1747458..51861b2 100644 --- a/src/Gemini.php +++ b/src/Gemini.php @@ -4,6 +4,7 @@ namespace GeminiAPI\Laravel; +use CurlHandle; use GeminiAPI\ClientInterface; use GeminiAPI\Enums\MimeType; use GeminiAPI\Enums\ModelName; @@ -15,6 +16,7 @@ use GeminiAPI\Resources\Model; use GeminiAPI\Resources\Parts\ImagePart; use GeminiAPI\Resources\Parts\TextPart; +use GeminiAPI\Responses\GenerateContentResponse; use Psr\Http\Client\ClientExceptionInterface; use function array_map; @@ -64,6 +66,23 @@ public function generateText(string $prompt): string return $response->text(); } + /** + * @param callable(string): void $callback + */ + public function generateTextStream( + string $prompt, + callable $callback, + ?CurlHandle $ch = null, + ): void { + $this->client + ->generativeModel(ModelName::GeminiPro) + ->generateContentStream( + fn (GenerateContentResponse $response) => $callback($response->text()), + [new TextPart($prompt)], + $ch, + ); + } + /** * Generates a text based on the given image file. * You can also provide a prompt.