Cassandra Chat Memory
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
- Create an Astra DB account.
- Create a vector enabled database.
- Download your secure connect bundle and application token on your database's "Connect" tab.
- Set up the following env vars:
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY_HERE
export CASSANDRA_SCB=YOUR_CASSANDRA_SCB_HERE
export CASSANDRA_TOKEN=YOUR_CASSANDRA_TOKEN_HERE
- Install the Cassandra Node.js driver.
- npm
- Yarn
- pnpm
npm install cassandra-driver
yarn add cassandra-driver
pnpm add cassandra-driver
Usage
import { BufferMemory } from "langchain/memory";
import { CassandraChatMessageHistory } from "langchain/stores/message/cassandra";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { ConversationChain } from "langchain/chains";
const memory = new BufferMemory({
chatHistory: new CassandraChatMessageHistory({
cloud: {
secureConnectBundle: "<path to your secure bundle>",
},
credentials: {
username: "token",
password: "<your Cassandra access token>",
},
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.call({ input: "Hi! I'm Jonathan." });
console.log({ res1 });
/*
{
res1: {
text: "Hello Jonathan! How can 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 Jonathan."
}
}
*/
API Reference:
- BufferMemory from
langchain/memory
- CassandraChatMessageHistory from
langchain/stores/message/cassandra
- ChatOpenAI from
langchain/chat_models/openai
- ConversationChain from
langchain/chains