跳到正文
原文
LlamaIndex:产品、工程与评测·· 3 小时前精选AI 评分65

LlamaIndex 推出 AgentWorkflow,用于构建多智能体系统

Introducing AgentWorkflow: A Powerful System for Building AI Agent Systems

AI 导读

LlamaIndex 推出 AgentWorkflow,一个用于构建和编排 AI 智能体系统的新系统,基于其 Workflow 抽象构建。它负责智能体间协调、状态维护和上下文保持,支持 FunctionAgent 与 ReActAgent 两种智能体类型、内置全局 Context 状态管理、实时事件流监控以及 human-in-the-loop 人工审批。

推荐理由

官方介绍 AgentWorkflow 的状态管理、多智能体协作和事件流等核心能力,并附可直接上手的代码示例。

正文 · AI 翻译

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

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

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

构建智能体系统的挑战

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

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

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

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

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

AgentWorkflow 登场

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

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

AgentWorkflow 构建在我们久经考验的 Workflows 之上,让创建简单和复杂的智能体系统都变得轻而易举。

入门很简单

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

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?",
)

构建多智能体系统

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

# 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. 灵活的智能体类型

支持不同的智能体架构以满足你的需求:

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

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. 从我们的基础智能体工作流教程开始
  2. 查看更详细的文档
  3. 探索多智能体研究工作流示例
  4. 加入我们的 Discord 社区讨论你的用例
  5. 查看我们在 YouTube 上的入门视频

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

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