Order messages in PostgresChatMessageHistory (#6830)

Fixes issue: https://github.com/hwchase17/langchain/issues/6829

This guarantees message history is in the correct order. 

---------

Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
pull/6949/head
Matt Florence 1 year ago committed by GitHub
parent e9950392dd
commit 0f6737735d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,7 +49,9 @@ class PostgresChatMessageHistory(BaseChatMessageHistory):
@property
def messages(self) -> List[BaseMessage]: # type: ignore
"""Retrieve the messages from PostgreSQL"""
query = f"SELECT message FROM {self.table_name} WHERE session_id = %s;"
query = (
f"SELECT message FROM {self.table_name} WHERE session_id = %s ORDER BY id;"
)
self.cursor.execute(query, (self.session_id,))
items = [record["message"] for record in self.cursor.fetchall()]
messages = messages_from_dict(items)

Loading…
Cancel
Save