-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
astradb.ts
121 lines (106 loc) Β· 3.28 KB
/
astradb.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
BaseMessage,
StoredMessage,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "@langchain/core/messages";
import { DataAPIClient, Collection } from "@datastax/astra-db-ts";
export interface AstraDBChatMessageHistoryInput {
token: string;
endpoint: string;
collectionName: string;
namespace?: string;
sessionId: string;
}
export interface AstraDBChatMessageHistoryProps {
collection: Collection;
sessionId: string;
}
/**
* Class for storing chat message history with Astra DB. It extends the
* BaseListChatMessageHistory class and provides methods to get, add, and
* clear messages.
* @example
*
* ```typescript
* const client = new AstraDB(
* process.env.ASTRA_DB_APPLICATION_TOKEN,
* process.env.ASTRA_DB_ENDPOINT,
* process.env.ASTRA_DB_NAMESPACE
* );
*
* const collection = await client.collection("test_chat");
*
* const chatHistory = new AstraDBChatMessageHistory({
* collection,
* sessionId: "YOUR_SESSION_ID",
* });
*
* const messages = await chatHistory.getMessages();
*
* await chatHistory.clear();
*/
export class AstraDBChatMessageHistory extends BaseListChatMessageHistory {
lc_namespace = ["langchain", "stores", "message", "astradb"];
private sessionId: string;
private collection: Collection;
constructor({ collection, sessionId }: AstraDBChatMessageHistoryProps) {
super();
this.sessionId = sessionId;
this.collection = collection;
}
/**
* async initializer function to return a new instance of AstraDBChatMessageHistory in a single step
* @param AstraDBChatMessageHistoryInput
* @returns Promise<AstraDBChatMessageHistory>
*
* @example
* const chatHistory = await AstraDBChatMessageHistory.initialize({
* token: process.env.ASTRA_DB_APPLICATION_TOKEN,
* endpoint: process.env.ASTRA_DB_ENDPOINT,
* namespace: process.env.ASTRA_DB_NAMESPACE,
* collectionName:"test_chat",
* sessionId: "YOUR_SESSION_ID"
* });
*/
static async initialize({
token,
endpoint,
collectionName,
namespace,
sessionId,
}: AstraDBChatMessageHistoryInput): Promise<AstraDBChatMessageHistory> {
const client = new DataAPIClient(token, { caller: ["langchainjs"] });
const db = client.db(endpoint, { namespace });
const collection = await db.collection(collectionName);
return new AstraDBChatMessageHistory({ collection, sessionId });
}
async getMessages(): Promise<BaseMessage[]> {
const docs = this.collection.find({
sessionId: this.sessionId,
});
const docsArray = await docs.toArray();
const sortedDocs = docsArray.sort((a, b) => a.timestamp - b.timestamp);
const storedMessages: StoredMessage[] = sortedDocs.map((doc) => ({
type: doc.type,
data: doc.data,
}));
return mapStoredMessagesToChatMessages(storedMessages);
}
async addMessage(message: BaseMessage): Promise<void> {
const messages = mapChatMessagesToStoredMessages([message]);
const { type, data } = messages[0];
await this.collection.insertOne({
sessionId: this.sessionId,
timestamp: Date.now(),
type,
data,
});
}
async clear(): Promise<void> {
await this.collection.deleteMany({
sessionId: this.sessionId,
});
}
}