Skip to content

Commit

Permalink
uses messages ids to check for the existing messages in DjangoChatMes…
Browse files Browse the repository at this point in the history
…sageHistory
  • Loading branch information
filipeximenes committed Sep 11, 2024
1 parent 2ed100f commit e54dcd5
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions django_ai_assistant/langchain/chat_message_histories.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def add_messages(self, messages: Sequence[BaseMessage]) -> None:
messages: A list of BaseMessage objects to store.
"""
with transaction.atomic():
existing_messages = self.get_messages()
existing_message_ids = [m.id for m in self.get_messages()]

messages_to_create = [m for m in messages if m not in existing_messages]
messages_to_create = [m for m in messages if m.id not in existing_message_ids]

created_messages = Message.objects.bulk_create(
[Message(thread_id=self._thread_id, message=dict()) for _ in messages_to_create]
Expand All @@ -99,9 +99,9 @@ async def aadd_messages(self, messages: Sequence[BaseMessage]) -> None:
Args:
messages: A list of BaseMessage objects to store.
"""
existing_messages = await self.aget_messages()
existing_message_ids = [m.id for m in await self.aget_messages()]

messages_to_create = [m for m in messages if m not in existing_messages]
messages_to_create = [m for m in messages if m.id not in existing_message_ids]

# NOTE: This method does not use transactions because it do not yet work in async mode.
# Source: https://docs.djangoproject.com/en/5.0/topics/async/#queries-the-orm
Expand Down

0 comments on commit e54dcd5

Please sign in to comment.