Skip to content

Commit

Permalink
Add streaming chat session
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemkose committed Jan 5, 2024
1 parent 569db52 commit d56e712
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ _This library is not developed or endorsed by Google._
- [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)
- [Chat Session with history](#chat-session-with-history)
- [Streaming responses](#streaming-responses)
- [Streaming Chat Session](#streaming-chat-session)
- [Tokens counting](#tokens-counting)
- [Listing models](#listing-models)

Expand Down Expand Up @@ -153,6 +154,8 @@ This code will print "Hello World!" to the standard output.

### Streaming responses

> Requires `curl` extension to be enabled
In the streaming response, the callback function will be called whenever a response is returned from the server.

Long responses may be broken into separate responses, and you can start receiving responses faster using a content stream.
Expand All @@ -178,6 +181,55 @@ $client->geminiPro()->generateContentStream(
// its simple syntax and rich library of functions.
```

### Streaming Chat Session

> Requires `curl` extension to be enabled
```php
$client = new GeminiAPI\Client('GEMINI_API_KEY');

$history = [
Content::text('Hello World in PHP', Role::User),
Content::text(
<<<TEXT
<?php
echo "Hello World!";
?>

This code will print "Hello World!" to the standard output.
TEXT,
Role::Model,
),
];
$chat = $client->geminiPro()
->startChat()
->withHistory($history);

$callback = function (GenerateContentResponse $response): void {
static $count = 0;

print "\nResponse #{$count}\n";
print $response->text();
$count++;
};

$chat->sendMessageStream($callback, new TextPart('in Go'));
```

```text
Response #0
package main
import "fmt"
func main() {
Response #1
fmt.Println("Hello World!")
}
This code will print "Hello World!" to the standard output.
```

### Embed Content

Expand Down
31 changes: 31 additions & 0 deletions src/ChatSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ public function sendMessage(PartInterface ...$parts): GenerateContentResponse
return $response;
}

/**
* @param callable(GenerateContentResponse): void $callback
* @param PartInterface ...$parts
* @return void
*/
public function sendMessageStream(
callable $callback,
PartInterface ...$parts,
): void {
$this->history[] = new Content($parts, Role::User);

$parts = [];
$partsCollectorCallback = function (GenerateContentResponse $response) use ($callback, &$parts) {
if(!empty($response->candidates)) {
array_push($parts, ...$response->parts());
}

$callback($response);
};

$config = (new GenerationConfig())
->withCandidateCount(1);
$this->model
->withGenerationConfig($config)
->generateContentStreamWithContents($partsCollectorCallback, $this->history);

if (!empty($parts)) {
$this->history[] = new Content($parts, Role::Model);
}
}

/**
* @return Content[]
*/
Expand Down
14 changes: 13 additions & 1 deletion src/GenerativeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,21 @@ public function generateContentStream(
): void {
$content = new Content($parts, Role::User);

$this->generateContentStreamWithContents($callback, [$content]);
}

/**
* @param callable(GenerateContentResponse): void $callback
* @param Content[] $contents
* @return void
*/
public function generateContentStreamWithContents(callable $callback, array $contents): void
{
$this->ensureArrayOfType($contents, Content::class);

$request = new GenerateContentStreamRequest(
$this->modelName,
[$content],
$contents,
$this->safetySettings,
$this->generationConfig,
);
Expand Down

0 comments on commit d56e712

Please sign in to comment.