Skip to main content

Persist Conversation Data

The MongoDB Chatbot Server persists and retrieves conversation data data using the ConversationsService interface.

Store Data in MongoDB

By default, the MongoDB Chatbot Server uses MongoDB to persist conversation data.

Use the makeMongoDbConversationsService() function to create a ConversationsService that stores data in MongoDB.

When you create a ConversationService with MongoDB, pass a MongoDB database and the chatbot's SystemPrompt. All conversations are stored in the database's conversations collection. The SystemPrompt is the first message in all conversations. For more information on the SystemPrompt, refer to the System Prompt Engineering guide.

Add the ConversationsService to the ConversationsRouterParams.conversations property.

import { MongoClient } from "mongodb";
import {
makeMongoDbConversationsService,
SystemPrompt,
} from "mongodb-chatbot-server";

const systemPrompt: SystemPrompt = {
role: "system",
content: `<some system prompt>`,
};
const mongodb = new MongoClient(MONGODB_CONNECTION_URI);
const conversations = makeMongoDbConversationsService(
mongodb.db(MONGODB_DATABASE_NAME),
systemPrompt
);

const appConfig: AppConfig = {
// ...other config
conversationsRouterParams: {
conversations,
// ...other config
},
};

Every conversation is stored in a MongoDB collection with the name conversations in a document with the schema Conversations.

Alternate Databases

You could use a different database to store conversation data, as long as you use the ConversationsService interface.