Skip to main content

PlanetScale 聊天内存

¥PlanetScale Chat Memory

由于 PlanetScale 通过 REST API 工作,因此你可以将其用于 Vercel EdgeCloudflare Workers 和其他无服务器环境。

¥Because PlanetScale works via a REST API, you can use this with Vercel Edge, Cloudflare Workers and other Serverless environments.

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

¥For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory that backs chat memory classes like BufferMemory for an PlanetScale Database instance.

设置

¥Setup

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

¥You will need to install @planetscale/database in your project:

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

你还需要一个 PlanetScale 账户和一个用于连接的数据库。请参阅 PlanetScale 文档 上的说明,了解如何创建 HTTP 客户端。

¥You will also need an PlanetScale Account and a database to connect to. See instructions on PlanetScale Docs on how to create a HTTP client.

用法

¥Usage

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

¥Each chat history session stored in PlanetScale database must have a unique id. The config parameter is passed directly into the new Client() constructor of @planetscale/database, and takes all the same arguments.

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 Reference:

高级用法

¥Advanced Usage

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

¥You can also directly pass in a previously created @planetscale/database client instance:

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 Reference: