Streaming Responses
Get real-time token-by-token responses for better user experience.
tip
Streaming is ideal for chat interfaces where you want to display text as it's generated.
Basic Streaming
- TypeScript
- Python
- Rust
import { getAgent } from '@sekuire/sdk';
const agent = await getAgent('assistant');
// Stream tokens
for await (const chunk of agent.chatStream('Tell me a story')) {
process.stdout.write(chunk);
}
console.log(); // newline at end
import asyncio
from sekuire_sdk import get_agent
async def main():
agent = await get_agent('assistant')
# Stream tokens
async for chunk in agent.chat_stream('Tell me a story'):
print(chunk, end='', flush=True)
print() # newline at end
asyncio.run(main())
use sekuire_sdk::get_agent;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut agent = get_agent(Some("assistant"), None).await?;
// Stream tokens
let mut stream = agent.chat_stream("Tell me a story", None).await?;
while let Some(chunk) = stream.next().await {
print!("{}", chunk?);
}
println!();
Ok(())
}
Web Streaming (Server-Sent Events)
Stream responses to web clients using SSE:
Express.js
- TypeScript
- Python
- Rust
import express from 'express';
import { getAgent } from '@sekuire/sdk';
const app = express();
app.get('/chat/stream', async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
const agent = await getAgent('assistant');
const message = req.query.message as string;
for await (const chunk of agent.chatStream(message)) {
res.write(`data: ${JSON.stringify({ chunk })}\n\n`);
}
res.write('data: [DONE]\n\n');
res.end();
});
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from sekuire_sdk import get_agent
app = FastAPI()
@app.get("/chat/stream")
async def stream_chat(message: str):
agent = await get_agent('assistant')
async def generate():
async for chunk in agent.chat_stream(message):
yield f"data: {json.dumps({'chunk': chunk})}\n\n"
yield "data: [DONE]\n\n"
return StreamingResponse(
generate(),
media_type="text/event-stream"
)
use actix_web::{web, App, HttpServer, HttpResponse};
use sekuire_sdk::get_agent;
async fn stream_chat(query: web::Query<ChatQuery>) -> HttpResponse {
let agent = get_agent(Some("assistant"), None).await.unwrap();
let stream = async_stream::stream! {
let mut stream = agent.chat_stream(&query.message, None).await.unwrap();
while let Some(chunk) = stream.next().await {
yield Ok::<_, actix_web::Error>(
format!("data: {}\n\n", serde_json::json!({"chunk": chunk}))
);
}
yield Ok("data: [DONE]\n\n".to_string());
};
HttpResponse::Ok()
.content_type("text/event-stream")
.streaming(stream)
}
Client-Side Consumption
Consume the SSE stream in a browser:
- TypeScript
- Python
- Rust
// Browser JavaScript
const eventSource = new EventSource('/chat/stream?message=Hello');
eventSource.onmessage = (event) => {
if (event.data === '[DONE]') {
eventSource.close();
return;
}
const { chunk } = JSON.parse(event.data);
// Append to UI
document.getElementById('response').textContent += chunk;
};
eventSource.onerror = () => {
eventSource.close();
};
# Python client (using sseclient)
import sseclient
import requests
response = requests.get(
'http://localhost:8000/chat/stream?message=Hello',
stream=True
)
client = sseclient.SSEClient(response)
for event in client.events():
if event.data == '[DONE]':
break
data = json.loads(event.data)
print(data['chunk'], end='', flush=True)
// Rust client (using eventsource-client)
use eventsource_client::Client;
let client = Client::for_url("http://localhost:8000/chat/stream?message=Hello")?;
let mut stream = client.stream();
while let Some(event) = stream.next().await {
if event.data == "[DONE]" {
break;
}
let data: serde_json::Value = serde_json::from_str(&event.data)?;
print!("{}", data["chunk"]);
}
Streaming Options
- TypeScript
- Python
- Rust
for await (const chunk of agent.chatStream('Hello', {
temperature: 0.7,
maxTokens: 500,
stopSequences: ['\n\n'],
})) {
process.stdout.write(chunk);
}
async for chunk in agent.chat_stream('Hello', {
'temperature': 0.7,
'max_tokens': 500,
'stop_sequences': ['\n\n'],
}):
print(chunk, end='')
let options = StreamOptions {
temperature: Some(0.7),
max_tokens: Some(500),
stop_sequences: Some(vec!["\n\n".to_string()]),
..Default::default()
};
let mut stream = agent.chat_stream("Hello", Some(options)).await?;