Streaming conversations

Chariot uses server-sent events to stream new message chunks as they are generated. To get the server-sent event stream from the Chariot API, set stream:true while calling the conversations endpoint.

Using the SDK

If you're using the Chariot Node SDK, you can use the use the streamConversation() method to read the server-sent event stream from the Chariot API:

import { Chariot } from 'chariotai';

const chariot = new Chariot(process.env.CHARIOT_API_KEY);

// If no conversation_id is provided, a new conversation will be created
const conversation = await chariot.streamConversation({
  message: message,
  application_id: 'app_MDUxZmU4'
})

// Triggered for each new message chunk
// Includes the message chunk, conversation_id, and conversation title
conversation.on('message', (message: any) => {
  console.log(message);
});

// Triggered when the stream ends successfuly
// Includes total token count and sources used by the LLM
conversation.on('complete', (data: any) => {
  console.log('Streaming completed:', data);
});

// Triggered when the stream ends (successfuly or not)
conversation.on('end', () => {
  console.log('Stream ended');
});

// Triggered when there is an error during the stream
conversation.on('error', (data: any) => {
  console.log('Error:', data);
});

// Aborts the stream and terminates the request
conversation.abort();

Alternatively, if you want to work with the raw server-sent event stream, you can make a POST request to the conversations endpoint.

For questions and support, you can join our Discord community or email help@chariotai.com.