Skip to content

Commit

Permalink
optimizes DjangoChatMessageHistory add_messages by returning only the…
Browse files Browse the repository at this point in the history
… message ids
  • Loading branch information
filipeximenes committed Sep 11, 2024
1 parent e54dcd5 commit 93d5f74
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions django_ai_assistant/langchain/chat_message_histories.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def add_messages(self, messages: Sequence[BaseMessage]) -> None:
messages: A list of BaseMessage objects to store.
"""
with transaction.atomic():
existing_message_ids = [m.id for m in self.get_messages()]
existing_message_ids = [
str(i) for i in self._get_messages_qs().values_list("id", flat=True)
]

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

Expand All @@ -99,7 +101,9 @@ async def aadd_messages(self, messages: Sequence[BaseMessage]) -> None:
Args:
messages: A list of BaseMessage objects to store.
"""
existing_message_ids = [m.id for m in await self.aget_messages()]
existing_message_ids = [
str(i) async for i in self._get_messages_qs().values_list("id", flat=True)
]

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

Expand Down

0 comments on commit 93d5f74

Please sign in to comment.