开发 AG2 代理

AG2 是原始 AutoGen 的社区驱动型分支,是一个用于构建 AI 赋能的智能体的开源框架。本页介绍了如何使用特定于框架的 AG2 模板(Vertex AI SDK for Python 中的 AG2Agent 类)来开发代理。该代理会返回指定日期两种货币之间的汇率。具体步骤如下所示:

  1. 定义和配置可运行项
  2. 定义和使用工具
  3. (可选)自定义编排

准备工作

请按照设置环境中的步骤设置您的环境。

第 1 步:定义和配置 Runnable

定义要使用的模型版本

model = "gemini-1.5-flash-001"

定义要使用的可运行名称。

runnable_name = "Get Exchange Rate Agent"

(可选)配置模型。

from google.cloud.aiplatform.aiplatform import initializer

llm_config = {
    "config_list": [{
        "project_id":       initializer.global_config.project,
        "location":         initializer.global_config.location,
        "model":            "gemini-1.5-flash-001",
        "api_type":         "google",
    }]
}

如需详细了解如何在 AG2 中配置模型,请参阅模型配置深入解析

(可选)配置模型的安全设置。以下示例展示了如何配置安全设置:

from vertexai.generative_models import HarmBlockThreshold, HarmCategory

safety_settings = {
    HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
}

for config_item in llm_config["config_list"]:
    config_item["safety_settings"] = safety_settings

如需详细了解可用于 Gemini 中安全设置的选项,请参阅配置安全属性

使用模型配置创建 AG2Agent

agent = agent_engines.AG2Agent(
    model=model,                  # Required.
    runnable_name=runnable_name,  # Required.
    llm_config=llm_config,        # Optional.
)

如果您是在交互式环境(例如终端或 Colab 笔记本)中运行,则可以将运行查询作为中间测试步骤:

response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?", max_turns=1)

print(response)

响应是类似于以下示例的 Python 字典:

{'chat_id': None,
 'chat_history': [{'content': 'What is the exchange rate from US dollars to Swedish currency?',
   'role': 'assistant',
   'name': 'user'},
  {'content': 'I do not have access to real-time information, including currency exchange rates. To get the most up-to-date exchange rate from US dollars to Swedish Krona (SEK), I recommend using a reliable online currency converter or checking with your bank. \n',
   'role': 'user',
   'name': 'Exchange Rate Agent'}],
 'summary': 'I do not have access to real-time information, including currency exchange rates. To get the most up-to-date exchange rate from US dollars to Swedish Krona (SEK), I recommend using a reliable online currency converter or checking with your bank. \n',
 'cost': {'usage_including_cached_inference': {'total_cost': 5.2875e-06,
   'gemini-1.5-flash-001': {'cost': 5.2875e-06,
    'prompt_tokens': 34,
    'completion_tokens': 62,
    'total_tokens': 96}},
  'usage_excluding_cached_inference': {'total_cost': 5.2875e-06,
   'gemini-1.5-flash-001': {'cost': 5.2875e-06,
    'prompt_tokens': 34,
    'completion_tokens': 62,
    'total_tokens': 96}}},
 'human_input': []}

(可选)高级自定义

AG2Agent 模板默认使用 api_type=="google",因为它可提供对 Google Cloud中所有基础模型的访问权限。如需使用无法通过 api_type=="google" 获取的模型,您可以自定义 llm_config 参数。

如需查看 AG2 中支持的模型及其功能的列表,请参阅模型提供程序llm_config= 支持的值集因聊天模型而异,因此您应参阅相应文档以了解详情。

Gemini

默认安装

当您省略 llm_config 参数时,它会在 AG2Agent 模板中使用,例如

agent = agent_engines.AG2Agent(
    model=model,                # Required.
    runnable_name=runnable_name # Required.
)

Anthropic

首先,按照其文档设置账号并安装软件包。

接下来,定义 llm_config

llm_config = {
    "config_list": [{
        "model": "claude-3-5-sonnet-20240620",            # Required.
        "api_key": "ANTHROPIC_API_KEY",  # Required.
        "api_type": "anthropic",                          # Required.
     }]
}

最后在 AG2Agent 模板中使用它,代码如下:

agent = agent_engines.AG2Agent(
    model="claude-3-5-sonnet-20240620",             # Required.
    runnable_name=runnable_name,                    # Required.
    llm_config=llm_config,                          # Optional.
)

OpenAI

您可以将 OpenAI 与 Gemini 的 ChatCompletions API 搭配使用。

首先,定义 llm_config

import google.auth
from google.cloud.aiplatform.aiplatform import initializer

project = initializer.global_config.project
location = initializer.global_config.location
base_url = f"https://{location}-aiplatform.googleapis.com/v1beta1/projects/{project}/locations/{location}/endpoints/openapi"

# Note: the credential lives for 1 hour by default.
# After expiration, it must be refreshed.
creds, _ = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)

llm_config = {
    "config_list": [{
        "model": "google/gemini-1.5-flash-001",  # Required.
        "api_type": "openai",                    # Required.
        "base_url": base_url,                    # Required.
        "api_key": creds.token,                  # Required.
    }]
}

最后在 AG2Agent 模板中使用它,代码如下:

agent = agent_engines.AG2Agent(
    model="google/gemini-1.5-flash-001",  # Or "meta/llama3-405b-instruct-maas".
    runnable_name=runnable_name,          # Required.
    llm_config=llm_config,                # Optional.
)

第 2 步:定义和使用工具

定义模型后,下一步是定义模型用于推理的工具。这里的工具可以是 AG2 工具,也可以是 Python 函数。

定义函数时,请务必添加能完整而清晰地描述函数的参数、函数的用途以及函数返回内容的注释。模型会使用此信息来确定要使用的函数。您还必须在本地测试函数,以确认其是否正常运行。

使用以下代码定义一个返回汇率的函数:

def get_exchange_rate(
    currency_from: str = "USD",
    currency_to: str = "EUR",
    currency_date: str = "latest",
):
    """Retrieves the exchange rate between two currencies on a specified date.

    Uses the Frankfurter API (https://api.frankfurter.app/) to obtain
    exchange rate data.

    Args:
        currency_from: The base currency (3-letter currency code).
            Defaults to "USD" (US Dollar).
        currency_to: The target currency (3-letter currency code).
            Defaults to "EUR" (Euro).
        currency_date: The date for which to retrieve the exchange rate.
            Defaults to "latest" for the most recent exchange rate data.
            Can be specified in YYYY-MM-DD format for historical rates.

    Returns:
        dict: A dictionary containing the exchange rate information.
            Example: {"amount": 1.0, "base": "USD", "date": "2023-11-24",
                "rates": {"EUR": 0.95534}}
    """
    import requests
    response = requests.get(
        f"https://api.frankfurter.app/{currency_date}",
        params={"from": currency_from, "to": currency_to},
    )
    return response.json()

如需在代理中使用函数之前先对其进行测试,请运行以下命令:

get_exchange_rate(currency_from="USD", currency_to="SEK")

响应应该类似以下内容:

{'amount': 1.0, 'base': 'USD', 'date': '2024-02-22', 'rates': {'SEK': 10.3043}}

如需在 AG2Agent 模板内使用该工具,您需要将其添加到 tools= 参数下的工具列表中:

agent = agent_engines.AG2Agent(
    model=model,                 # Required.
    runnable_name=runnable_name, # Required.
    tools=[get_exchange_rate],   # Optional.
)

您可以通过对代理发起测试查询在本地测试该代理。运行以下命令,使用美元和瑞典克朗在本地测试代理:

response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?", max_turns=2)

响应是一个类似于以下内容的字典:

{'chat_id': None,
 'chat_history': [{'content': 'What is the exchange rate from US dollars to Swedish currency?',
   'role': 'assistant',
   'name': 'user'},
  {'content': '',
   'tool_calls': [{'id': '2285',
     'function': {'arguments': '{"currency_from": "USD", "currency_to": "SEK"}',
      'name': 'get_exchange_rate'},
     'type': 'function'}],
   'role': 'assistant'},
  {'content': "{'amount': 1.0, 'base': 'USD', 'date': '2025-02-27', 'rates': {'SEK': 10.6509}}",
   'tool_responses': [{'tool_call_id': '2285',
     'role': 'tool',
     'content': "{'amount': 1.0, 'base': 'USD', 'date': '2025-02-27', 'rates': {'SEK': 10.6509}}"}],
   'role': 'tool',
   'name': 'user'},
  {'content': 'The current exchange rate is 1 USD to 10.6509 SEK. \n',
   'role': 'user',
   'name': 'Get Exchange Rate Agent'},
  {'content': 'What is the exchange rate from US dollars to Swedish currency?',
   'role': 'assistant',
   'name': 'user'},
  {'content': '',
   'tool_calls': [{'id': '4270',
     'function': {'arguments': '{"currency_from": "USD", "currency_to": "SEK"}',
      'name': 'get_exchange_rate'},
     'type': 'function'}],
   'role': 'assistant'},
  {'content': "{'amount': 1.0, 'base': 'USD', 'date': '2025-02-27', 'rates': {'SEK': 10.6509}}",
   'tool_responses': [{'tool_call_id': '4270',
     'role': 'tool',
     'content': "{'amount': 1.0, 'base': 'USD', 'date': '2025-02-27', 'rates': {'SEK': 10.6509}}"}],
   'role': 'tool',
   'name': 'user'},
  {'content': 'The current exchange rate is 1 USD to 10.6509 SEK. \n',
   'role': 'user',
   'name': 'Get Exchange Rate Agent'}],
 'summary': 'The current exchange rate is 1 USD to 10.6509 SEK. \n',
 'cost': {'usage_including_cached_inference': {'total_cost': 0.0002790625,
   'gemini-1.5-flash-001': {'cost': 0.0002790625,
    'prompt_tokens': 757,
    'completion_tokens': 34,
    'total_tokens': 791}},
  'usage_excluding_cached_inference': {'total_cost': 0.0002790625,
   'gemini-1.5-flash-001': {'cost': 0.0002790625,
    'prompt_tokens': 757,
    'completion_tokens': 34,
    'total_tokens': 791}}},
 'human_input': []}

第 3 步:自定义编排

所有 AG2 代理都实现了 ConversableAgent 接口,该接口为编排提供了输入和输出架构。AG2Agent 需要构建一个可响应查询的可运行对象。默认情况下,AG2Agent 将通过将模型与工具绑定来构建此类可运行对象。

如果您打算执行以下操作,则可能需要自定义编排:(i) 实现可使用模型解决任务的助理代理,或者 (ii) 实现可执行代码并向其他代理提供反馈的用户代理,或者 (iii) 实现可使用模型和思维树推理解决任务的推理代理。为此,您必须在创建 AG2Agent 时替换默认可运行对象,方法是使用具有以下签名的 Python 函数指定 runnable_builder= 参数:


def runnable_builder(
    **runnable_kwargs,
):

这样,您就可以使用不同的选项来自定义编排逻辑。

助理客服人员

在最简单的情况下,如需创建不进行编排的 Google 助理代理,您可以替换 AG2Agentrunnable_builder

def runnable_builder(**kwargs):
    from autogen import agentchat

    return agentchat.AssistantAgent(**kwargs)

agent = agent_engines.AG2Agent(
    model=model,
    runnable_name=runnable_name,
    runnable_builder=runnable_builder,
)

用户代理

在最简单的情况下,如需创建不进行编排的 Google 助理代理,您可以替换 AG2Agentrunnable_builder

def runnable_builder(**kwargs):
    from autogen import agentchat

    return agentchat.UserProxyAgent(**kwargs)

agent = agent_engines.AG2Agent(
    model=model,
    runnable_name=runnable_name,
    runnable_builder=runnable_builder,
)

推理代理

在最简单的情况下,如需创建不进行编排的推理代理,您可以替换 AG2Agentrunnable_builder

def runnable_builder(**kwargs):
    from autogen import agentchat

    return agentchat.ReasoningAgent(**kwargs)

agent = agent_engines.AG2Agent(
    model=model,
    runnable_name=runnable_name,
    runnable_builder=runnable_builder,
)

后续步骤