Find answers from the community

Updated 11 months ago

Hello, is there a way to load into the

Hello, is there a way to load into the memory a summarized text of its contents? I tried this code:

from llama_index.llms.base import ChatMessage

Load the chat history from memory

chat_history = memory.get_all()
print("chat_history",chat_history)

Concatenate text from chat history

text_of_memory = " ".join([message.content for message in chat_history])
print("text_of_memory",text_of_memory)

Summarize the chat history

summary_response = await summarizer.aget_response("Summarize chat", [text_of_memory])
print("summary_response",summary_response)

Clear the memory

chat_engine.reset()

Create a ChatMessage with the summary

summary_message = ChatMessage(content=summary_response, role="system")
print("summary_message",summary_message)

Insert the summary message into the memory

memory.insert(summary_message)
print("memory",memory)

It works until the memory.insert:

chat_history [ChatMessage(role=<MessageRole.USER: 'user'>, content='hello', additional_kwargs={}), ChatMessage(role=<MessageRole.USER: 'user'>, content='hello', additional_kwargs={}), ChatMessage(role=<MessageRole.USER: 'user'>, content='hello', additional_kwargs={})]
text_of_memory hello hello hello
1 text chunks after repacking
summary_response The chat appears to consist of the repeated the word "hello" multiple times.
summary_message system: The chat appears to consist of the repeated phrase "como hacer la peticion" (how to make the request) multiple times.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-6d2d42d50f09> in <cell line: 19>()
17 print("summary_message",summary_message)
18 # Insert the summary message into the memory
---> 19 memory.insert(summary_message)
20 print("memory",memory)

AttributeError: 'ChatMemoryBuffer' object has no attribute 'insert'
L
1 comment
you probably want memory.put(summary_message)
Add a reply
Sign up and join the conversation on Discord