I’m creating a chatbot that is also context-aware using a vector retriever. This is accomplished using create_stuff_documents_chain, create_retrieval_chain, and RunnableWithMessageHistory.
I can get the chat history working with just the RedisChatMessageHistory function, but after a few interactions, the chat history becomes huge because it returns every single AI/Human interaction within the current conversation.
I had an idea that to pipe the RedisChatMessageHistory into the ConversationSummaryBufferMemory’s own chat_memory argument, and use the resultant output as the session history to keep it relatively short. I tried that with my code here, but it does not work (see error below):
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(
history_aware_retriever, question_answer_chain)
# Statefully manage chat history
def get_session_history(session_id: str) -> BaseChatMessageHistory:
history = RedisChatMessageHistory(
session_id,
url="redis://:" + settings.passwd + "@redis",
key_prefix=settings.prefix
)
return ConversationSummaryBufferMemory(
llm=llm,
memory_key="chat_history",
input_key="input",
max_token_limit=10,
return_messages=False,
chat_memory=history
)
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain,
get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer"
)
# Stream the result
for s in conversational_rag_chain.stream(
{"input": the_question},
config={
"configurable": {"session_id": conversation_session_id}
}
):
# print(s, flush=True)
if ("answer" in s):
yield s["answer"]
The error message:
AttributeError: ‘ConversationSummaryBufferMemory’ object has no attribute ‘messages’
If I remove the ConversationSummaryBufferMemory call and return the Redis message history byitself as below, then it all works great:
# Statefully manage chat history
def get_session_history(session_id: str) -> BaseChatMessageHistory:
history = RedisChatMessageHistory(
session_id,
url="redis://:" + settings.passwd+ "@redis",
key_prefix=settings.prefix
)
return history
Am I approaching this the wrong way? Is there a way to summarise the Redis chat history in a similar way to ConversationSummaryBufferMemory?
Thank you!