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[patch]: update dynamodb chat history to update instead of overwrite #22397

Conversation

Davi-Schumacher
Copy link
Contributor

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.

Copy link

vercel bot commented Jun 2, 2024

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

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Nov 10, 2024 11:41pm

@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
@Davi-Schumacher Davi-Schumacher force-pushed the fix-dynamodb-chat-history-put-overwrite branch 3 times, most recently from fc4c447 to 98023e1 Compare November 3, 2024 20:58
@Davi-Schumacher
Copy link
Contributor Author

Hi @eyurtsev, can I get some guidance on what it would take to get this PR in shape for review/approval?

@Davi-Schumacher Davi-Schumacher force-pushed the fix-dynamodb-chat-history-put-overwrite branch from baa221b to df863f2 Compare November 10, 2024 23:35
…ead of put_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
@Davi-Schumacher Davi-Schumacher force-pushed the fix-dynamodb-chat-history-put-overwrite branch from df863f2 to ebe1485 Compare November 10, 2024 23:41
@eyurtsev
Copy link
Collaborator

@Davi-Schumacher looks good, could you confirm whether we need to escape any of the values to avoid sql injection?

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Dec 11, 2024
@eyurtsev eyurtsev changed the title community: update dynamodb chat history to update instead of overwrite community[patch]: update dynamodb chat history to update instead of overwrite Dec 11, 2024
@eyurtsev eyurtsev added the waiting-on-author PR Status: Confirmation from author is required label Dec 16, 2024
@eyurtsev eyurtsev merged commit 0f9b4bf into langchain-ai:master Dec 16, 2024
18 checks passed
@Davi-Schumacher Davi-Schumacher deleted the fix-dynamodb-chat-history-put-overwrite branch December 17, 2024 02:18
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 lgtm PR looks good. Use to confirm that a PR is ready for merging. size:L This PR changes 100-499 lines, ignoring generated files. waiting-on-author PR Status: Confirmation from author is required
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants