-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Memory order out of Astra DB Chat Memory is incorrect #3101
Comments
To ensure that the prompt is ordered by time instead of database order when using Chat Memory with Astra DB in Langflow, you need to modify the retrieval logic to sort the messages by their timestamp. This can be done by adjusting the query or sorting the messages after retrieval. In the provided code, the from langchain_astradb.chat_message_histories import AstraDBChatMessageHistory
class AstraDBMessageWriterComponent(BaseMemoryComponent):
# ... existing code ...
def get_sorted_messages(self, session_id: str, collection_name: str, token: str, api_endpoint: str, namespace: Optional[str] = None):
memory = AstraDBChatMessageHistory(
session_id=session_id,
collection_name=collection_name,
token=token,
api_endpoint=api_endpoint,
namespace=namespace,
)
messages = memory.get_messages()
sorted_messages = sorted(messages, key=lambda msg: msg.timestamp)
return sorted_messages
# ... existing code ... In this example, the Additionally, in Langflow, you can configure the order of messages directly in the class MemoryComponent(Component):
display_name = "Chat Memory"
description = "Retrieves stored chat messages."
icon = "message-square-more"
inputs = [
# ... other inputs ...
DropdownInput(
name="order",
display_name="Order",
options=["Ascending", "Descending"],
value="Ascending",
info="Order of the messages.",
advanced=True,
),
# ... other inputs ...
]
def retrieve_messages(self) -> Data:
sender = self.sender
sender_name = self.sender_name
session_id = self.session_id
n_messages = self.n_messages
order = "DESC" if self.order == "Descending" else "ASC"
if sender == "Machine and User":
sender = None
messages = get_messages(
sender=sender,
sender_name=sender_name,
session_id=session_id,
limit=n_messages,
order=order,
)
self.status = messages
return messages By setting the In the Langflow configuration, you can see that the {
"data": {
"nodes": [
{
"data": {
"id": "Memory-rvcL5",
"node": {
"custom_fields": {
"order": {
"advanced": true,
"display_name": "Order",
"dynamic": false,
"info": "Order of the messages.",
"name": "order",
"options": [
"Ascending",
"Descending"
],
"placeholder": "",
"required": false,
"show": true,
"title_case": false,
"type": "str",
"value": "Ascending"
}
}
},
"type": "Memory"
}
}
]
}
} By configuring the Additionally, the def get_messages(
sender: Optional[str] = None,
sender_name: Optional[str] = None,
session_id: Optional[str] = None,
order_by: Optional[str] = "timestamp",
order: Optional[str] = "DESC",
limit: Optional[int] = None,
):
monitor_service = get_monitor_service()
messages_df = monitor_service.get_messages(
sender=sender,
sender_name=sender_name,
session_id=session_id,
order_by=order_by,
limit=limit,
order=order,
)
messages: list[Message] = []
messages_df = messages_df[::-1] if order == "DESC" else messages_df
for row in messages_df.itertuples():
msg = Message(
text=row.text,
sender=row.sender,
session_id=row.session_id,
sender_name=row.sender_name,
timestamp=row.timestamp,
)
messages.append(msg)
return messages This function retrieves messages from the monitor service and orders them by the specified field, which defaults to "timestamp". By setting the |
Fixed with #3170 |
Bug Description
I have injected Chat Memory into a Prompt Context, with the Chat Memory connected to Astra DB Chat Memory as external memory. My chat order is (with timestamps):
The prompt (captured via LangSmith) is:
Which is clearly not in the right order. It is in database order (
select * from ...
):The ordering is by token (as the table is partitioned by
_id
with no clustering, and each message is in its own partition).Reproduction
default_keyspace.memories
. Ifselect * from
shows the correct order, truncate the table and re-run the flow. Eventually the order in the database will not match conversation orderExpected behavior
The prompt should be ordered by time.
Who can help?
No response
Operating System
Langflow Docker Container
Langflow Version
1.0.14
Python Version
3.12
Screenshot
No response
Flow File
https://raw.githubusercontent.com/DataStax-Examples/langflow-demo-prod/7c231fcfbfd2fb52fbec1b78da9c7fc0e31b6e5e/flows/AstraMemoryChatbot.json
The text was updated successfully, but these errors were encountered: