Skip to main content

基于 Cloudflare D1 的聊天记忆

info

此集成仅支持在 Cloudflare Workers 中使用。

为了在多个聊天会话中实现更长期的数据持久化,您可以将用于聊天记忆类(如 BufferMemory)的默认内存 chatHistory 替换为 Cloudflare D1 实例。

安装与配置

您需要安装 LangChain Cloudflare 集成包。
在以下示例中,我们同时使用了 Anthropic,但您也可以使用任意模型:

:::提示 请参阅安装集成包的一般说明部分。 :::

npm install @langchain/cloudflare @langchain/anthropic @langchain/core

按照 官方文档 为您的 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" # 在 Worker 中可通过 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 Reference:


Was this page helpful?


You can also leave detailed feedback on GitHub.