跳到主要内容

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 门户的实例的“设置/密钥”部分中找到它们。

信息

当使用 Azure 托管标识和基于角色的访问控制时,您必须确保已预先创建数据库和容器。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 参考


此页是否对您有帮助?


您也可以留下详细的反馈 在 GitHub 上.