Skip to main content

Upstash 基于 Redis 的聊天记忆

由于 Upstash Redis 通过 REST API 工作,所以您可以将其与 Vercel Edge[Cloudflare Workers] 一起使用(https://developers.cloudflare.com/workers/)和其他无服务器环境。 基于 Redis-Backed 聊天记忆。

为了在聊天会话之间实现长期持久性,您可以将支持聊天记忆类(如 BufferMemory)的默认内存中的 chatHistory 替换为 Upstash [Redis] 实例(https://redis.io/)。

设置

您需要在项目中安装 @upstash/redis

npm install @upstash/redis

您还需要一个 Upstash 帐户和一个 Redis 数据库进行连接。有关如何创建 HTTP 客户端的说明,请参见 Upstash Docs

使用

Redis 中存储的每个聊天历史记录会话必须具有唯一的 id。您可以提供可选的 sessionTTL,以使会话在确定的秒数后过期。 直接将 config 参数传递到 @upstash/redisnew Redis() 构造函数中,并使用相同的参数。

import { BufferMemory } from "langchain/memory";
import { UpstashRedisChatMessageHistory } from "langchain/stores/message/upstash_redis";
import { ChatOpenAI } from "langchain/chat_models/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({
modelName: "gpt-3.5-turbo",
temperature: 0,
});

const chain = new ConversationChain({ llm: model, memory });

const res1 = await chain.call({ 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.call({ input: "What did I just say my name was?" });
console.log({ res2 });

/*
{
res1: {
text: "You said your name was Jim."
}
}
*/

高级用法

您还可以直接传入先前创建的 @upstash/redis 客户端实例@#

import { Redis } from "@upstash/redis";
import { BufferMemory } from "langchain/memory";
import { UpstashRedisChatMessageHistory } from "langchain/stores/message/upstash_redis";
import { ChatOpenAI } from "langchain/chat_models/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({
modelName: "gpt-3.5-turbo",
temperature: 0,
});

const chain = new ConversationChain({ llm: model, memory });

const res1 = await chain.call({ 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.call({ input: "What did I just say my name was?" });
console.log({ res2 });

/*
{
res1: {
text: "You said your name was Jim."
}
}
*/