ai-agents-for-beginners

规划设计模式

(点击上方图片观看本课视频)

规划设计

介绍

本课内容包括

学习目标

完成本课后,您将了解:

定义总体目标与任务拆分

定义目标与任务

大多数现实世界任务过于复杂,无法一步完成。AI代理需要简明目标来指导其规划和行动。例如,设定目标:

“生成一个三天的旅游行程。”

虽然表述简单,但仍需细化。目标越清晰,代理(及任何人类协作者)就越能专注实现正确结果,如创建包含航班选项、酒店推荐和活动建议的完整行程。

任务拆分

将大型或复杂任务拆分成更小、更具目标性的子任务可提升可管理性。 针对旅游行程示例,您可以将目标拆分为:

然后由专门代理或流程处理各子任务。比如,一个代理专注搜索最佳机票,一个负责酒店预订,等等。最后由协调或“下游”代理将这些结果汇总,向最终用户提供完整行程。

这种模块化方式还便于逐步增强。例如,您可以增加专门负责美食推荐或本地活动建议的代理,随时间改进行程。

结构化输出

大型语言模型(LLM)能生成结构化输出(如JSON),便于下游代理或服务解析处理。在多代理场景中特别有用,可在接收规划输出后执行相应任务。

以下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"

# 旅行子任务模型
class TravelSubTask(BaseModel):
    task_details: str
    assigned_agent: AgentEnum  # 我们想要将任务分配给代理

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

provider = AzureAIProjectAgentProvider(credential=AzureCliCredential())

# 定义用户消息
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))

多代理编排的规划代理

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

规划者则:


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"

# 旅行子任务模型

class TravelSubTask(BaseModel):
    task_details: str
    assigned_agent: AgentEnum # 我们想将任务分配给代理

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

# 创建客户端

provider = AzureAIProjectAgentProvider(credential=AzureCliCredential())

from pprint import pprint

# 定义用户消息

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

# 加载为JSON后打印响应内容

pprint(json.loads(response_content))

下方为前述代码的输出,您可利用此结构化输出将任务路由至assigned_agent,并向最终用户总结行程计划。

{
    "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."
        }
    ]
}

含上述代码示例的笔记本范例可在 这里 获取。

迭代规划

部分任务需来回调整或重新规划,因一子任务结果会影响下一步。如代理在预订机票时发现意外数据格式,可能需要先调整策略再继续处理酒店预订。

此外,用户反馈(如人类决定更早的航班)可触发部分重新规划。这种动态迭代方式确保最终方案符合现实限制及不断变化的用户偏好。

示例代码

from agent_framework.azure import AzureAIProjectAgentProvider
from azure.identity import AzureCliCredential
#.. 与之前的代码相同,并传递用户历史、当前计划

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}",
)
# .. 重新规划并将任务发送给各自的代理

想要更全面的规划,请查看Magnetic One 博客文章,了解其解决复杂任务的方案。

总结

本文示例展示了如何创建一个规划者,能够动态选择定义的可用代理。规划者输出将任务拆分并分配代理执行。假设代理可访问完成任务所需的函数/工具。除代理外,还可包括反思、摘要、轮询聊天等模式以实现更细致定制。

额外资源

Magnetic One —— 一个用于解决复杂任务的通用多代理系统,在多个挑战性的代理基准测试中取得了优异成绩。参考:Magnetic One。该实现中,编排者创建特定任务计划并委派给可用代理,此外还采用跟踪机制监控任务进展并根据需要调整规划。

关于规划设计模式还有疑问?

加入 Microsoft Foundry Discord,与其他学习者交流,参加答疑时段,解决您的AI代理相关问题。

前一课

构建可信赖的AI代理

下一课

多代理设计模式


免责声明: 本文件采用 AI 翻译服务 Co-op Translator 进行翻译。虽然我们努力确保准确性,但请注意,自动翻译可能包含错误或不准确之处。原文文件及其原始语言版本应被视为权威来源。对于重要信息,建议采用专业人工翻译。因使用本翻译而产生的任何误解或误释,我们概不负责。