跳至主要内容

PlanetScale 聊天记忆

因为 PlanetScale 通过 REST API 工作,所以您可以将其与 Vercel EdgeCloudflare Workers 和其他无服务器环境一起使用。

为了在聊天会话之间实现更长期的持久性,您可以将支持聊天记忆类的默认内存中 chatHistory(例如 BufferMemory)替换为 PlanetScale 数据库 实例。

设置

您需要在项目中安装 @planetscale/database

npm install @langchain/openai @planetscale/database @langchain/community @langchain/core

您还需要一个 PlanetScale 帐户和一个要连接的数据库。有关如何创建 HTTP 客户端的信息,请查看 PlanetScale 文档

用法

存储在 PlanetScale 数据库中的每个聊天历史会话必须具有唯一的 ID。config 参数直接传递到 @planetscale/databasenew Client() 构造函数中,并接受所有相同的参数。

import { BufferMemory } from "langchain/memory";
import { PlanetScaleChatMessageHistory } from "@langchain/community/stores/message/planetscale";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";

const memory = new BufferMemory({
chatHistory: new PlanetScaleChatMessageHistory({
tableName: "stored_message",
sessionId: "lc-example",
config: {
url: "ADD_YOURS_HERE", // Override with your own database instance's URL
},
}),
});

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 参考

高级用法

您也可以直接传入之前创建的 @planetscale/database 客户端实例

import { BufferMemory } from "langchain/memory";
import { PlanetScaleChatMessageHistory } from "@langchain/community/stores/message/planetscale";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
import { Client } from "@planetscale/database";

// Create your own Planetscale database client
const client = new Client({
url: "ADD_YOURS_HERE", // Override with your own database instance's URL
});

const memory = new BufferMemory({
chatHistory: new PlanetScaleChatMessageHistory({
tableName: "stored_message",
sessionId: "lc-example",
client, // You can reuse your existing database client
}),
});

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 参考


此页面是否有帮助?


您也可以在 GitHub 上留下详细的反馈 在 GitHub 上.