-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
dynamodb.ts
196 lines (176 loc) Β· 5.11 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
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;
};
};
}
/**
* 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> = {};
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[]> {
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
.map((item) => ({
type: item.M?.type.S,
data: {
role: item.M?.role?.S,
content: item.M?.text.S,
},
}))
.filter(
(x): x is StoredMessage =>
x.type !== undefined && x.data.content !== undefined
);
return mapStoredMessagesToChatMessages(messages);
}
/**
* Deletes all messages from the DynamoDB table.
*/
async clear(): Promise<void> {
const params: DeleteItemCommandInput = {
TableName: this.tableName,
Key: this.dynamoKey,
};
await this.client.send(new DeleteItemCommand(params));
}
/**
* Adds a new message to the DynamoDB table.
* @param message The message to be added to the DynamoDB table.
*/
async addMessage(message: BaseMessage) {
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((message) => {
const dynamoSerializedMessage: DynamoDBSerializedChatMessage = {
M: {
type: {
S: message.type,
},
text: {
S: message.data.content,
},
},
};
if (message.data.role) {
dynamoSerializedMessage.M.role = { S: message.data.role };
}
return dynamoSerializedMessage;
}),
},
},
UpdateExpression:
"SET #m = list_append(if_not_exists(#m, :empty_list), :m)",
};
await this.client.send(new UpdateItemCommand(params));
}
}