How to Build Chat-Style AI Conversations
Use role-based messages, system prompts, few-shot examples, and optional knowledge-base retrieval in sendAI calls.
For richer prompts, pass a messages array with role and content fields — the same shape used by chat-completion APIs. Optionally set useKnowledgeBase: true to ground answers in your indexed documents.
When to Use Chat-Style vs. Simple Text
| Approach | Message shape | Best for |
|---|---|---|
| Simple text | { text: "..." } | Quick one-liners |
| Chat-style | { messages: [{ role, content }] } | System prompts, few-shot examples, and multi-role context |
| Chat + RAG | { useKnowledgeBase: true, messages: [...] } | Support bots grounded in your knowledge base |
Message Roles
| Role | Typical use |
|---|---|
| system | Persona, rules, and output format |
| user | End-user question or input |
| assistant | Prior model turns for replay or few-shot examples |
message: {
useKnowledgeBase: false,
messages: [
{
role: "system",
content: "You are a helpful support agent. Be concise.",
},
{
role: "user",
content: "How do I reset my password?",
},
],
}useKnowledgeBase
When true, DNotifier retrieves relevant knowledge-base chunks and injects them into the prompt before calling the model.
saveHistory
The default is true. Set it to false for classification, routing, or workflow steps you do not want in user-visible history.
Transport
Use transport: "http" for all sendAI calls.
JavaScript / TypeScript
Send structured role-based messages and optionally enable knowledge-base retrieval.
Support Agent With System Prompt
import { DNotifier } from "@dnotifier-realtime/dnotifier";
const USER_ID = "user-123";
const notifier = new DNotifier({
appId: process.env.DNOTIFIER_APP_ID,
secret: process.env.DNOTIFIER_SECRET,
transport: "http",
userId: USER_ID,
onConnected: () => {},
onMessage: () => {},
onDisconnected: () => {},
});
await notifier.connect();
const response = await notifier.sendAI({
senderId: USER_ID,
message: {
useKnowledgeBase: false,
messages: [
{
role: "system",
content:
"You are a helpful support agent. Answer in under 100 words.",
},
{
role: "user",
content: "How do I reset my password?",
},
],
},
saveHistory: true,
});
console.log(response?.data?.content ?? response);RAG-Enabled Q&A
const response = await notifier.sendAI({
senderId: USER_ID,
message: {
useKnowledgeBase: true,
messages: [
{
role: "system",
content:
"Answer using only the provided knowledge. If unsure, say you do not know.",
},
{
role: "user",
content: "What is the refund timeline?",
},
],
},
});Few-Shot Classification
const response = await notifier.sendAI({
senderId: USER_ID,
message: {
messages: [
{
role: "system",
content:
'Classify intent. Reply JSON only: {"intent":"billing"|"technical"}',
},
{ role: "user", content: "My invoice is wrong" },
{ role: "assistant", content: '{"intent":"billing"}' },
{ role: "user", content: "The app crashes on launch" },
],
},
saveHistory: false,
});Multi-Turn in One Call (No Session)
You can include prior turns in messages manually. For server-managed continuity, prefer sessions with sessionId.
TypeScript Types
type ChatMessage = {
role: "system" | "user" | "assistant";
content: string;
};
type ChatAiMessage = {
useKnowledgeBase?: boolean;
messages: ChatMessage[];
};
await notifier.sendAI({
senderId: USER_ID,
message: {
useKnowledgeBase: true,
messages: [
{ role: "system", content: "You are helpful." },
{ role: "user", content: "Hello" },
],
} satisfies ChatAiMessage,
});Dart / Flutter
Send structured role-based messages and optionally enable knowledge-base retrieval.
Support Agent With System Prompt
import 'package:dnotifier/dnotifier.dart';
const userId = 'user-123';
Future<void> main() async {
final notifier = DNotifier(
appId: 'your-app-id',
secret: 'your-app-secret',
transport: 'http',
userId: userId,
onConnected: () {},
onMessage: (_) {},
);
await notifier.connect();
final response = await notifier.sendAI(
senderId: userId,
message: {
'useKnowledgeBase': false,
'messages': [
{
'role': 'system',
'content':
'You are a helpful support agent. Answer in under 100 words.',
},
{
'role': 'user',
'content': 'How do I reset my password?',
},
],
},
saveHistory: true,
);
print(response);
}RAG-Enabled Q&A
final response = await notifier.sendAI(
senderId: userId,
message: {
'useKnowledgeBase': true,
'messages': [
{
'role': 'system',
'content':
'Answer using only the provided knowledge. If unsure, say you do not know.',
},
{
'role': 'user',
'content': 'What is the refund timeline?',
},
],
},
);Few-Shot Classification
await notifier.sendAI(
senderId: userId,
message: {
'messages': [
{
'role': 'system',
'content':
'Classify intent. Reply JSON only: {"intent":"billing"|"technical"}',
},
{'role': 'user', 'content': 'My invoice is wrong'},
{'role': 'assistant', 'content': '{"intent":"billing"}'},
{'role': 'user', 'content': 'The app crashes on launch'},
],
},
saveHistory: false,
);Flutter Widget Helper
Future<String> askSupport(
DNotifier notifier,
String question,
) async {
final response = await notifier.sendAI(
senderId: userId,
message: {
'useKnowledgeBase': true,
'messages': [
{'role': 'system', 'content': 'You are a support agent.'},
{'role': 'user', 'content': question},
],
},
);
if (response is Map && response['data'] is Map) {
return response['data']['content']?.toString() ?? '';
}
return response.toString();
}Python
Send structured role-based messages and optionally enable knowledge-base retrieval.
Support Agent With System Prompt
import asyncio
from dnotifier import DNotifier
USER_ID = "user-123"
async def main():
notifier = DNotifier(
app_id="your-app-id",
secret="your-app-secret",
transport="http",
user_id=USER_ID,
)
await notifier.connect()
response = await notifier.send_ai(
sender_id=USER_ID,
message={
"useKnowledgeBase": False,
"messages": [
{
"role": "system",
"content": (
"You are a helpful support agent. "
"Answer in under 100 words."
),
},
{
"role": "user",
"content": "How do I reset my password?",
},
],
},
save_history=True,
)
print(response)
asyncio.run(main())RAG-Enabled Q&A
response = await notifier.send_ai(
sender_id=USER_ID,
message={
"useKnowledgeBase": True,
"messages": [
{
"role": "system",
"content": (
"Answer using only the provided knowledge. "
"If unsure, say you do not know."
),
},
{
"role": "user",
"content": "What is the refund timeline?",
},
],
},
)Few-Shot Classification
await notifier.send_ai(
sender_id=USER_ID,
message={
"messages": [
{
"role": "system",
"content": (
'Classify intent. Reply JSON only: '
'{"intent":"billing"|"technical"}'
),
},
{"role": "user", "content": "My invoice is wrong"},
{"role": "assistant", "content": '{"intent":"billing"}'},
{
"role": "user",
"content": "The app crashes on launch",
},
],
},
save_history=False,
)Helper Function
async def ask_support(
notifier: DNotifier,
question: str,
) -> str:
response = await notifier.send_ai(
sender_id=USER_ID,
message={
"useKnowledgeBase": True,
"messages": [
{
"role": "system",
"content": "You are a support agent.",
},
{"role": "user", "content": question},
],
},
)
if (
isinstance(response, dict)
and isinstance(response.get("data"), dict)
):
return str(response["data"].get("content", ""))
return str(response)