run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
34.72k stars 4.9k forks source link

[Question]: Does agent creation reset memory? #13312

Closed risos8200 closed 2 weeks ago

risos8200 commented 3 months ago

Question Validation

Question

I am trying to dynamically create agents with memory based on user key on session creation from storage. But it clears the memory of the past conversation as soon as the agent is created? Any way to avoid that?

def initialize_agent(user_key):
        chat_store= SimpleChatStore.from_persist_path("./chat_store.json")
        memory = ChatMemoryBuffer.from_defaults(
        token_limit=3000,
        chat_store=chat_store,
        chat_store_key=user_key,
        )
        print('before agent',memory) # for debugging: past memory available here
        agent = ReActAgent.from_tools(my_tool_list, llm=llm, memory=memory, verbose=True)
        agent.update_prompts({"agent_worker:system_prompt": react_system_prompt})
        agent.reset()
        print('after agent',memory) # for debugging: past memory erased here
dosubot[bot] commented 3 months ago

To avoid the memory reset when dynamically creating agents, the key is to adjust how you use the reset method on the ReActAgent. The memory is cleared because the reset method, by default, clears the agent's memory. If you want to keep the memory intact, you should modify the reset method in your agent's implementation to include a parameter that controls whether to clear the memory.

Here's a quick guide on how to do it:

  1. Modify the reset method in the ReActAgent class to accept a clear_memory boolean parameter. By default, set this to True. When clear_memory is False, the method should skip the memory clearing step.

  2. Update your initialize_agent function to call reset with clear_memory=False when you want to preserve the memory after agent creation.

Example modification for the reset method:

def reset(self, clear_memory: bool = True):
    if clear_memory:
        self._memory.reset()

And then in your initialize_agent function:

agent.reset(clear_memory=False)

This approach allows you to dynamically create agents with preserved memory, avoiding the issue of memory being cleared upon agent creation.

#### Details **Sources** - [llama-index-legacy/llama_index/legacy/chat_engine/condense_question.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/chat_engine/condense_question.py) - [llama-index-core/llama_index/core/agent/react/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/agent/react/base.py) - [llama-index-core/llama_index/core/agent/legacy/react/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/agent/legacy/react/base.py) - [llama-index-legacy/llama_index/legacy/agent/react/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/agent/react/base.py) - [llama-index-legacy/llama_index/legacy/agent/legacy/react/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/agent/legacy/react/base.py) - [llama-index-core/llama_index/core/memory/chat_memory_buffer.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/memory/chat_memory_buffer.py) - [llama-index-legacy/llama_index/legacy/memory/chat_memory_buffer.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/memory/chat_memory_buffer.py) - [llama-index-core/llama_index/core/storage/chat_store/simple_chat_store.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/storage/chat_store/simple_chat_store.py) - [docs/docs/module_guides/storing/chat_stores.md](https://github.com/run-llama/llama_index/blob/main/docs/docs/module_guides/storing/chat_stores.md) - [llama-index-legacy/llama_index/legacy/storage/chat_store/simple_chat_store.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/storage/chat_store/simple_chat_store.py)
**About Dosu** This response is meant to be useful and save you time. 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.

To continue the conversation, mention @dosu.

logan-markewich commented 3 months ago

@risos8200 agent.reset() resets the memory -- any reason to run this when the agent was just created? I would just remove this line

risos8200 commented 3 months ago

@logan-markewich It gives this error if I don't

'dict' object has no attribute 'content'