西蒙哥西蒙哥  2022-04-19 20:02 Github 隐藏边栏 |   抢沙发  5 
文章评分 0 次,平均分 0.0
导语: 定义清晰的总体目标,并将复杂任务分解为易于管理的任务。 利用结构化输出以获得更可靠且机器可读的响应。 应用事件驱动方法来处理动态任务和意外输入。

简介 (Introduction)

本课程将涵盖:

  • 定义清晰的总体目标,并将复杂任务分解为易于管理的任务。
  • 利用结构化输出以获得更可靠且机器可读的响应。
  • 应用事件驱动方法来处理动态任务和意外输入。

学习目标 (Learning Goals)

完成本课程后,您将了解以下内容:

  • 为 AI 代理识别并设定总体目标,确保它明确知道需要完成什么。
  • 将复杂任务分解为可管理的子任务,并按逻辑顺序进行组织。
  • 为代理配备合适的工具(例如搜索工具或数据分析工具),决定何时及如何使用它们,并处理出现的意外情况。
  • 评估子任务结果,衡量性能,并迭代操作以改进最终输出。

定义总体目标并分解任务 (Defining the Overall Goal and Breaking Down a Task)

定义总体目标并分解任务

大多数现实世界中的任务都过于复杂,无法一步完成。AI 代理需要一个简明扼要的目标来指导其规划和行动。例如,考虑以下目标:

“生成一份为期 3 天的旅行行程。”

虽然这说起来简单,但它仍需要细化。目标越清晰,代理(以及任何人类协作者)就越能专注于实现正确的结果,例如创建一份包含航班选项、酒店推荐和活动建议的全面行程。

任务分解 (Task Decomposition)

当将大型或复杂的任务拆分为以目标为导向的较小子任务时,它们会变得更易于管理。对于旅行行程的示例,您可以将总体目标分解为:

  • 预订航班 (Flight Booking)
  • 预订酒店 (Hotel Booking)
  • 租车 (Car Rental)
  • 个性化定制 (Personalization)

然后,每个子任务都可以由专用的代理或流程来处理。一个代理可能专门寻找最佳航班优惠,另一个专门负责预订酒店,等等。随后,一个协调代理(或“下游”代理)可以将这些结果汇总成一份连贯的行程并提供给最终用户。

这种模块化方法还允许进行增量增强。例如,您可以添加专门用于“美食推荐”或“当地活动建议”的代理,并随着时间的推移不断完善行程。

结构化输出 (Structured output)

大型语言模型 (LLM) 可以生成结构化输出(例如 JSON),这使得下游代理或服务更容易解析和处理。这在多代理上下文中特别有用,这样我们就可以在接收到规划输出后执行相应的任务。

以下 Python 代码片段展示了一个简单的规划代理,它将目标分解为子任务并生成结构化计划:

Python

from pydantic import BaseModel
from enum import Enum
from typing import List, Optional, Union
import json
import os
from typing import Optional
from pprint import pprint
from agent_framework.azure import AzureAIProjectAgentProvider
from azure.identity import AzureCliCredential

class AgentEnum(str, Enum):
    FlightBooking = "flight_booking"
    HotelBooking = "hotel_booking"
    CarRental = "car_rental"
    ActivitiesBooking = "activities_booking"
    DestinationInfo = "destination_info"
    DefaultAgent = "default_agent"
    GroupChatManager = "group_chat_manager"

# Travel SubTask Model
class TravelSubTask(BaseModel):
    task_details: str
    assigned_agent: AgentEnum  # we want to assign the task to the agent

class TravelPlan(BaseModel):
    main_task: str
    subtasks: List[TravelSubTask]
    is_greeting: bool

provider = AzureAIProjectAgentProvider(credential=AzureCliCredential())

# Define the user message
system_prompt = """You are a planner agent.
    Your job is to decide which agents to run based on the user's request.
    Provide your response in JSON format with the following structure:
{'main_task': 'Plan a family trip from Singapore to Melbourne.',
 'subtasks': [{'assigned_agent': 'flight_booking',
               'task_details': 'Book round-trip flights from Singapore to '
                               'Melbourne.'}
    Below are the available agents specialised in different tasks:
    - FlightBooking: For booking flights and providing flight information
    - HotelBooking: For booking hotels and providing hotel information
    - CarRental: For booking cars and providing car rental information
    - ActivitiesBooking: For booking activities and providing activity information
    - DestinationInfo: For providing information about destinations
    - DefaultAgent: For handling general requests"""

user_message = "Create a travel plan for a family of 2 kids from Singapore to Melbourne"

response = client.create_response(input=user_message, instructions=system_prompt)

response_content = response.output_text
pprint(json.loads(response_content))

具有多代理编排的规划代理 (Planning Agent with Multi-Agent Orchestration)

在此示例中,语义路由器代理 (Semantic Router Agent) 接收用户请求(例如,“我需要一份旅行的酒店计划。”)。

然后,规划器执行以下操作:

  1. 接收酒店计划: 规划器获取用户的消息,并根据系统提示词(包括可用代理的详细信息),生成结构化的旅行计划。
  2. 列出代理及其工具: 代理注册表保存着一个代理列表(例如,用于航班、酒店、租车和活动的代理)及其提供的功能或工具。
  3. 将计划路由给相应的代理: 根据子任务的数量,规划器可以直接将消息发送给专用代理(对于单任务场景),或者通过群聊管理器进行协调(用于多代理协作)。
  4. 总结结果: 最后,规划器会总结生成的计划,以确保清晰明了。

