Skip to main content

Dialog Mode with Assistant

Using this method, you can send messages to the bot in dialog format. The bot will independently determine whether to use document search (RAG) to form the answer, and when using search, will return sources of information with the answer.

Sending Messages in Dialog Format

POST your_base_url/agent/{bot_id}/v1/chat/completions

Below are request examples for Python and cURL.

Important! Only 2 roles are used: user and assistant

import requests
import json

# Replace with your bot_id and token
bot_id = "assistant_1234"
token = "your_InsightStream_token"

# Define URL
url = f"your_base_url/agent/{bot_id}/v1/chat/completions"

headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}"
}

# Create request body
payload = {
"messages": [
{
"role": "user",
"content": "What are the urgency assessment criteria?"
}
],
"stream": False # Set True for streaming response
}

# Execute POST request
response = requests.post(url, headers=headers, data=json.dumps(payload))

</TabItem>

<TabItem value="curl" label="cURL">

```bash
curl -X POST "your_base_url/agent/{bot_id}/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_InsightStream_token" \
-d '{
"messages": [
{
"role": "user",
"content": "What are the urgency assessment criteria?"
}
],
"stream": false
}'

Request Response:

  • 200 OK: Returns an answer and, when using RAG, links to relevant documents.
  • 400 Bad Request: Invalid input data.
  • 404 Not Found: Bot not found or indexing not completed.
  • 429 Too Many Requests: Request limit exceeded.

Example Response (without RAG)

{
"id": "chatcmpl-ABC123XYZ",
"object": "chat.completion",
"created": 1741569952,
"model": "your-model-name",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Urgency assessment criteria usually include task importance, execution deadlines, and impact on business processes. For a more accurate answer, I need the context of your question.",
},
"finish_reason": "stop"
}
]
}

Example Response (with RAG)

The documents and relevant_questions fields appear in the response only if the bot decided to use RAG.

{
"id": "chatcmpl-ABC123XYZ",
"object": "chat.completion",
"created": 1741569952,
"model": "your-model-name",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "Good day! Here's the answer to your question."
}
}
],
"sources": [
{
"source_location": "http://your_base_url/documents/doc.pdf",
"source_document_name": "doc.pdf",
"source_document_type": "pdf",
"chunk": {
"begin_page": 1,
"end_page": 7,
"seq_number": 0
}
},
{
"source_location": "http://your_base_url/documents/doc.pdf",
"source_document_name": "doc.pdf",
"source_document_type": "pdf",
"chunk": {
"begin_page": 24,
"end_page": 30,
"seq_number": 4
}
},
note

After the first messages, you need to add the entire conversation history to "messages" so the bot remembers the conversation context. "sources" should not be added to "messages".

Streaming Responses

When setting the "stream": true parameter in the request, the response will come in parts in Server-Sent Events format. The first chunk may contain the sources field (if the bot decided to use RAG), subsequent chunks will contain only parts of the answer text.

Example streaming response:

data: {"id": "chatcmpl-0b09136c-dd92-402e-b653-eb8eecd194c4", "object": "chat.completion.chunk", "created": 1748471741, "model": "compressa", "choices": [{"index": 0, "delta": {"role": "assistant"}, "finish_reason": null}], "sources": [{"source_location": "http://your_url/documents/doc.pdf", "source_document_name": "doc.pdf", "source_document_type": "pdf", "chunk": {"begin_page": 37, "end_page": 44, "seq_number": 6}}, {"source_location": "http://your_url/documents/doc2.pdf", "source_document_name": "doc2.pdf", "source_document_type": "pdf", "chunk": {"begin_page": 19, "end_page": 24, "seq_number": 3}}]}

data: {"id": "chatcmpl-0b09136c-dd92-402e-b653-eb8eecd194c4", "object": "chat.completion.chunk", "created": 1748471741, "model": "compressa", "choices": [{"index": 0, "delta": {"content": ""}, "finish_reason": null}]}

data: {"id": "chatcmpl-0b09136c-dd92-402e-b653-eb8eecd194c4", "object": "chat.completion.chunk", "created": 1748471741, "model": "compressa", "choices": [{"index": 0, "delta": {"content": "H"}, "finish_reason": null}]}

Response Parameter Descriptions

id: Unique request identifier.

object: Response object type.

created: Response creation timestamp.

model: Name of the model used.

choices: Array with possible answers (usually one).

message.role: Message sender role (in this case "assistant").

message.content: Bot answer content.

sources: List of documents the bot used to answer the question (present only if RAG was used).

finish_reason: Reason for generation completion ("stop" means normal completion).