Upstash Redis 支持的聊天内存
¥Upstash Redis-Backed Chat Memory
由于 Upstash Redis 通过 REST API 工作,因此你可以将其用于 Vercel Edge、Cloudflare Workers 和其他无服务器环境。基于 Redis 支持的聊天内存。
¥Because Upstash Redis works via a REST API, you can use this with Vercel Edge, Cloudflare Workers and other Serverless environments. Based on Redis-Backed Chat Memory.
为了在聊天会话中实现更长期的持久化,你可以将支持聊天内存类(如 BufferMemory)的默认内存 chatHistory 替换为 Upstash Redis 实例。
¥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 Upstash Redis instance.
设置
¥Setup
你需要在你的项目中安装 @upstash/redis:
¥You will need to install @upstash/redis in your project:
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core @upstash/redis
yarn add @langchain/openai @langchain/community @langchain/core @upstash/redis
pnpm add @langchain/openai @langchain/community @langchain/core @upstash/redis
你还需要一个 Upstash 账户和一个用于连接的 Redis 数据库。请参阅 Upstash 文档 上的说明,了解如何创建 HTTP 客户端。
¥You will also need an Upstash Account and a Redis database to connect to. See instructions on Upstash Docs on how to create a HTTP client.
用法
¥Usage
存储在 Redis 中的每个聊天历史记录会话都必须具有唯一的 ID。你可以提供可选的 sessionTTL,使会话在指定秒数后过期。config 参数直接传递给 @upstash/redis 的 new Redis() 构造函数,并接受所有相同的参数。
¥Each chat history session stored in Redis must have a unique id. You can provide an optional sessionTTL to make sessions expire after a give number of seconds.
The config parameter is passed directly into the new Redis() constructor of @upstash/redis, and takes all the same arguments.
import { BufferMemory } from "langchain/memory";
import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
const memory = new BufferMemory({
chatHistory: new UpstashRedisChatMessageHistory({
sessionId: new Date().toISOString(), // Or some other unique identifier for the conversation
sessionTTL: 300, // 5 minutes, omit this parameter to make sessions never expire
config: {
url: "https://ADD_YOURS_HERE.upstash.io", // Override with your own instance's URL
token: "********", // Override with your own instance's token
},
}),
});
const model = new ChatOpenAI({
model: "gpt-3.5-turbo",
temperature: 0,
});
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:
- BufferMemory from
langchain/memory - UpstashRedisChatMessageHistory from
@langchain/community/stores/message/upstash_redis - ChatOpenAI from
@langchain/openai - ConversationChain from
langchain/chains
高级用法
¥Advanced Usage
你还可以直接传入之前创建的 @upstash/redis 客户端实例:
¥You can also directly pass in a previously created @upstash/redis client instance:
import { Redis } from "@upstash/redis";
import { BufferMemory } from "langchain/memory";
import { UpstashRedisChatMessageHistory } from "@langchain/community/stores/message/upstash_redis";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";
// Create your own Redis client
const client = new Redis({
url: "https://ADD_YOURS_HERE.upstash.io",
token: "********",
});
const memory = new BufferMemory({
chatHistory: new UpstashRedisChatMessageHistory({
sessionId: new Date().toISOString(),
sessionTTL: 300,
client, // You can reuse your existing Redis client
}),
});
const model = new ChatOpenAI({
model: "gpt-3.5-turbo",
temperature: 0,
});
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:
- BufferMemory from
langchain/memory - UpstashRedisChatMessageHistory from
@langchain/community/stores/message/upstash_redis - ChatOpenAI from
@langchain/openai - ConversationChain from
langchain/chains