Skip to main content

MongoDB 聊天内存

¥MongoDB Chat Memory

Compatibility

仅在 Node.js 上可用。

¥Only available on Node.js.

你仍然可以通过将 runtime 变量设置为 nodejs,创建使用 MongoDB 和 Next.js 的 API 路由,如下所示:

¥You can still create API routes that use MongoDB with Next.js by setting the runtime variable to nodejs like so:



export const runtime = "nodejs";
```



You can read more about Edge runtimes in the Next.js documentation [here](https://next.nodejs.cn/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes).


For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory that backs chat memory classes like BufferMemory for a MongoDB instance.

Setup

You need to install Node MongoDB SDK in your project:

npm install -S mongodb
npm install @langchain/openai @langchain/community @langchain/core

你还需要一个 MongoDB 实例来连接。

¥You will also need a MongoDB instance to connect to.

用法

¥Usage

存储在 MongoDB 中的每个聊天历史记录会话都必须具有唯一的会话 ID。

¥Each chat history session stored in MongoDB must have a unique session id.

import { MongoClient, ObjectId } from "mongodb";
import { BufferMemory } from "langchain/memory";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import { MongoDBChatMessageHistory } from "@langchain/mongodb";

const client = new MongoClient(process.env.MONGODB_ATLAS_URI || "", {
driverInfo: { name: "langchainjs" },
});
await client.connect();
const collection = client.db("langchain").collection("memory");

// generate a new sessionId string
const sessionId = new ObjectId().toString();

const memory = new BufferMemory({
chatHistory: new MongoDBChatMessageHistory({
collection,
sessionId,
}),
});

const model = new ChatOpenAI({
model: "gpt-3.5-turbo",
temperature: 0,
});

const chain = new ConversationChain({ llm: model, memory });

const res1 = await chain.invoke({ input: "Hi! I'm Jim." });
console.log({ res1 });
/*
{
res1: {
text: "Hello Jim! It's nice to meet you. My name is AI. How may I assist you today?"
}
}
*/

const res2 = await chain.invoke({ input: "What did I just say my name was?" });
console.log({ res2 });

/*
{
res1: {
text: "You said your name was Jim."
}
}
*/

// See the chat history in the MongoDb
console.log(await memory.chatHistory.getMessages());

// clear chat history
await memory.chatHistory.clear();

API Reference: