@@ -50,12 +50,12 @@ const client = new Perplexity({
5050 apiKey: process .env [' PERPLEXITY_API_KEY' ], // This is the default and can be omitted
5151});
5252
53- const completion = await client .chat .completions .create ({
53+ const streamChunk = await client .chat .completions .create ({
5454 messages: [{ role: ' user' , content: ' Tell me about the latest developments in AI' }],
5555 model: ' sonar' ,
5656});
5757
58- console .log (completion . choices [ 0 ]. message .content );
58+ console .log (streamChunk .content );
5959```
6060
6161### Advanced Search Features
@@ -139,6 +139,28 @@ const localSearch = await client.search.create({
139139});
140140```
141141
142+ ## Streaming responses
143+
144+ We provide support for streaming responses using Server Sent Events (SSE).
145+
146+ ``` ts
147+ import Perplexity from ' @perplexity-ai/perplexity_ai' ;
148+
149+ const client = new Perplexity ();
150+
151+ const stream = await client .chat .completions .create ({
152+ messages: [{ role: ' user' , content: ' What is the capital of France?' }],
153+ model: ' sonar' ,
154+ stream: true ,
155+ });
156+ for await (const streamChunk of stream ) {
157+ console .log (streamChunk .id );
158+ }
159+ ```
160+
161+ If you need to cancel a stream, you can ` break ` from the loop
162+ or call ` stream.controller.abort() ` .
163+
142164### Request & Response types
143165
144166This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
@@ -169,7 +191,7 @@ const chatParams: Perplexity.Chat.CompletionCreateParams = {
169191 messages: [{ role: ' user' , content: ' What is the capital of France?' }],
170192 model: ' sonar' ,
171193};
172- const chatResponse : Perplexity .Chat . CompletionCreateResponse = await client .chat .completions .create (chatParams );
194+ const streamChunk : Perplexity .StreamChunk = await client .chat .completions .create (chatParams );
173195```
174196
175197Documentation for each method, request param, and response field are available in docstrings and will appear on hover in most modern editors.
@@ -195,7 +217,7 @@ const search = await client.search
195217 });
196218
197219// Chat completions error handling
198- const completion = await client .chat .completions
220+ const streamChunk = await client .chat .completions
199221 .create ({ messages: [{ role: ' user' , content: ' What is the capital of France?' }], model: ' sonar' })
200222 .catch (async (err ) => {
201223 if (err instanceof Perplexity .APIError ) {
@@ -302,11 +324,11 @@ const chatResponse = await client.chat.completions
302324console .log (chatResponse .headers .get (' X-My-Header' ));
303325console .log (chatResponse .statusText ); // access the underlying Response object
304326
305- const { data : completion , response : rawChatResponse } = await client .chat .completions
327+ const { data : streamChunk , response : rawChatResponse } = await client .chat .completions
306328 .create ({ messages: [{ role: ' user' , content: ' What is the capital of France?' }], model: ' sonar' })
307329 .withResponse ();
308330console .log (rawChatResponse .headers .get (' X-My-Header' ));
309- console .log (completion .id );
331+ console .log (streamChunk .id );
310332```
311333
312334### Logging
0 commit comments