-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
dynamodb.ts
290 lines (262 loc) Β· 7.9 KB
/
dynamodb.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import {
DynamoDBClient,
DynamoDBClientConfig,
GetItemCommand,
GetItemCommandInput,
UpdateItemCommand,
UpdateItemCommandInput,
DeleteItemCommand,
DeleteItemCommandInput,
AttributeValue,
} from "@aws-sdk/client-dynamodb";
import { BaseListChatMessageHistory } from "@langchain/core/chat_history";
import {
BaseMessage,
StoredMessage,
mapChatMessagesToStoredMessages,
mapStoredMessagesToChatMessages,
} from "@langchain/core/messages";
/**
* Interface defining the fields required to create an instance of
* `DynamoDBChatMessageHistory`. It includes the DynamoDB table name,
* session ID, partition key, sort key, message attribute name, and
* DynamoDB client configuration.
*/
export interface DynamoDBChatMessageHistoryFields {
tableName: string;
sessionId: string;
partitionKey?: string;
sortKey?: string;
messageAttributeName?: string;
config?: DynamoDBClientConfig;
key?: Record<string, AttributeValue>;
}
/**
* Interface defining the structure of a chat message as it is stored in
* DynamoDB.
*/
interface DynamoDBSerializedChatMessage {
M: {
type: {
S: string;
};
text: {
S: string;
};
role?: {
S: string;
};
additional_kwargs?: {
S: string;
};
};
}
/**
* Class providing methods to interact with a DynamoDB table to store and
* retrieve chat messages. It extends the `BaseListChatMessageHistory`
* class.
*/
export class DynamoDBChatMessageHistory extends BaseListChatMessageHistory {
lc_namespace = ["langchain", "stores", "message", "dynamodb"];
get lc_secrets(): { [key: string]: string } | undefined {
return {
"config.credentials.accessKeyId": "AWS_ACCESS_KEY_ID",
"config.credentials.secretAccessKey": "AWS_SECRETE_ACCESS_KEY",
"config.credentials.sessionToken": "AWS_SESSION_TOKEN",
};
}
private tableName: string;
private sessionId: string;
private client: DynamoDBClient;
private partitionKey = "id";
private sortKey?: string;
private messageAttributeName = "messages";
private dynamoKey: Record<string, AttributeValue> = {};
/**
* Transforms a `StoredMessage` into a `DynamoDBSerializedChatMessage`.
* The `DynamoDBSerializedChatMessage` format is suitable for storing in DynamoDB.
*
* @param message - The `StoredMessage` to be transformed.
* @returns The transformed `DynamoDBSerializedChatMessage`.
*/
private createDynamoDBSerializedChatMessage(
message: StoredMessage
): DynamoDBSerializedChatMessage {
const {
type,
data: { content, role, additional_kwargs },
} = message;
const isAdditionalKwargs =
additional_kwargs && Object.keys(additional_kwargs).length;
const dynamoSerializedMessage: DynamoDBSerializedChatMessage = {
M: {
type: {
S: type,
},
text: {
S: content,
},
additional_kwargs: isAdditionalKwargs
? { S: JSON.stringify(additional_kwargs) }
: { S: "{}" },
},
};
if (role) {
dynamoSerializedMessage.M.role = { S: role };
}
return dynamoSerializedMessage;
}
constructor({
tableName,
sessionId,
partitionKey,
sortKey,
messageAttributeName,
config,
key = {},
}: DynamoDBChatMessageHistoryFields) {
super();
this.tableName = tableName;
this.sessionId = sessionId;
this.client = new DynamoDBClient(config ?? {});
this.partitionKey = partitionKey ?? this.partitionKey;
this.sortKey = sortKey;
this.messageAttributeName =
messageAttributeName ?? this.messageAttributeName;
this.dynamoKey = key;
// override dynamoKey with partition key and sort key when key not specified
if (Object.keys(this.dynamoKey).length === 0) {
this.dynamoKey[this.partitionKey] = { S: this.sessionId };
if (this.sortKey) {
this.dynamoKey[this.sortKey] = { S: this.sortKey };
}
}
}
/**
* Retrieves all messages from the DynamoDB table and returns them as an
* array of `BaseMessage` instances.
* @returns Array of stored messages
*/
async getMessages(): Promise<BaseMessage[]> {
try {
const params: GetItemCommandInput = {
TableName: this.tableName,
Key: this.dynamoKey,
};
const response = await this.client.send(new GetItemCommand(params));
const items = response.Item
? response.Item[this.messageAttributeName]?.L ?? []
: [];
const messages = items
.filter(
(
item
): item is AttributeValue & { M: DynamoDBSerializedChatMessage } =>
item.M !== undefined
)
.map((item) => {
const data: {
role?: string;
content: string | undefined;
additional_kwargs?: Record<string, unknown>;
} = {
role: item.M?.role?.S,
content: item.M?.text.S,
additional_kwargs: item.M?.additional_kwargs?.S
? JSON.parse(item.M?.additional_kwargs.S)
: undefined,
};
return {
type: item.M?.type.S,
data,
};
})
.filter(
(x): x is StoredMessage =>
x.type !== undefined && x.data.content !== undefined
);
return mapStoredMessagesToChatMessages(messages);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
throw new Error(`Error getting messages: ${error.message}`);
}
}
/**
* Adds a new message to the DynamoDB table.
* @param message The message to be added to the DynamoDB table.
*/
async addMessage(message: BaseMessage) {
try {
const messages = mapChatMessagesToStoredMessages([message]);
const params: UpdateItemCommandInput = {
TableName: this.tableName,
Key: this.dynamoKey,
ExpressionAttributeNames: {
"#m": this.messageAttributeName,
},
ExpressionAttributeValues: {
":empty_list": {
L: [],
},
":m": {
L: messages.map(this.createDynamoDBSerializedChatMessage),
},
},
UpdateExpression:
"SET #m = list_append(if_not_exists(#m, :empty_list), :m)",
};
await this.client.send(new UpdateItemCommand(params));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
throw new Error(`Error adding message: ${error.message}`);
}
}
/**
* Adds new messages to the DynamoDB table.
* @param messages The messages to be added to the DynamoDB table.
*/
async addMessages(messages: BaseMessage[]): Promise<void> {
try {
const storedMessages = mapChatMessagesToStoredMessages(messages);
const dynamoMessages = storedMessages.map(
this.createDynamoDBSerializedChatMessage
);
const params: UpdateItemCommandInput = {
TableName: this.tableName,
Key: this.dynamoKey,
ExpressionAttributeNames: {
"#m": this.messageAttributeName,
},
ExpressionAttributeValues: {
":empty_list": {
L: [],
},
":m": {
L: dynamoMessages,
},
},
UpdateExpression:
"SET #m = list_append(if_not_exists(#m, :empty_list), :m)",
};
await this.client.send(new UpdateItemCommand(params));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
throw new Error(`Error adding messages: ${error.message}`);
}
}
/**
* Deletes all messages from the DynamoDB table.
*/
async clear(): Promise<void> {
try {
const params: DeleteItemCommandInput = {
TableName: this.tableName,
Key: this.dynamoKey,
};
await this.client.send(new DeleteItemCommand(params));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
throw new Error(`Error clearing messages: ${error.message}`);
}
}
}