Upstash Redis 支持的聊天记忆
由于 Upstash Redis 通过 REST API 工作,您可以将其与 Vercel Edge、Cloudflare Workers 和其他无服务器环境一起使用。基于 Redis 支持的聊天记忆。
为了在聊天会话之间实现更长期的持久性,您可以将默认的内存中 chatHistory
(支持 BufferMemory
等聊天记忆类)替换为 Upstash Redis 实例。
设置
您需要在您的项目中安装 @upstash/redis
提示
有关安装集成包的常规说明,请参阅 本节。
- 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 数据库。有关如何创建 HTTP 客户端的说明,请参阅 Upstash 文档。
用法
存储在 Redis 中的每个聊天历史会话都必须具有唯一的 ID。您可以提供一个可选的 sessionTTL
,以使会话在给定秒数后过期。config
参数直接传递到 @upstash/redis 的 new 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";
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 参考
- BufferMemory 来自
langchain/memory
- UpstashRedisChatMessageHistory 来自
@langchain/community/stores/message/upstash_redis
- ChatOpenAI 来自
@langchain/openai
- ConversationChain 来自
langchain/chains
高级用法
您也可以直接传递一个先前创建的 @upstash/redis 客户端实例
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 参考
- BufferMemory 来自
langchain/memory
- UpstashRedisChatMessageHistory 来自
@langchain/community/stores/message/upstash_redis
- ChatOpenAI 来自
@langchain/openai
- ConversationChain 来自
langchain/chains