教程 9:多智能体编排 掌握复杂的多智能体工作流!本教程将教你如何利用并行执行、智能体即工具模式以及高级编排技术,协调多个智能体,构建功能强大的 AI 系统。 你将学到的内容 并行执行:使用 同时运行多个智能体 智能体即工具:将智能体用作函数工具进行复杂编排 工作流协调:顺序与并行的智能体处理模式 结果合成:智能地整合多个智能体的输出 核心概念:什么是多智能体编排? 多智能体编排实现了协调的 AI 工作流,其中多个专业化的智能体协同合作,共同解决复杂问题。可以把编排想象成一位指挥家带领交响乐团: 不同的智能体拥有专门的角色和专长 智能体可根据工作流需求并行或顺序工作 多个智能体的结果被智能地整合 复杂任务被分解为多个 AI 能力协作完成 教程概览 本教程将演示三种关键的编排模式: 1.
掌握复杂的多智能体工作流!本教程将教你如何利用并行执行、智能体即工具模式以及高级编排技术,协调多个智能体,构建功能强大的 AI 系统。
asyncio.gather() 同时运行多个智能体多智能体编排实现了协调的 AI 工作流,其中多个专业化的智能体协同合作,共同解决复杂问题。可以把编排想象成一位指挥家带领交响乐团:
┌─────────────────────────────────────────────────────────────-┐ │ MULTI-AGENT ORCHESTRATION │ ├─────────────────────────────────────────────────────────────-┤ │ │ │ COMPLEX TASK │ │ │ │ │ ▼ │ │ ┌─────────────┐ 1. TASK DECOMPOSITION │ │ │ORCHESTRATOR │ │ │ │ AGENT │ 2. AGENT COORDINATION │ │ └─────────────┘ │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ PARALLEL EXECUTION │ │ │ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │ | │ │ │RESEARCH │ │WRITING │ │ANALYSIS │ │REVIEW │ │ │ | │ │ │ AGENT │ │ AGENT │ │ AGENT │ │ AGENT │ │ │ | │ │ └─────────┘ └─────────┘ └─────────┘ └─────────┘ │ │ | │ └─────────────────────────────────────────────────────────┘ │ │ │ │ │ │ │ │ ▼ ▼ ▼ ▼ │ │ ┌─────────────────────────────────────────────────────────┐ │ │ │ RESULT SYNTHESIS │ │ │ │ • Combine outputs intelligently │ │ │ │ • Quality assessment and selection │ │ │ │ • Final coordinated response │ │ │ └─────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────-┘
本教程将演示三种关键的编排模式:
parallel_execution.py)asyncio.gather() 同时运行多个智能体agents_as_tools.py)complex_orchestration.py)9_multi_agent_orchestration/ ├── README.md # This file - concept explanation ├── requirements.txt # Dependencies ├── parallel_execution.py # Parallel agent patterns (45 lines) ├── agents_as_tools.py # Agents as tools orchestration (55 lines) ├── complex_orchestration.py # Advanced workflow patterns (70 lines) ├── app.py # Streamlit orchestration demo (optional) └── env.example # Environment variables template
完成本教程后,你将理解:
安装 OpenAI Agents SDK:
pip install openai-agents
安装依赖:
pip install -r requirements.txt
设置环境变量:
cp env.example .env # Edit .env and add your OpenAI API key
测试并行执行:
python parallel_execution.py
尝试智能体即工具:
python agents_as_tools.py
探索复杂工作流:
python complex_orchestration.py
import asyncio from agents import Agent, Runner, trace # Run multiple agents in parallel with trace("Parallel translation"): results = await asyncio.gather( Runner.run(translator_agent, message), Runner.run(translator_agent, message), Runner.run(translator_agent, message) ) # Select best result best = await Runner.run(selector_agent, combined_results)
from agents import Agent, function_tool @function_tool async def research_tool(topic: str) -> str: result = await Runner.run(research_agent, f"Research: {topic}") return str(result.final_output) orchestrator = Agent( name="Content Orchestrator", tools=[research_tool, writing_tool] )
# Sequential stages with parallel execution within stages with trace("Content Creation Pipeline"): # Stage 1: Parallel research research_results = await asyncio.gather( research_agent_1.run(topic), research_agent_2.run(topic) ) # Stage 2: Sequential writing content = await writing_agent.run(combined_research) # Stage 3: Parallel review reviews = await asyncio.gather( quality_agent.run(content), style_agent.run(content) )
trace() 对多智能体工作流进行分组完成本教程后,你将准备好:
免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。