-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
Conversation
(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.
for more information, see https://pre-commit.ci
There was a problem hiding this 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:
get_memory()
: returnsBaseChatHandler.memory
(notself.memory
)reset_memory()
: setsBaseChatHandler.memory = ConversationBufferWindowMemory(return_messages=self.llm.is_chat_provider, k=2)
clear_memory()
: callsBaseChatHandler.memory.clear()
Furthermore:
self.get_memory()
should be called bycreate_llm_chain()
indefault
and/ask
self.reset_memory()
should be called byBaseChatHandler.get_llm_chain()
right before callingself.create_llm_chain()
when switching LLMsself.clear_memory()
should be called by/clear
Also note:
- The
PROMPT_TEMPLATE
inask.py
should change{chat_history}
to{history}
under this proposal. This is because the memory object in/ask
definesmemory_key='chat_history'
instead of leaving it unset and defaulting tomemory_key='history'
.
Can you explore this implementation?
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. |
Addresses #616
clear.py
to clear chat history and display the help command again.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
![Screenshot 2024-06-19 at 8 47 27 AM](https://private-user-images.githubusercontent.com/29005/341140953-e9c53ab0-ef65-4648-b891-580c60353e18.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1Nzg5OTcsIm5iZiI6MTcyNDU3ODY5NywicGF0aCI6Ii8yOTAwNS8zNDExNDA5NTMtZTljNTNhYjAtZWY2NS00NjQ4LWI4OTEtNTgwYzYwMzUzZTE4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA5MzgxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPThiMTIzZmNjOTZkNmQ2YjU3YjIwMWVjM2ZjYTk4ZTVmZjlhZTVmZmEwNDgwYzBlOGZkZGE4ZTU0OGRiNjVlMDMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.jIg-ZqzCzZdJp2KM0Df61sCEPo_zEl0vq_q72CoCTFE)
k=2
.Then also check that
![image](https://private-user-images.githubusercontent.com/29005/341141455-14aa5db7-bac0-47c1-9fd9-fc80a7949d61.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1Nzg5OTcsIm5iZiI6MTcyNDU3ODY5NywicGF0aCI6Ii8yOTAwNS8zNDExNDE0NTUtMTRhYTVkYjctYmFjMC00N2MxLTlmZDktZmM4MGE3OTQ5ZDYxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA5MzgxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJmNTZiYTk1MTNhYjg5YWQzNzVkODY2NjAxMDhhZmVhMGMxOTcwYWVmNDliMDUyZmZhZjQwYWUxMTEzNTI0ZjEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.ZosQ98kJRj342bZDFB8iXh8V4MaWy0ZsTz7wpRGxClY)
/export
contains the entire question history:Next, use
![Screenshot 2024-06-19 at 8 49 19 AM](https://private-user-images.githubusercontent.com/29005/341141579-420adebf-f6ae-4d49-8324-ad1dc931eb90.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1Nzg5OTcsIm5iZiI6MTcyNDU3ODY5NywicGF0aCI6Ii8yOTAwNS8zNDExNDE1NzktNDIwYWRlYmYtZjZhZS00ZDQ5LTgzMjQtYWQxZGM5MzFlYjkwLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA5MzgxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTM5ZjYzNjFjODYyNjQxNzNjYzFjM2NmNDA4NzJhMjMzNGYyMzNhZDc0ODhjMjM2MDVhM2Q1ZjhjODk5MmViZTcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.Rm2AoBf89tC3A9gV2QsrxEtgP6o8n-q1_yeX-SZwdsk)
/clear
and see that the next question does not have any conversation memory:See the log message above:
Clear conversation memory, re-initializing the llm chain.
Then check that
![image](https://private-user-images.githubusercontent.com/29005/341141940-664d62cd-0c5d-473f-b834-603532e8c21c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1Nzg5OTcsIm5iZiI6MTcyNDU3ODY5NywicGF0aCI6Ii8yOTAwNS8zNDExNDE5NDAtNjY0ZDYyY2QtMGM1ZC00NzNmLWI4MzQtNjAzNTMyZThjMjFjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDA5MzgxN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQzNWYyODNiNWFjYzc4NWFiOGFjYzdjMzkxZjljMmRkYTM5YjQ0ZDZkYmI4MGIyOGYzOWY3MzdkMGNiM2U1OWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.fVno6N96JdfGJY_xlvYVvnbLldGjXC0uTC8Qyh9-L8Y)
/export
only shows the new entries in chat:This ensures that chat history and conversation memory are consistent.