----------------------
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:
- First, you need to create a
Context
object for your workflow:
from llama_index.core.workflow import Context
ctx = Context(workflow)
- Then, you can use this context in your workflow runs:
response = await workflow.run(user_msg="Your message here", ctx=ctx)
- The context will automatically store the chat history and other relevant information.
- In subsequent runs, you can use the same context to maintain the conversation state:
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:
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 StateAgent Workflow Basic Introduction