Continue Conversation
Have multi-turn conversations by including the full message history.
How It Works
- Use the same
idfor all messages in the conversation - Include all previous messages in order
- Add your new message at the end
Example
Turn 1: First Question
curl -N -X POST BOX_URL/api/ai/chat \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"id": "conversation-123",
"messages": [
{"role": "user", "content": "What is the capital of France?"}
],
"boxAddress": "BOX_URL"
}'Response: "The capital of France is Paris."
Turn 2: Follow-up
Include the previous exchange:
curl -N -X POST BOX_URL/api/ai/chat \
-H "x-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"id": "conversation-123",
"messages": [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What is its population?"}
],
"boxAddress": "BOX_URL"
}'The AI knows "its" refers to Paris because it has the context.
Turn 3: Another Follow-up
{
"id": "conversation-123",
"messages": [
{"role": "user", "content": "What is the capital of France?"},
{"role": "assistant", "content": "The capital of France is Paris."},
{"role": "user", "content": "What is its population?"},
{"role": "assistant", "content": "Paris has a population of about 2.1 million..."},
{"role": "user", "content": "What about the metro area?"}
],
"boxAddress": "BOX_URL"
}JavaScript Example
conversation.js
class Conversation {
constructor(apiKey, boxAddress = 'https://your-box.intelligencebox.it') {
this.id = 'conv-' + Date.now();
this.messages = [];
this.apiKey = apiKey;
this.boxAddress = boxAddress;
}
async send(message) {
// Add user message
this.messages.push({ role: 'user', content: message });
const response = await fetch(`${this.boxAddress}/api/ai/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey
},
body: JSON.stringify({
id: this.id,
messages: this.messages,
boxAddress: this.boxAddress
})
});
// Parse SSE and collect response
const reader = response.body.getReader();
const decoder = new TextDecoder();
let assistantResponse = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
for (const line of decoder.decode(value).split('\n')) {
if (line.startsWith('data: ')) {
try {
const data = JSON.parse(line.slice(6));
if (data.type === 'text-delta') {
assistantResponse += data.textDelta;
}
} catch (e) {}
}
}
}
// Add assistant response to history
this.messages.push({ role: 'assistant', content: assistantResponse });
return assistantResponse;
}
}
// Usage
const chat = new Conversation('YOUR_API_KEY');
const r1 = await chat.send('What is the capital of France?');
console.log(r1); // "The capital of France is Paris."
const r2 = await chat.send('What is its population?');
console.log(r2); // "Paris has a population of about 2.1 million..."
const r3 = await chat.send('Compare it to London');
console.log(r3); // AI knows we're comparing Paris to LondonWith Assistant
You can continue conversations with assistants too:
{
"id": "conversation-123",
"messages": [
{"role": "user", "content": "Analyze my sales data"},
{"role": "assistant", "content": "Based on your documents, sales increased by 15%..."},
{"role": "user", "content": "What about Q4 specifically?"}
],
"boxAddress": "BOX_URL",
"assistantId": "your-assistant-id",
"vector": ["your-folder-id"]
}Tips
- Same ID: Always use the same
idfor the entire conversation - Full History: Include all messages every time
- Order Matters: Messages must be in chronological order
- Trim Long Chats: For very long conversations, you may want to summarize older messages
