How to Implement Real-time Chat

Learn how to implement chat with streaming to show the response as it's being generated.

Problem

You want to create a smooth chat experience where the user sees the response being typed in real-time.

Solution

Use the /v1/kb/chat/stream endpoint that returns the response via Server-Sent Events (SSE).

Endpoint

POST /v1/kb/chat/stream
Content-Type: application/json
x-api-key: YOUR_API_KEY

Response Format

The response comes as SSE events:

data: {"event":"message","data":{"content":"First"}}

data: {"event":"message","data":{"content":" part"}}

data: {"event":"message","data":{"content":" of the response"}}

data: [DONE]

JavaScript Implementation

Using fetch + ReadableStream

Using EventSource (simpler)

Note: EventSource only works with GET, so you'll need a proxy or use fetch.

Python Implementation

React Implementation

Handling Errors

Tips

1. Show Loading Indicator

2. Auto-scroll

3. Allow Cancellation

Last updated

Was this helpful?