Firestore 聊天记忆
对于跨聊天会话的更长期持久性,您可以将支持聊天记忆类的默认内存中 chatHistory
(例如 BufferMemory
)替换为 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 @langchain/core
yarn add @langchain/openai @langchain/community @langchain/core
pnpm add @langchain/openai @langchain/community @langchain/core
从您的 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;
}