-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathdynamodb_chat_storage.py
More file actions
267 lines (232 loc) · 10.3 KB
/
Copy pathdynamodb_chat_storage.py
File metadata and controls
267 lines (232 loc) · 10.3 KB
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
from typing import Union, Optional
import json
import time
import boto3
from agent_squad.storage import ChatStorage
from agent_squad.types import ConversationMessage, ParticipantRole, TimestampedMessage
from agent_squad.utils import Logger, conversation_to_dict
from operator import attrgetter
from agent_squad.shared import user_agent
class DynamoDbChatStorage(ChatStorage):
def __init__(self,
table_name: str,
region: str,
ttl_key: Optional[str] = None,
ttl_duration: int = 3600,
content_filters: Optional[list[tuple[str, str]]] = None):
super().__init__()
self.table_name = table_name
self.ttl_key = ttl_key
self.ttl_duration = int(ttl_duration)
self.content_filters = content_filters or []
self.dynamodb = boto3.resource('dynamodb', region_name=region)
self.table = self.dynamodb.Table(table_name)
user_agent.register_feature_to_resource(self.dynamodb, feature='storage-ddb')
def _apply_content_filters(self, content: list) -> list:
if not self.content_filters:
return content
serialized = json.dumps(content)
for original, placeholder in self.content_filters:
serialized = serialized.replace(original, placeholder)
return json.loads(serialized)
def _reverse_content_filters(self, content: list) -> list:
if not self.content_filters:
return content
serialized = json.dumps(content)
for original, placeholder in self.content_filters:
serialized = serialized.replace(placeholder, original)
return json.loads(serialized)
async def _fetch_raw(
self,
user_id: str,
session_id: str,
agent_id: str
) -> list[TimestampedMessage]:
key = self._generate_key(user_id, session_id, agent_id)
try:
response = self.table.get_item(Key={'PK': user_id, 'SK': key})
return self._dict_to_conversation(
response.get('Item', {}).get('conversation', [])
)
except Exception as error:
Logger.error(f"Error getting conversation from DynamoDB: {str(error)}")
raise error
async def save_chat_message(
self,
user_id: str,
session_id: str,
agent_id: str,
new_message: Union[ConversationMessage, TimestampedMessage],
max_history_size: Optional[int] = None
) -> list[ConversationMessage]:
key = self._generate_key(user_id, session_id, agent_id)
existing_conversation = await self._fetch_raw(user_id, session_id, agent_id)
if self.is_same_role_as_last_message(existing_conversation, new_message):
Logger.debug(f"> Consecutive {new_message.role} \
message detected for agent {agent_id}. Not saving.")
return existing_conversation
if isinstance(new_message, ConversationMessage):
new_message = TimestampedMessage(
role=new_message.role,
content=self._apply_content_filters(new_message.content))
elif self.content_filters:
new_message = TimestampedMessage(
role=new_message.role,
content=self._apply_content_filters(new_message.content),
timestamp=new_message.timestamp)
existing_conversation.append(new_message)
trimmed_conversation: list[TimestampedMessage] = self.trim_conversation(
existing_conversation,
max_history_size
)
item: dict[str, Union[str, list[TimestampedMessage], int]] = {
'PK': user_id,
'SK': key,
'conversation': conversation_to_dict(trimmed_conversation),
}
if self.ttl_key:
item[self.ttl_key] = int(time.time()) + self.ttl_duration
try:
self.table.put_item(Item=item)
except Exception as error:
Logger.error(f"Error saving conversation to DynamoDB:{str(error)}")
raise error
return [ConversationMessage(
role=msg.role,
content=self._reverse_content_filters(msg.content)
) for msg in trimmed_conversation]
async def save_chat_messages(self,
user_id: str,
session_id: str,
agent_id: str,
new_messages: Union[list[ConversationMessage], list[TimestampedMessage]],
max_history_size: Optional[int] = None
) -> list[ConversationMessage]:
"""
Save multiple messages at once
"""
key = self._generate_key(user_id, session_id, agent_id)
existing_conversation = await self._fetch_raw(user_id, session_id, agent_id)
#TODO: check messages are consecutive
# if self.is_same_role_as_last_message(existing_conversation, new_messages):
# Logger.debug(f"> Consecutive {new_message.role} \
# message detected for agent {agent_id}. Not saving.")
# return existing_conversation
if isinstance(new_messages[0], ConversationMessage): # Check only first message
new_messages = [
TimestampedMessage(
role=new_message.role,
content=self._apply_content_filters(new_message.content)
)
for new_message in new_messages]
elif self.content_filters:
new_messages = [
TimestampedMessage(
role=new_message.role,
content=self._apply_content_filters(new_message.content),
timestamp=new_message.timestamp
)
for new_message in new_messages]
existing_conversation.extend(new_messages)
trimmed_conversation: list[TimestampedMessage] = self.trim_conversation(
existing_conversation,
max_history_size
)
item: dict[str, str | list[TimestampedMessage] | int] = {
'PK': user_id,
'SK': key,
'conversation': conversation_to_dict(trimmed_conversation),
}
if self.ttl_key:
item[self.ttl_key] = int(time.time()) + self.ttl_duration
try:
self.table.put_item(Item=item)
except Exception as error:
Logger.error(f"Error saving conversation to DynamoDB:{str(error)}")
raise error
return [ConversationMessage(
role=msg.role,
content=self._reverse_content_filters(msg.content)
) for msg in trimmed_conversation]
async def fetch_chat(
self,
user_id: str,
session_id: str,
agent_id: str
) -> list[ConversationMessage]:
try:
stored_messages = await self._fetch_raw(user_id, session_id, agent_id)
return [ConversationMessage(
role=msg.role,
content=self._reverse_content_filters(msg.content)
) for msg in stored_messages]
except Exception as error:
Logger.error(f"Error getting conversation from DynamoDB:{str(error)}")
raise error
async def fetch_chat_with_timestamp(
self,
user_id: str,
session_id: str,
agent_id: str
) -> list[TimestampedMessage]:
try:
stored_messages = await self._fetch_raw(user_id, session_id, agent_id)
if not self.content_filters:
return stored_messages
return [TimestampedMessage(
role=msg.role,
content=self._reverse_content_filters(msg.content),
timestamp=msg.timestamp
) for msg in stored_messages]
except Exception as error:
Logger.error(f"Error getting conversation from DynamoDB: {str(error)}")
raise error
async def fetch_all_chats(self, user_id: str, session_id: str) -> list[ConversationMessage]:
try:
response = self.table.query(
KeyConditionExpression="PK = :pk AND begins_with(SK, :skPrefix)",
ExpressionAttributeValues={
':pk': user_id,
':skPrefix': f"{session_id}#"
}
)
if not response.get('Items'):
return []
all_chats = []
for item in response['Items']:
if not isinstance(item.get('conversation'), list):
Logger.error(f"Unexpected item structure:{item}")
continue
agent_id = item['SK'].split('#')[1]
for msg in item['conversation']:
content = self._reverse_content_filters(msg['content']) \
if isinstance(msg['content'], list) else msg['content']
if msg['role'] == ParticipantRole.ASSISTANT.value:
text = content[0]['text'] if isinstance(content, list) else content
content = [{'text': f"[{agent_id}] {text}"}]
elif not isinstance(content, list):
content = [{'text': content}]
all_chats.append(
TimestampedMessage(
role=msg['role'],
content=content,
timestamp=int(msg['timestamp'])
))
all_chats.sort(key=attrgetter('timestamp'))
return self._remove_timestamps(all_chats)
except Exception as error:
Logger.error(f"Error querying conversations from DynamoDB:{str(error)}")
raise error
def _generate_key(self, user_id: str, session_id: str, agent_id: str) -> str:
return f"{session_id}#{agent_id}"
def _remove_timestamps(self,
messages: list[Union[TimestampedMessage]]) -> list[ConversationMessage]:
return [ConversationMessage(role=message.role,
content=message.content
) for message in messages]
def _dict_to_conversation(self,
messages: list[dict]) -> list[TimestampedMessage]:
return [TimestampedMessage(role=msg['role'],
content=msg['content'],
timestamp=msg['timestamp']
) for msg in messages]