跳到主要内容

Azure Cosmos DB Mongo vCore Chat Message History

AzureCosmosDBMongoChatMessageHistory 使用 Azure Cosmos DB Mongo vCore 来存储聊天消息历史记录。为了在聊天会话中实现更长期的持久性,您可以替换默认的内存中 chatHistory,它支持聊天记忆类,如 BufferMemory。如果您没有 Azure 帐户,您可以创建一个免费帐户以开始使用。

安装设置

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

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

您还需要运行 Azure Cosmos DB mongo vCore 实例。您可以按照本指南在 Azure 门户上部署免费版本,无需任何费用。

一旦您的实例运行起来,请确保您拥有连接字符串。

用法

import { ChatOpenAI } from "@langchain/openai";
import {
AzureCosmosDBMongoChatMessageHistory,
AzureCosmosDBMongoChatHistoryDBConfig,
} 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 dbcfg: AzureCosmosDBMongoChatHistoryDBConfig = {
connectionString: process.env.AZURE_COSMOSDB_MONGODB_CONNECTION_STRING,
databaseName: "langchain",
collectionName: "chathistory",
};

const chainWithHistory = new RunnableWithMessageHistory({
runnable: chain,
inputMessagesKey: "input",
historyMessagesKey: "chat_history",
getMessageHistory: async (sessionId) => {
const chatHistory = new AzureCosmosDBMongoChatMessageHistory(
dbcfg,
sessionId,
"user-id"
);
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 AzureCosmosDBMongoChatMessageHistory;

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

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

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

API 参考


此页内容对您有帮助吗?


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