Skip to main content

Azure Cosmos DB NoSQL 聊天消息历史记录

AzureCosmosDBNoSQLChatMessageHistory 使用 Cosmos DB 存储聊天消息历史记录。为了在聊天会话之间实现更长期的持久化,您可以替换默认的内存中 chatHistory,该实例支持如 BufferMemory 这类聊天内存类。

如果您还没有 Azure 帐户,可以创建一个免费帐户来开始使用。

设置

首先,您需要安装 @langchain/azure-cosmosdb 包:

npm install @langchain/azure-cosmosdb @langchain/core

:::提示 请参阅安装集成包的一般说明部分。 :::

npm install @langchain/openai @langchain/community @langchain/core

此外,您还需要运行一个 Azure Cosmos DB for NoSQL 实例。您可以按照此指南在 Azure 门户中免费部署一个实例。

一旦实例运行起来,请确保您拥有连接字符串。如果您使用的是托管身份,则需要拥有端点地址。您可以在 Azure 门户中实例的 "设置 / 密钥" 部分找到这些信息。

info

当使用 Azure 托管身份和基于角色的访问控制(RBAC)时,您必须确保数据库和容器已经预先创建好。RBAC 不提供创建数据库和容器的权限。您可以从 Azure Cosmos DB 文档中了解更多有关权限模型的信息。

使用方法

import { ChatOpenAI } from "@langchain/openai";
import { AzureCosmsosDBNoSQLChatMessageHistory } from "@langchain/azure-cosmosdb";
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";

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

const prompt = ChatPromptTemplate.fromMessages([
[
"system",
"You are a helpful assistant. Answer all questions to the best of your ability.",
],
new MessagesPlaceholder("chat_history"),
["human", "{input}"],
]);

const chain = prompt.pipe(model).pipe(new StringOutputParser());

const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
getMessageHistory: async (sessionId) => {
const chatHistory = new AzureCosmsosDBNoSQLChatMessageHistory({
sessionId,
userId: "user-id",
databaseName: "DATABASE_NAME",
containerName: "CONTAINER_NAME",
});
return chatHistory;
},
});

const res1 = await chainWithHistory.invoke(
{ input: "Hi! I'm Jim." },
{ configurable: { sessionId: "langchain-test-session" } }
);
console.log({ res1 });
/*
{ res1: 'Hi Jim! How can I assist you today?' }
*/

const res2 = await chainWithHistory.invoke(
{ input: "What did I just say my name was?" },
{ configurable: { sessionId: "langchain-test-session" } }
);
console.log({ res2 });
/*
{ res2: { response: 'You said your name was Jim.' }
*/

// Give this session a title
const chatHistory = (await chainWithHistory.getMessageHistory(
"langchain-test-session"
)) as AzureCosmsosDBNoSQLChatMessageHistory;

await chatHistory.setContext({ title: "Introducing Jim" });

// List all session for the user
const sessions = await chatHistory.getAllSessions();

console.log(sessions);
/*
[
{ sessionId: 'langchain-test-session', context: { title: "Introducing Jim" } }
]
*/

API Reference:


Was this page helpful?


You can also leave detailed feedback on GitHub.