Cloudflare D1 支持的聊天记忆
信息
此集成仅在 Cloudflare Workers 中受支持。
为了跨聊天会话实现更长时间的持久性,您可以将支持聊天记忆类的默认内存中 chatHistory
(例如 BufferMemory
)替换为 Cloudflare D1 实例。
设置
您需要安装 LangChain Cloudflare 集成包。对于以下示例,我们还使用 Anthropic,但您可以使用任何您喜欢的模型
提示
- npm
- Yarn
- pnpm
npm install @langchain/cloudflare @langchain/anthropic
yarn add @langchain/cloudflare @langchain/anthropic
pnpm add @langchain/cloudflare @langchain/anthropic
按照 官方文档 为您的 worker 设置 D1 实例。您的项目的 wrangler.toml
文件应类似于以下内容
name = "YOUR_PROJECT_NAME"
main = "src/index.ts"
compatibility_date = "2024-01-10"
[vars]
ANTHROPIC_API_KEY = "YOUR_ANTHROPIC_KEY"
[[d1_databases]]
binding = "DB" # available in your Worker as env.DB
database_name = "YOUR_D1_DB_NAME"
database_id = "YOUR_D1_DB_ID"
使用
然后,您可以使用 D1 存储您的历史记录,如下所示
import type { D1Database } from "@cloudflare/workers-types";
import { BufferMemory } from "langchain/memory";
import { CloudflareD1MessageHistory } from "@langchain/cloudflare";
import {
ChatPromptTemplate,
MessagesPlaceholder,
} from "@langchain/core/prompts";
import { RunnableSequence } from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatAnthropic } from "@langchain/anthropic";
export interface Env {
DB: D1Database;
ANTHROPIC_API_KEY: string;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
try {
const { searchParams } = new URL(request.url);
const input = searchParams.get("input");
if (!input) {
throw new Error(`Missing "input" parameter`);
}
const memory = new BufferMemory({
returnMessages: true,
chatHistory: new CloudflareD1MessageHistory({
tableName: "stored_message",
sessionId: "example",
database: env.DB,
}),
});
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful chatbot"],
new MessagesPlaceholder("history"),
["human", "{input}"],
]);
const model = new ChatAnthropic({
apiKey: env.ANTHROPIC_API_KEY,
});
const chain = RunnableSequence.from([
{
input: (initialInput) => initialInput.input,
memory: () => memory.loadMemoryVariables({}),
},
{
input: (previousOutput) => previousOutput.input,
history: (previousOutput) => previousOutput.memory.history,
},
prompt,
model,
new StringOutputParser(),
]);
const chainInput = { input };
const res = await chain.invoke(chainInput);
await memory.saveContext(chainInput, {
output: res,
});
return new Response(JSON.stringify(res), {
headers: { "content-type": "application/json" },
});
} catch (err: any) {
console.log(err.message);
return new Response(err.message, { status: 500 });
}
},
};
API 参考
- BufferMemory 来自
langchain/memory
- CloudflareD1MessageHistory 来自
@langchain/cloudflare
- ChatPromptTemplate 来自
@langchain/core/prompts
- MessagesPlaceholder 来自
@langchain/core/prompts
- RunnableSequence 来自
@langchain/core/runnables
- StringOutputParser 来自
@langchain/core/output_parsers
- ChatAnthropic 来自
@langchain/anthropic