宣布 LlamaCloud 全面上线(以及我们获得 1900 万美元 A 轮融资!)
LlamaIndex

LlamaIndex 2025-01-22

引入 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. 灵活的代理类型

支持不同的代理架构以满足您的需求

  • 具有函数调用能力的 LLMs 的 FunctionAgent
  • 适用于任何 LLM 的 ReActAgent
  • 易于扩展以支持自定义代理类型

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。敬请关注!