Skip to content

Commit d56e712

Browse files
committed
Add streaming chat session
1 parent 569db52 commit d56e712

3 files changed

Lines changed: 96 additions & 1 deletion

File tree

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _This library is not developed or endorsed by Google._
2323
- [Chat Session (Multi-Turn Conversations)](#chat-session-multi-turn-conversations)
2424
- [Chat Session with history](#chat-session-with-history)
2525
- [Streaming responses](#streaming-responses)
26+
- [Streaming Chat Session](#streaming-chat-session)
2627
- [Tokens counting](#tokens-counting)
2728
- [Listing models](#listing-models)
2829

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

154155
### Streaming responses
155156

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

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

184+
### Streaming Chat Session
185+
186+
> Requires `curl` extension to be enabled
187+
188+
```php
189+
$client = new GeminiAPI\Client('GEMINI_API_KEY');
190+
191+
$history = [
192+
Content::text('Hello World in PHP', Role::User),
193+
Content::text(
194+
<<<TEXT
195+
<?php
196+
echo "Hello World!";
197+
?>
198+
199+
This code will print "Hello World!" to the standard output.
200+
TEXT,
201+
Role::Model,
202+
),
203+
];
204+
$chat = $client->geminiPro()
205+
->startChat()
206+
->withHistory($history);
207+
208+
$callback = function (GenerateContentResponse $response): void {
209+
static $count = 0;
210+
211+
print "\nResponse #{$count}\n";
212+
print $response->text();
213+
$count++;
214+
};
215+
216+
$chat->sendMessageStream($callback, new TextPart('in Go'));
217+
```
218+
219+
```text
220+
Response #0
221+
package main
222+
223+
import "fmt"
224+
225+
func main() {
226+
227+
Response #1
228+
fmt.Println("Hello World!")
229+
}
230+
231+
This code will print "Hello World!" to the standard output.
232+
```
181233

182234
### Embed Content
183235

src/ChatSession.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@ public function sendMessage(PartInterface ...$parts): GenerateContentResponse
4545
return $response;
4646
}
4747

48+
/**
49+
* @param callable(GenerateContentResponse): void $callback
50+
* @param PartInterface ...$parts
51+
* @return void
52+
*/
53+
public function sendMessageStream(
54+
callable $callback,
55+
PartInterface ...$parts,
56+
): void {
57+
$this->history[] = new Content($parts, Role::User);
58+
59+
$parts = [];
60+
$partsCollectorCallback = function (GenerateContentResponse $response) use ($callback, &$parts) {
61+
if(!empty($response->candidates)) {
62+
array_push($parts, ...$response->parts());
63+
}
64+
65+
$callback($response);
66+
};
67+
68+
$config = (new GenerationConfig())
69+
->withCandidateCount(1);
70+
$this->model
71+
->withGenerationConfig($config)
72+
->generateContentStreamWithContents($partsCollectorCallback, $this->history);
73+
74+
if (!empty($parts)) {
75+
$this->history[] = new Content($parts, Role::Model);
76+
}
77+
}
78+
4879
/**
4980
* @return Content[]
5081
*/

src/GenerativeModel.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,21 @@ public function generateContentStream(
7272
): void {
7373
$content = new Content($parts, Role::User);
7474

75+
$this->generateContentStreamWithContents($callback, [$content]);
76+
}
77+
78+
/**
79+
* @param callable(GenerateContentResponse): void $callback
80+
* @param Content[] $contents
81+
* @return void
82+
*/
83+
public function generateContentStreamWithContents(callable $callback, array $contents): void
84+
{
85+
$this->ensureArrayOfType($contents, Content::class);
86+
7587
$request = new GenerateContentStreamRequest(
7688
$this->modelName,
77-
[$content],
89+
$contents,
7890
$this->safetySettings,
7991
$this->generationConfig,
8092
);

0 commit comments

Comments
 (0)