Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community: update dynamodb chat history to update instead of overwrite #22397

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Davi-Schumacher
Copy link

Description:
The current implementation of DynamoDBChatMessageHistory updates the History attribute for a given chat history record by first extracting the existing contents into memory, appending the new message, and then using the put_item method to put the record back. This has the effect of overwriting any additional attributes someone may want to include in the record, like chat session metadata.

This PR suggests changing from using put_item to using update_item instead which will keep any other attributes in the record untouched. The change is backward compatible since

  1. update_item is an "upsert" operation, creating the record if it doesn't already exist, otherwise updating it
  2. It only touches the db insert call and passes the exact same information. The rest of the class is left untouched

Dependencies:
None

Tests and docs:
No unit tests currently exist for the DynamoDBChatMessageHistory class. This PR adds the file libs/community/tests/unit_tests/chat_message_histories/test_dynamodb_chat_message_history.py to test the add_message and clear methods. I wanted to use the moto library to mock DynamoDB calls but I could not get poetry to resolve it so I mocked those calls myself in the test. Therefore, no test dependencies were added.

The change was tested on a test DynamoDB table as well. The first three images below show the current behavior. First a message is added to chat history, then a value is inserted in the record in some other attribute, and finally another message is added to the record, destroying the other attribute.
using_put_1_first_message
using_put_2_add_attribute
using_put_3_second_message

The next three images show the new behavior. Once again a value is added to an attribute other than the History attribute, but now when the followup message is added it does not destroy that other attribute. The History attribute itself is unaffected by this change.
using_update_1_first_message
using_update_2_add_attribute
using_update_3_second_message

The doc located at docs/docs/integrations/memory/aws_dynamodb.ipynb required no changes and was tested as well.

…item

this allows someone to use the same chat session record to store additional attributes by updating the History attribute without overwriting the entire record
Copy link

vercel bot commented Jun 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Jun 2, 2024 4:51am

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔌: aws Primarily related to Amazon Web Services (AWS) integrations 🤖:improvement Medium size change to existing code to handle new use-cases labels Jun 2, 2024
@baskaryan baskaryan requested a review from eyurtsev June 3, 2024 22:06
@ccurme ccurme added the community Related to langchain-community label Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌: aws Primarily related to Amazon Web Services (AWS) integrations community Related to langchain-community 🤖:improvement Medium size change to existing code to handle new use-cases size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Status: Needs support
Development

Successfully merging this pull request may close these issues.

3 participants