Skip to main content

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

  1. Create an Astra DB account.
  2. Create a vector enabled database.
  3. Download your secure connect bundle and application token on your database's "Connect" tab.
  4. 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
  1. Install the Cassandra Node.js driver.
npm install 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: