Skip to main content

Momento支持的聊天记录

如果要在聊天会话中使用分布式、无服务器的持久性,可以使用Momento支持的聊天消息历史记录,即刻缓存,无需任何基础设施维护,无论是在本地构建还是在生产环境中都是一个很好的起点。

设置

在项目中安装Momento客户端库:

npm install @gomomento/sdk

您还需要从Momento获得API密钥。您可以在此处签署免费帐户这里

用法

为了区分一个聊天历史记录会话和另一个会话,我们需要一个唯一的“sessionId”。您还可以提供一个可选的“sessionTtl”,以使会话在给定的秒数后过期。

import {
CacheClient,
Configurations,
CredentialProvider,
} from "@gomomento/sdk";
import { BufferMemory } from "langchain/memory";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { ConversationChain } from "langchain/chains";
import { MomentoChatMessageHistory } from "langchain/stores/message/momento";

// See https://github.com/momentohq/client-sdk-javascript for connection options
const client = new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: "MOMENTO_AUTH_TOKEN",
}),
defaultTtlSeconds: 60 * 60 * 24,
});

// Create a unique session ID
const sessionId = new Date().toISOString();
const cacheName = "langchain";

const memory = new BufferMemory({
chatHistory: await MomentoChatMessageHistory.fromProps({
client,
cacheName,
sessionId,
sessionTtl: 300,
}),
});
console.log(
`cacheName=${cacheName} and sessionId=${sessionId} . This will be used to store the chat history. You can inspect the values at your Momento console at https://console.gomomento.com.`
);

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."
}
}
*/

// See the chat history in the Momento
console.log(await memory.chatHistory.getMessages());