Skip to content
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

PostgresChatMessageHistory message order isn't guaranteed #6829

Closed
2 of 14 tasks
mattflo opened this issue Jun 27, 2023 · 1 comment
Closed
2 of 14 tasks

PostgresChatMessageHistory message order isn't guaranteed #6829

mattflo opened this issue Jun 27, 2023 · 1 comment
Labels
01 bug Confirmed bug

Comments

@mattflo
Copy link
Contributor

mattflo commented Jun 27, 2023

System Info

Langchain version: 0.0.208
OS version: macOS 13.4
Python version: 3.10.12

Who can help?

@hwchase17

Information

  • The official example notebooks/scripts
  • My own modified scripts

Related Components

  • LLMs/Chat Models
  • Embedding Models
  • Prompts / Prompt Templates / Prompt Selectors
  • Output Parsers
  • Document Loaders
  • Vector Stores / Retrievers
  • Memory
  • Agents / Agent Executors
  • Tools / Toolkits
  • Chains
  • Callbacks/Tracing
  • Async

Reproduction

Queries without an order by clause aren't guaranteed to have any particular order.

Expected behavior

Chat history should be in order.

@dosubot
Copy link

dosubot bot commented Jun 27, 2023

Answer generated by a 🤖

Answer

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:

@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 ORDER BY id;"
    self.cursor.execute(query, (self.session_id,))
    items = [record["message"] for record in self.cursor.fetchall()]
    messages = messages_from_dict(items)
    return messages

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.

Sources


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.

dev2049 added a commit that referenced this issue Jun 30, 2023
Fixes issue: #6829

This guarantees message history is in the correct order. 

---------

Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
@mattflo mattflo closed this as completed Jun 30, 2023
vowelparrot pushed a commit that referenced this issue Jul 4, 2023
Fixes issue: #6829

This guarantees message history is in the correct order. 

---------

Co-authored-by: Dev 2049 <dev.dev2049@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

1 participant