以下 Python 代码示例说明了这些步骤:

Python


from pydantic import BaseModel

from enum import Enum
from typing import List, Optional, Union

class AgentEnum(str, Enum):
FlightBooking = "flight_booking"
HotelBooking = "hotel_booking"
CarRental = "car_rental"
ActivitiesBooking = "activities_booking"
DestinationInfo = "destination_info"
DefaultAgent = "default_agent"
GroupChatManager = "group_chat_manager"

# Travel SubTask Model

class TravelSubTask(BaseModel):
task_details: str
assigned_agent: AgentEnum # we want to assign the task to the agent

class TravelPlan(BaseModel):
main_task: str
subtasks: List[TravelSubTask]
is_greeting: bool
import json
import os
from typing import Optional

from agent_framework.azure import AzureAIProjectAgentProvider
from azure.identity import AzureCliCredential

# Create the client

provider = AzureAIProjectAgentProvider(credential=AzureCliCredential())

from pprint import pprint

# Define the user message

system_prompt = """You are a planner agent.
Your job is to decide which agents to run based on the user's request.
Below are the available agents specialized in different tasks:
- FlightBooking: For booking flights and providing flight information
- HotelBooking: For booking hotels and providing hotel information
- CarRental: For booking cars and providing car rental information
- ActivitiesBooking: For booking activities and providing activity information
- DestinationInfo: For providing information about destinations
- DefaultAgent: For handling general requests"""

user_message = "Create a travel plan for a family of 2 kids from Singapore to Melbourne"

response = client.create_response(input=user_message, instructions=system_prompt)

response_content = response.output_text

# Print the response content after loading it as JSON

pprint(json.loads(response_content))

以下是前面代码的输出结果,然后您可以使用此结构化输出将其路由到指定的 assigned_agent,并为最终用户总结旅行计划。

JSON

{
    "is_greeting": "False",
    "main_task": "Plan a family trip from Singapore to Melbourne.",
    "subtasks": [
        {
            "assigned_agent": "flight_booking",
            "task_details": "Book round-trip flights from Singapore to Melbourne."
        },
        {
            "assigned_agent": "hotel_booking",
            "task_details": "Find family-friendly hotels in Melbourne."
        },
        {
            "assigned_agent": "car_rental",
            "task_details": "Arrange a car rental suitable for a family of four in Melbourne."
        },
        {
            "assigned_agent": "activities_booking",
            "task_details": "List family-friendly activities in Melbourne."
        },
        {
            "assigned_agent": "destination_info",
            "task_details": "Provide information about Melbourne as a travel destination."
        }
    ]
}

包含前面代码示例的笔记本示例可在此处 (here) 获取。

迭代规划 (Iterative Planning)

某些任务需要来回沟通或重新规划,其中一个子任务的结果会影响下一个子任务。例如,如果代理在预订航班时发现意外的数据格式,它可能需要在转入预订酒店之前调整其策略。

此外,用户反馈(例如,人类决定他们更喜欢早一点的航班)可能会触发部分重新规划。这种动态的迭代方法可确保最终的解决方案符合现实世界的约束和不断变化的用户偏好。

例如示例代码:

Python

from agent_framework.azure import AzureAIProjectAgentProvider
from azure.identity import AzureCliCredential
#.. same as previous code and pass on the user history, current plan

system_prompt = """You are a planner agent to optimize the
    Your job is to decide which agents to run based on the user's request.
    Below are the available agents specialized in different tasks:
    - FlightBooking: For booking flights and providing flight information
    - HotelBooking: For booking hotels and providing hotel information
    - CarRental: For booking cars and providing car rental information
    - ActivitiesBooking: For booking activities and providing activity information
    - DestinationInfo: For providing information about destinations
    - DefaultAgent: For handling general requests"""

user_message = "Create a travel plan for a family of 2 kids from Singapore to Melbourne"

response = client.create_response(
    input=user_message,
    instructions=system_prompt,
    context=f"Previous travel plan - {TravelPlan}",
)
# .. re-plan and send the tasks to respective agents

有关更全面的规划,请查看 Magnetic One 博客文章以了解如何解决复杂任务。

总结 (Summary)

在本文中,我们看了一个示例,展示了如何创建一个能够动态选择已定义可用代理的规划器。规划器的输出会分解任务并分配代理,以便执行这些任务。前提是假设代理有权访问执行任务所需的函数/工具。除了代理之外,您还可以包含其他模式,如反思机制 (reflection)、摘要生成器 (summarizer) 和轮询聊天 (round robin chat) 以进行进一步的自定义。

附加资源 (Additional Resources)

Magentic One - 一个用于解决复杂任务的通用多代理系统,在多个具有挑战性的代理基准测试中取得了令人印象深刻的结果。参考资料:Magentic One。在此实现中,编排器创建特定于任务的计划,并将这些任务委派给可用的代理。除了规划之外,编排器还采用了一种跟踪机制来监控任务进度,并根据需要进行重新规划。

本文来自投稿,不代表西蒙园立场,来源于Github,版权归原作者所有,欢迎分享本文,转载请保留出处!

西蒙哥
西蒙哥 关注:0    粉丝:0 最后编辑于:2026-04-20
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到

扫一扫二维码分享