Astra DB 聊天记忆
对于跨聊天会话的长期持久性,您可以将支持聊天记忆类(如 BufferMemory
)的默认内存中 chatHistory
替换为 Astra DB。
设置
您需要安装 Astra DB TS 客户端
- npm
- Yarn
- pnpm
npm install @datastax/astra-db-ts
yarn add @datastax/astra-db-ts
pnpm add @datastax/astra-db-ts
提示
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
配置和初始化
有两种方法可以初始化您的 AstraDBChatMessageHistory
如果您已经定义了 AstraDB
客户端的实例,您可以连接到您的集合并使用构造函数初始化 ChatMessageHistory
的实例。
const client = (client = new AstraDB(
process.env.ASTRA_DB_APPLICATION_TOKEN,
process.env.ASTRA_DB_ENDPOINT,
process.env.ASTRA_DB_NAMESPACE
));
const collection = await client.collection("YOUR_COLLECTION_NAME");
const chatHistory = new AstraDBChatMessageHistory({
collection,
sessionId: "YOUR_SESSION_ID",
});
如果您还没有 AstraDB
客户端的实例,可以使用 initialize
方法。
const chatHistory = await AstraDBChatMessageHistory.initialize({
token: process.env.ASTRA_DB_APPLICATION_TOKEN ?? "token",
endpoint: process.env.ASTRA_DB_ENDPOINT ?? "endpoint",
namespace: process.env.ASTRA_DB_NAMESPACE,
collectionName: "YOUR_COLLECTION_NAME",
sessionId: "YOUR_SESSION_ID",
});
用法
提示
您的集合必须已经存在
import { RunnableWithMessageHistory } from "@langchain/core/runnables";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatOpenAI } from "@langchain/openai";
import { AstraDBChatMessageHistory } from "@langchain/community/stores/message/astradb";
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 = await AstraDBChatMessageHistory.initialize({
token: process.env.ASTRA_DB_APPLICATION_TOKEN as string,
endpoint: process.env.ASTRA_DB_ENDPOINT as string,
namespace: process.env.ASTRA_DB_NAMESPACE,
collectionName: "YOUR_COLLECTION_NAME",
sessionId,
});
return chatHistory;
},
});
const res1 = await chainWithHistory.invoke(
{
input: "Hi! I'm Jim.",
},
{ configurable: { sessionId: "langchain-test-session" } }
);
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 chainWithHistory.invoke(
{ input: "What did I just say my name was?" },
{ configurable: { sessionId: "langchain-test-session" } }
);
console.log({ res2 });
/*
{
res2: {
text: "You said your name was Jim."
}
}
*/
API 参考
- RunnableWithMessageHistory 来自
@langchain/core/runnables
- ChatPromptTemplate 来自
@langchain/core/prompts
- MessagesPlaceholder 来自
@langchain/core/prompts
- StringOutputParser 来自
@langchain/core/output_parsers
- ChatOpenAI 来自
@langchain/openai
- AstraDBChatMessageHistory 来自
@langchain/community/stores/message/astradb