Advanced Streaming
Implement real-time streaming conversations.
For real-time interactions, the stream()
method allows you to receive chunks of data as they are generated:
const stream = await client.chat.stream([
{ role: 'user', content: 'Explain quantum mechanics.' }
]);
for await (const chunk of stream.stream) {
console.log(chunk);
}
You can also apply transformations to the stream:
import { mapStreamIterator } from 'secton';
const transformedStream = mapStreamIterator(
stream.stream,
chunk => chunk.toUpperCase()
);
for await (const chunk of transformedStream) {
console.log(chunk);
}
This approach is ideal for apps requiring real-time data processing.
Last updated
Was this helpful?