PlanetScale 聊天记忆
由于 PlanetScale 通过 REST API 工作,您可以将其与Vercel Edge、Cloudflare Workers 和其他无服务器环境一起使用。
为了在聊天会话之间实现长期持久性,您可以用 PlanetScale 数据库 实例替换默认的内存中 chatHistory
,它支持 BufferMemory
等聊天记忆类。
设置
您需要在项目中安装@planetscale/database
提示
- npm
- Yarn
- pnpm
npm install @langchain/openai @planetscale/database @langchain/community
yarn add @langchain/openai @planetscale/database @langchain/community
pnpm add @langchain/openai @planetscale/database @langchain/community
您还需要一个 PlanetScale 帐户和一个要连接的数据库。有关如何创建 HTTP 客户端的说明,请参见PlanetScale 文档.
用法
存储在 PlanetScale 数据库中的每个聊天历史会话必须具有一个唯一的 ID。config
参数直接传递给 @planetscale/database 的 new 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 参考
- BufferMemory 来自
langchain/memory
- PlanetScaleChatMessageHistory 来自
@langchain/community/stores/message/planetscale
- ChatOpenAI 来自
@langchain/openai
- ConversationChain 来自
langchain/chains
高级用法
您还可以直接传递之前创建的 @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 参考
- BufferMemory 来自
langchain/memory
- PlanetScaleChatMessageHistory 来自
@langchain/community/stores/message/planetscale
- ChatOpenAI 来自
@langchain/openai
- ConversationChain 来自
langchain/chains