You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To ensure that the chat history is ordered, you can modify the messages property in the PostgresChatMessageHistory class to include an ORDER BY clause in the SQL query. You can order the messages by their id to maintain the order in which they were added to the database.
Here's the updated messages property:
@propertydefmessages(self) ->List[BaseMessage]: # type: ignore"""Retrieve the messages from PostgreSQL"""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"] forrecordinself.cursor.fetchall()]
messages=messages_from_dict(items)
returnmessages
This change will ensure that the chat history is returned in the order of the id column, which should maintain the order in which the messages were added.
This response is meant to be useful, save you time, and share context. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
System Info
Langchain version: 0.0.208
OS version: macOS 13.4
Python version: 3.10.12
Who can help?
@hwchase17
Information
Related Components
Reproduction
Queries without an
order by
clause aren't guaranteed to have any particular order.Expected behavior
Chat history should be in order.
The text was updated successfully, but these errors were encountered: