Skip to content

Commit

Permalink
Add streaming content generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkose committed Jan 18, 2024
1 parent fc17c03 commit 54036d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
23 changes: 23 additions & 0 deletions src/Gemini.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GeminiAPI\Laravel;

use CurlHandle;
use GeminiAPI\ClientInterface;
use GeminiAPI\Enums\MimeType;
use GeminiAPI\Enums\ModelName;
Expand All @@ -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;
Expand Down Expand Up @@ -64,6 +66,27 @@ public function generateText(string $prompt): string
return $response->text();
}

/**
* @param string $prompt
* @param callable(string): void $callback
* @param CurlHandle|null $ch
* @return void
*/
public function generateTextStream(
string $prompt,
callable $callback,
?CurlHandle $ch = null,
): void {
$cb = fn(GenerateContentResponse $response) => $callback($response->text());
$this->client
->generativeModel(ModelName::GeminiPro)
->generateContentStream(
$cb,
[new TextPart($prompt)],
$ch,
);
}

/**
* Generates a text based on the given image file.
* You can also provide a prompt.
Expand Down

0 comments on commit 54036d0

Please sign in to comment.