-
Notifications
You must be signed in to change notification settings - Fork 2k
/
cassandra-store.ts
47 lines (41 loc) · 1.13 KB
/
cassandra-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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."
}
}
*/