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

Clear both chat history and conversation memory when /clear is applied #842

Closed
wants to merge 2 commits into from

Conversation

srdas
Copy link
Collaborator

@srdas srdas commented Jun 19, 2024

Addresses #616

  1. Updated clear.py to clear chat history and display the help command again.
  2. Updated base.py to remove conversation memory when chat history is cleared.

Use chat to ask questions and verify that the logs show it is using conversation memory to the default depth of k=2.
Screenshot 2024-06-19 at 8 47 27 AM

Then also check that /export contains the entire question history:
image

Next, use /clear and see that the next question does not have any conversation memory:
Screenshot 2024-06-19 at 8 49 19 AM
See the log message above: Clear conversation memory, re-initializing the llm chain.

Then check that /export only shows the new entries in chat:
image

This ensures that chat history and conversation memory are consistent.

(1) Updated `clear.py` to clear chat history and display the help command again.
(2) Updated `base.py` to remove conversation memory when chat history is cleared.
@srdas srdas added the enhancement New feature or request label Jun 19, 2024
Copy link
Collaborator

@dlqqq dlqqq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srdas Great work on this, thank you! I agree that this solution works in the default case, since DefaultChatHandler.create_llm_chain() always creates a new instance of ConversationBufferWindowMemory every call.

However, I've noticed that the LangChain chat history is actually split between the default and /ask chat handlers. Here is an excerpt /ask that shows it using its own separate memory, which will not be cleared in general:

class AskChatHandler(BaseChatHandler):
    ...

    def create_llm_chain(
        self, provider: Type[BaseProvider], provider_params: Dict[str, str]
    ):
        unified_parameters = {
            **provider_params,
            **(self.get_model_parameters(provider, provider_params)),
        }
        self.llm = provider(**unified_parameters)
        memory = ConversationBufferWindowMemory(
            memory_key="chat_history", return_messages=True, k=2
        )

Right now, default and /ask track two separate chat histories (one with all messages & replies to/from default, the other to/from /ask).

Ideally there should only be one instance of ConversationBufferWindowMemory shared between all chat handlers. I think the best way to do this is to define the memory as a class attribute on BaseChatHandler. This effectively shares global state between all chat handlers.

Under this proposal, BaseChatHandler should define three new methods:

  1. get_memory(): returns BaseChatHandler.memory (not self.memory)
  2. reset_memory(): sets BaseChatHandler.memory = ConversationBufferWindowMemory(return_messages=self.llm.is_chat_provider, k=2)
  3. clear_memory(): calls BaseChatHandler.memory.clear()

Furthermore:

  1. self.get_memory() should be called by create_llm_chain() in default and /ask
  2. self.reset_memory() should be called by BaseChatHandler.get_llm_chain() right before calling self.create_llm_chain() when switching LLMs
  3. self.clear_memory() should be called by /clear

Also note:

  • The PROMPT_TEMPLATE in ask.py should change {chat_history} to {history} under this proposal. This is because the memory object in /ask defines memory_key='chat_history' instead of leaving it unset and defaulting to memory_key='history'.

Can you explore this implementation?

@srdas
Copy link
Collaborator Author

srdas commented Jun 19, 2024

Thanks @dlqqq @3coins for the comments, I will refactor all the code to address the expanded intended functionality, i.e., to clear both default and ask chat histories as well as the related conversation memories.

@srdas
Copy link
Collaborator Author

srdas commented Aug 6, 2024

I am closing this PR as the code base has changed since this was opened and it needs a fresh PR, as this one is outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants