Skip to main content

Cassandra 聊天内存

¥Cassandra Chat Memory

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

¥For longer-term persistence across chat sessions, you can swap out the default in-memory chatHistory that backs chat memory classes like BufferMemory for a Cassandra cluster.

设置

¥Setup

首先,安装 Cassandra Node.js 驱动程序:

¥First, install the Cassandra Node.js driver:

npm install cassandra-driver @langchain/openai @langchain/community @langchain/core

根据数据库提供商的不同,连接数据库的具体方法会有所不同。我们将创建一个文档 configConnection,它将用作向量存储配置的一部分。

¥Depending on your database providers, the specifics of how to connect to the database will vary. We will create a document configConnection which will be used as part of the vector store configuration.

Apache Cassandra ®

const configConnection = {
contactPoints: ['h1', 'h2'],
localDataCenter: 'datacenter1',
credentials: {
username: <...> as string,
password: <...> as string,
},
};

Astra 数据库

¥Astra DB

Astra DB 是一个云原生的 Cassandra 即服务平台。

¥Astra DB is a cloud-native Cassandra-as-a-Service platform.

  1. 创建一个 Astra DB 账户

    ¥Create an Astra DB account.

  2. 创建一个 支持向量的数据库

    ¥Create a vector enabled database.

  3. 为你的数据库创建一个 token

    ¥Create a token for your database.

const configConnection = {
serviceProviderArgs: {
astra: {
token: <...> as string,
endpoint: <...> as string,
},
},
};

你可以提供属性 datacenterID: 和可选的 regionName:,而不是 endpoint:

¥Instead of endpoint:, you many provide property datacenterID: and optionally regionName:.

用法

¥Usage

import { BufferMemory } from "langchain/memory";
import { CassandraChatMessageHistory } from "@langchain/community/stores/message/cassandra";
import { ChatOpenAI } from "@langchain/openai";
import { ConversationChain } from "langchain/chains";

// The example below uses Astra DB, but you can use any Cassandra connection
const configConnection = {
serviceProviderArgs: {
astra: {
token: "<your Astra Token>" as string,
endpoint: "<your Astra Endpoint>" as string,
},
},
};

const memory = new BufferMemory({
chatHistory: new CassandraChatMessageHistory({
...configConnection,
keyspace: "langchain",
table: "message_history",
sessionId: "<some unique session identifier>",
}),
});

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

const res1 = await chain.invoke({ input: "Hi! I'm Jonathan." });
console.log({ res1 });
/*
{
res1: {
text: "Hello Jonathan! How can 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 Jonathan."
}
}
*/

API Reference: