@@ -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+
156159In the streaming response, the callback function will be called whenever a response is returned from the server.
157160
158161Long 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
0 commit comments