我们很高兴地推出 AgentWorkflow,这是 LlamaIndex 中的一个新系统,旨在简化 AI agent 系统的构建与编排。AgentWorkflow 基于我们广受欢迎的 Workflow 抽象构建,使 Agent 的部署与运行更加简单。AgentWorkflow 负责处理 Agent 之间的协调、状态维护等工作,同时仍为您提供 Workflow 的灵活性和可扩展性。
无论您需要的是单个专业 Agent 还是一个协作 Agent 团队,AgentWorkflow 都提供了构建稳健、有状态的 Agent 应用的基础组件。
构建 Agent 系统的挑战
随着 AI 应用变得日益复杂,简单的单次 Agent 交互往往不足以满足需求。设想构建一个研究助手,它需要:
- 从多个来源搜索并分析信息
- 将发现的结果综合成一份连贯的报告
- 审查并优化内容以确保准确性
- 在多次交互中保持上下文
- 处理不同专业组件之间复杂的来回交互
构建此类系统的传统方法通常涉及:
- 组件之间复杂的协调逻辑
- 难以维护状态和上下文
- 不同阶段之间脆弱的移交机制
- 对系统运行的可见性有限
在 LlamaIndex 引入 Workflows 之后,构建此类系统变得容易得多。我们甚至构建了自己的 多 Agent 示例!然而,我们发现仍存在大量可以被抽象掉的样板代码和复杂度,从而在保持 Workflow 系统灵活性和功能的同时,让构建 Agent 系统变得更简单。
AgentWorkflow 应运而生
The AgentWorkflow 通过提供一种结构化的方式来构建 Agent 系统,从而解决了这些挑战,该系统可以:
- 在交互过程中维护状态和上下文
- 在专业 Agent 之间进行协调
- 处理复杂的多步流程
- 实时流式传输并监控 Agent 活动
基于经过实战验证的 Workflows,AgentWorkflow 使创建简单或复杂的 Agent 系统变得轻而易举。
快速上手
对于基础用例,您可以快速创建一个包含单个 Agent 的工作流
python
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 的研究报告系统
python
# 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)。
python
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. 实时可见性
通过事件流监控执行过程
python
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. 人机协作 (Human-in-the-Loop) 支持
轻松集成人工监督和输入
python
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 准备好开始构建了吗?
立即开始!
- 从我们的 基础 Agent 工作流教程 开始
- 查看 更详细的文档
- 探索 多 Agent 研究工作流示例
- 加入我们的 Discord 社区 讨论您的使用场景
- 观看我们的 YouTube 介绍视频
在接下来的几周里,我们将发布更多示例,展示如何将 AgentWorkflow 应用于各种实际场景。敬请期待!