llama-agents stream events 处理

llama-agents 支持stream events 处理,可以提升用户体验,同时有更好的执行情况显示

参考玩法

  • step 发送stream events
@step
async def step_three(self, ctx: Context, ev: SecondEvent) -> StopEvent:
    ctx.write_event_to_stream(ProgressEvent(msg="Step three is happening"))
    return StopEvent(result="Workflow complete.")           
  • 执行获取stream 信息
async def main():
    workflow = SimpleFlow()
    handler =  workflow.run()
    # 获取stream events 
    async for ev in handler.stream_events():
        print(ev)
    # 获取结果
    final_result = await handler
    print("Final result", final_result)

说明

llama-agents 提供的一些其他stream 事件(workflow 执行异常时候返回的),比如WorkflowTimedOutEvent,WorkflowCancelledEvent,WorkflowFailedEvent,可以更好的进行状态显示

参考资料

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

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

导航