Azure Cosmos DB NoSQL 聊天消息历史记录
¥Azure Cosmos DB NoSQL Chat Message History
AzureCosmosDBNoSQLChatMessageHistory 使用 Cosmos DB 存储聊天消息历史记录。为了在聊天会话中实现更长期的持久化,你可以将支持聊天内存类(如 BufferMemory)的默认内存 chatHistory 替换为 Upstash 实例。如果你没有 Azure 账户,可以使用 创建免费账户 开始使用。
¥The AzureCosmosDBNoSQLChatMessageHistory uses Cosmos DB to store chat message history. For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory that backs chat memory classes like BufferMemory.
If you don't have an Azure account, you can create a free account to get started.
设置
¥Setup
你首先需要安装 @langchain/azure-cosmosdb 软件包:
¥You'll first need to install the @langchain/azure-cosmosdb package:
- npm
- Yarn
- pnpm
npm install @langchain/azure-cosmosdb @langchain/core
yarn add @langchain/azure-cosmosdb @langchain/core
pnpm add @langchain/azure-cosmosdb @langchain/core
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
你还需要运行一个 Azure Cosmos DB for NoSQL 实例。你可以按照 此指南 在 Azure 门户上免费部署一个版本。
¥You'll also need to have an Azure Cosmos DB for NoSQL instance running. You can deploy a free version on Azure Portal without any cost, following this guide.
实例运行后,请确保你拥有连接字符串。如果你使用托管身份,则需要拥有端点。你可以在 Azure 门户中实例的 "设置/密钥" 部分下找到它们。
¥Once you have your instance running, make sure you have the connection string. If you are using Managed Identity, you need to have the endpoint. You can find them in the Azure Portal, under the "Settings / Keys" section of your instance.
使用 Azure 托管标识和基于角色的访问控制时,必须确保已预先创建数据库和容器。RBAC 不提供创建数据库和容器的权限。你可以在 Azure Cosmos DB 文档 中获取有关权限模型的更多信息。
¥When using Azure Managed Identity and role-based access control, you must ensure that the database and container have been created beforehand. RBAC does not provide permissions to create databases and containers. You can get more information about the permission model in the Azure Cosmos DB documentation.
用法
¥Usage
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:
- ChatOpenAI from
@langchain/openai - AzureCosmsosDBNoSQLChatMessageHistory from
@langchain/azure-cosmosdb - RunnableWithMessageHistory from
@langchain/core/runnables - StringOutputParser from
@langchain/core/output_parsers - ChatPromptTemplate from
@langchain/core/prompts - MessagesPlaceholder from
@langchain/core/prompts