Skip to content

Commit

Permalink
community[patch]: history size support for DynamoDBChatMessageHistory (
Browse files Browse the repository at this point in the history
…#16794)

**Description:** PR adds support for limiting number of messages
preserved in a session history for DynamoDBChatMessageHistory

---------

Co-authored-by: Bagatur <[email protected]>
Co-authored-by: Bagatur <[email protected]>
  • Loading branch information
3 people authored and hinthornw committed Apr 26, 2024
1 parent 9582316 commit c68ac47
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class DynamoDBChatMessageHistory(BaseChatMessageHistory):
table. DynamoDB handles deletion of expired items without consuming
write throughput. To enable this feature on the table, follow the
[AWS DynamoDB documentation](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-how-to.html)
history_size: Maximum number of messages to store. If None then there is no
limit. If not None then only the latest `history_size` messages are stored.
"""

def __init__(
Expand All @@ -56,6 +58,7 @@ def __init__(
kms_key_id: Optional[str] = None,
ttl: Optional[int] = None,
ttl_key_name: str = "expireAt",
history_size: Optional[int] = None,
):
if boto3_session:
client = boto3_session.resource("dynamodb", endpoint_url=endpoint_url)
Expand All @@ -75,6 +78,7 @@ def __init__(
self.key: Dict = key or {primary_key_name: session_id}
self.ttl = ttl
self.ttl_key_name = ttl_key_name
self.history_size = history_size

if kms_key_id:
try:
Expand Down Expand Up @@ -149,6 +153,9 @@ def add_message(self, message: BaseMessage) -> None:
_message = message_to_dict(message)
messages.append(_message)

if self.history_size:
messages = messages[-self.history_size :]

try:
if self.ttl:
import time
Expand Down

0 comments on commit c68ac47

Please sign in to comment.