跳到正文
LlamaIndex:产品、工程与评测·· 2025-01-22精选AI 评分60

LlamaIndex 发布 AgentWorkflow,用于构建多智能体系统

Introducing AgentWorkflow: A Powerful System for Building AI Agent Systems

AI 导读

LlamaIndex 推出 AgentWorkflow,一个基于其 Workflow 抽象的智能体系统构建与编排框架。它负责智能体间协调、状态维护和实时事件流监控,支持 FunctionAgent 与 ReActAgent 两种类型、全局 Context 状态管理,以及通过 InputRequiredEvent 集成人工审批,官方提供了单智能体和多智能体研究报告系统的代码示例。

推荐理由

官方介绍了 AgentWorkflow 的多智能体协作、内置状态管理和事件流监控能力,并附可直接运行的代码示例,适合评估是否引入现有 Agent 开发流程。

正文 · AI 翻译

我们很高兴推出 AgentWorkflow,这是 LlamaIndex 中的一个新系统,让构建和编排 AI agent 系统变得轻而易举。AgentWorkflow 建立在我们广受欢迎的 Workflow 抽象之上,让 agent 的启动和运行更加简单。AgentWorkflows 负责协调 agent 之间的交互、维护状态等,同时仍为你提供 Workflows 的灵活性和可扩展性。

无论你需要单个专用 agent 还是一组协作 agent,AgentWorkflow 都提供了构建健壮、有状态 agent 应用的构建块。

立即探索我们的免费和付费方案。

构建 Agent 系统的挑战

随着 AI 应用日益复杂,简单的一次性 agent 交互往往不够用。设想构建一个研究助手,它需要:

  1. 从多个来源搜索和分析信息
  2. 将发现综合成一份连贯的报告
  3. 审查并完善内容以确保准确性
  4. 在多次交互中保持上下文
  5. 处理不同专用组件之间复杂的来回交互

构建此类系统的传统方法通常涉及:

  • 组件之间复杂的协调逻辑
  • 难以维护状态和上下文
  • 不同阶段之间脆弱的交接
  • 对系统运行的可见性有限

在 LlamaIndex 中引入 Workflows 之后,这类系统变得容易构建得多。我们甚至构建了自己的多 agent 示例!然而,我们发现仍有足够多的样板代码和复杂性可以抽象掉,从而让 agent 系统的构建更加容易,同时仍保持 Workflow 系统的灵活性和强大功能。

认识 AgentWorkflow

AgentWorkflow 通过提供一种结构化的方式来构建 agent 系统,从而应对这些挑战,这些系统可以:

  • 在交互中保持状态和上下文
  • 在专用 agent 之间进行协调
  • 处理复杂的多步骤流程
  • 实时流式传输和监控 agent 活动

构建在我们久经考验的 Workflows 之上,AgentWorkflow 让创建简单和复杂的 agent 系统都变得容易。

入门很简单

对于基本用例,你可以快速创建一个包含单个 agent 的工作流:

workflow = AgentWorkflow.from_tools_or_functions(
    [search_web],
    llm=llm,
    system_prompt=(
        "You are a helpful assistant that can search the web for information."
    )
)

# Run the agent
response = await workflow.run(
    user_msg="What is the latest news about AI?",
)

构建多 Agent 系统

对于更复杂的场景,AgentWorkflow 也可用于构建多 agent 系统。让我们看看如何构建一个包含多个专用 agent 的研究报告系统:

# Create specialized agents
research_agent = FunctionAgent(
    name="ResearchAgent",
    description="Searches and analyzes information from multiple sources",
    system_prompt="You are a thorough researcher...",
    tools=[search_web, record_notes],
    can_handoff_to=["WriteAgent"]
)

write_agent = FunctionAgent(
    name="WriteAgent",
    description="Writes clear, comprehensive reports",
    system_prompt="You are an expert writer...",
    tools=[write_report],
    can_handoff_to=["ReviewAgent", "ResearchAgent"]
)

review_agent = FunctionAgent(
    name="ReviewAgent",
    description="Reviews content for accuracy and quality",
    system_prompt="You are a meticulous reviewer...",
    tools=[review_report],
    can_handoff_to=["WriteAgent"]
)

# Create the workflow
agent_workflow = AgentWorkflow(
    agents=[research_agent, write_agent, review_agent],
    root_agent="ResearchAgent",
    initial_state={
        "research_notes": {},
        "report_draft": "",
        "review_feedback": "",
    }
)

让它强大的关键特性

1. 灵活的 Agent 类型

支持不同的 agent 架构以满足你的需求:

  • FunctionAgent 适用于具有函数调用能力的 LLM
  • ReActAgent 适用于任何 LLM
  • 可轻松扩展以支持自定义 agent 类型

2. 内置状态管理

在交互中保持上下文。AgentWorkflow 中的工具/函数可以访问全局工作流 Context。

async def record_notes(ctx: Context, notes: str) -> str:
    """Record research notes for later use."""
    current_state = await ctx.get("state")
    current_state["research_notes"].append(notes)
    await ctx.set("state", current_state)
    return "Notes recorded."

3. 实时可见性

通过事件流监控执行过程:

handler = workflow.run(user_msg="Research quantum computing")
async for event in handler.stream_events():
    if isinstance(event, AgentInput):
        print(f"Agent {event.current_agent_name}: ")
    if isinstance(event, AgentStream):
        print(f"{event.delta}", end="")
    elif isinstance(event, ToolCallResult):
        print(f"Tool called: {event.tool_name} -> {event.tool_output}")

4. 人在回路支持

轻松集成人工监督和输入:

async def get_approval(ctx: Context) -> bool:
    """Request human approval before proceeding."""
    ctx.write_event_to_stream(
        InputRequiredEvent(
            prefix="Please review and approve this section:"
        )
    )
    response = await ctx.wait_for_event(HumanResponseEvent)
    return response.approved

准备好构建了吗?

开始吧!

  1. 从我们的基础 agent 工作流教程开始
  2. 查看更详细的文档
  3. 探索多 agent 研究工作流示例
  4. 加入我们的 Discord 社区讨论你的用例
  5. 查看我们在 YouTube 上的入门视频

在接下来的几周里,我们将发布更多示例,展示如何将 AgentWorkflow 用于各种实际应用。敬请关注!

来源:LlamaIndex:产品、工程与评测 · llamaindex.ai