Find answers from the community

Updated 2 days ago

and <@334536717648265216> lets say I am using multi-agent workflows. Is it possible to h

@kapa.ai and @Logan M lets say I am using multi-agent workflows. Is it possible to have some type of chat memory stored and injected in the next round of multie agent workflow?
k
c
7 comments
Please stand by @cmosguy as I search the knowledge base. I normally take about 15 seconds or so. :wave:
----------------------
Yes, it is possible to have chat memory stored and injected in the next round of multi-agent workflows in LlamaIndex. This can be achieved using the Context class, which allows you to maintain state between runs of an agent workflow. Here's how you can do it:

  1. First, you need to create a Context object for your workflow:
Plain Text
from llama_index.core.workflow import Context

ctx = Context(workflow)


  1. Then, you can use this context in your workflow runs:
Plain Text
response = await workflow.run(user_msg="Your message here", ctx=ctx)


  1. The context will automatically store the chat history and other relevant information.
  1. In subsequent runs, you can use the same context to maintain the conversation state:
Plain Text
next_response = await workflow.run(user_msg="Your next message", ctx=ctx)


This approach allows the agent to remember previous interactions and maintain a coherent conversation across multiple runs.

Additionally, if you need to persist the context for longer periods (e.g., between sessions), you can serialize and deserialize the context:

Plain Text
from llama_index.core.workflow import JsonSerializer

# Serialize
ctx_dict = ctx.to_dict(serializer=JsonSerializer())

# Deserialize
restored_ctx = Context.from_dict(workflow, ctx_dict, serializer=JsonSerializer())


This feature is documented in the LlamaIndex documentation:

Understanding Agent State
Agent Workflow Basic Introduction
By using this approach, you can effectively store and inject chat memory in multi-agent workflows, allowing for more coherent and context-aware interactions across multiple rounds of conversation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
what if memory is stored int he chat memory buffer @kapa.ai ?
Let me check the knowledge base for you again. One moment! :books:
----------------------
If memory is stored in the ChatMemoryBuffer, it provides a simple and effective way to maintain chat history for agents in LlamaIndex. The ChatMemoryBuffer is specifically designed for storing chat history and can be easily integrated into agent workflows. Here's what you need to know:

  1. ChatMemoryBuffer is a class in LlamaIndex that implements a simple buffer for storing chat history. It's defined in the llama_index.core.memory.chat_memory_buffer module ChatMemoryBuffer.
  1. You can create a ChatMemoryBuffer instance using the from_defaults method:
Plain Text
   from llama_index.core.memory import ChatMemoryBuffer

   memory = ChatMemoryBuffer.from_defaults(llm=your_llm)
   


  1. In multi-agent workflows, you can check if memory is already set up and create it if it doesn't exist:
Plain Text
   memory = await ctx.get("memory", default=None)
   if not memory:
       memory = ChatMemoryBuffer.from_defaults(llm=self.llm)
   


This pattern is used in both function calling agents and react agents, as seen in the LlamaIndex documentation Function Calling Agent and React Agent.

  1. The ChatMemoryBuffer can be used with various types of agents, including OpenAIAgent. For example:
Plain Text
   agent = OpenAIAgent.from_tools(
       tools,
       llm=llm,
       memory=ChatMemoryBuffer.from_defaults(llm=llm),
       verbose=True
   )
   


This is demonstrated in the LlamaIndex documentation OpenAIAgent.from_tools.
  1. The ChatMemoryBuffer has a token limit to manage the size of the chat history. You can specify this limit when creating the buffer:
Plain Text
   memory = ChatMemoryBuffer.from_defaults(
       llm=llm,
       token_limit=your_token_limit
   )
   


If not specified, it uses a default token limit based on the LLM's context window.

  1. To retrieve the chat history from the ChatMemoryBuffer, you can use the get method:
Plain Text
   chat_history = memory.get()
   


This returns a list of ChatMessage objects representing the conversation history.

By using ChatMemoryBuffer, you can easily maintain and inject chat memory into your multi-agent workflows, allowing agents to have context-aware conversations across multiple interactions. This approach is simpler than using the Context class mentioned earlier and is well-suited for straightforward chat history management in LlamaIndex.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord