Firestore 聊天记忆
为了在聊天会话中实现更长期的持久性,您可以将支持聊天记忆类(如 BufferMemory
)的默认内存中 chatHistory
替换为 firestore。
设置
首先,在项目中安装 Firebase 管理包
- npm
- Yarn
- pnpm
npm install firebase-admin
yarn add firebase-admin
pnpm add firebase-admin
提示
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community
yarn add @langchain/openai @langchain/community
pnpm add @langchain/openai @langchain/community
从您的 Firebase 项目中访问 项目设置
页面,并选择 服务帐号
选项卡。
在 服务帐号
选项卡中,单击 Firebase Admin SDK
部分中的 生成新的私钥
按钮,以下载包含服务帐号凭据的 JSON 文件。
使用下载的 JSON 文件,将 projectId
、privateKey
和 clientEmail
传递到 FirestoreChatMessageHistory
类的 config
对象中,如下所示
import { FirestoreChatMessageHistory } from "@langchain/community/stores/message/firestore";
import admin from "firebase-admin";
const messageHistory = new FirestoreChatMessageHistory({
collections: ["chats"],
docs: ["user-id"],
sessionId: "user-id",
userId: "[email protected]",
config: {
projectId: "YOUR-PROJECT-ID",
credential: admin.credential.cert({
projectId: "YOUR-PROJECT-ID",
privateKey:
"-----BEGIN PRIVATE KEY-----\nCHANGE-ME\n-----END PRIVATE KEY-----\n",
clientEmail: "[email protected]",
}),
},
});
这里,collections
字段应与数据库中 collections
的名称和顺序匹配。docs
也一样,它应该与数据库中 docs
的名称和顺序匹配。
用法
import { BufferMemory } from "langchain/memory";
import { FirestoreChatMessageHistory } from "@langchain/community/stores/message/firestore";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import admin from "firebase-admin";
const memory = new BufferMemory({
chatHistory: new FirestoreChatMessageHistory({
collections: ["langchain"],
docs: ["lc-example"],
sessionId: "lc-example-id",
userId: "[email protected]",
config: {
projectId: "YOUR-PROJECT-ID",
credential: admin.credential.cert({
projectId: "YOUR-PROJECT-ID",
privateKey:
"-----BEGIN PRIVATE KEY-----\nnCHANGE-ME\n-----END PRIVATE KEY-----\n",
clientEmail: "[email protected]",
}),
},
}),
});
const model = new ChatOpenAI();
const chain = new ConversationChain({ llm: model, memory });
const res1 = await chain.invoke({ input: "Hi! I'm Jim." });
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 chain.invoke({ input: "What did I just say my name was?" });
console.log({ res2 });
/*
{ res1: { text: "You said your name was Jim." } }
*/
API 参考
- BufferMemory 来自
langchain/memory
- FirestoreChatMessageHistory 来自
@langchain/community/stores/message/firestore
- ChatOpenAI 来自
@langchain/openai
- ConversationChain 来自
langchain/chains
嵌套集合
FirestoreChatMessageHistory
类支持嵌套集合和动态集合/文档名称。
以下示例演示了如何从具有以下结构的数据库中添加和检索消息
/chats/{chat-id}/bots/{bot-id}/messages/{message-id}
import { BufferMemory } from "langchain/memory";
import { FirestoreChatMessageHistory } from "@langchain/community/stores/message/firestore";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import admin from "firebase-admin";
const memory = new BufferMemory({
chatHistory: new FirestoreChatMessageHistory({
collections: ["chats", "bots"],
docs: ["chat-id", "bot-id"],
sessionId: "user-id",
userId: "[email protected]",
config: {
projectId: "YOUR-PROJECT-ID",
credential: admin.credential.cert({
projectId: "YOUR-PROJECT-ID",
privateKey:
"-----BEGIN PRIVATE KEY-----\nnCHANGE-ME\n-----END PRIVATE KEY-----\n",
clientEmail: "[email protected]",
}),
},
}),
});
const model = new ChatOpenAI();
const chain = new ConversationChain({ llm: model, memory });
const res1 = await chain.invoke({ input: "Hi! I'm Jim." });
console.log({ res1 });
/*
{ res1: { response: 'Hello Jim! How can I assist you today?' } }
*/
const res2 = await chain.invoke({ input: "What did I just say my name was?" });
console.log({ res2 });
/*
{ res2: { response: 'You just said that your name is Jim.' } }
*/
API 参考
- BufferMemory 来自
langchain/memory
- FirestoreChatMessageHistory 来自
@langchain/community/stores/message/firestore
- ChatOpenAI 来自
@langchain/openai
- ConversationChain 来自
langchain/chains
Firestore 规则
如果您的集合名称为“chathistory”,则可以按如下方式配置 Firestore 规则。
match /chathistory/{sessionId} {
allow read: if request.auth.uid == resource.data.createdBy;
allow write: if request.auth.uid == request.resource.data.createdBy;
}
match /chathistory/{sessionId}/messages/{messageId} {
allow read: if request.auth.uid == resource.data.createdBy;
allow write: if request.auth.uid == request.resource.data.createdBy;
}