llama-agents 状态管理

在工作流执行中经常会有共享状态的处理,llama-agents 通过context 的storage 可以存储信息,默认是没有类型的,当然也可以添加状态

参考玩法

  • 获取以及存储状态
class MyWorkflow(Workflow):

    @step
    async def my_step(self, ctx: Context, ev: StartEvent) -> StopEvent:
       current_count = await ctx.store.get("count", default=0)
       current_count += 1
       await ctx.store.set("count", current_count)
       return StopEvent()

状态复用

实际上就是对于context 的持久化处理

workflow = MyWorkflow()
ctx = Context(workflow)

handler = workflow.run(ctx=ctx)
result = await handler

# Optional: save the ctx somewhere and restore
# ctx_dict = ctx.to_dict()
# ctx = Context.from_dict(workflow, ctx_dict)

# continue with next run
handler = workflow.run(ctx=ctx)
result = await handler

说明

状态参数对于workflow 的运行是比较重要的东西,使用好比较方便

参考资料

https://developers.llamaindex.ai/python/llamaagents/workflows/managing_state/

posted on 2026-05-27 08:00  荣锋亮  阅读(18)  评论(0)    收藏  举报

导航