除了使用智能体的一般说明之外,本页面还介绍了特定于 AG2Agent
的功能。
准备工作
本教程假定您已阅读并遵循以下说明:
- 开发 AG2 智能体:将
agent
作为AG2Agent
的实例进行开发。 - 用户身份验证,以用户身份进行身份验证,以便查询智能体。
- 导入并初始化 SDK:初始化客户端,以获取已部署的实例(如有需要)。
获取智能体实例
如需查询 AG2Agent
,您需要先创建新实例或获取现有实例。
如需获取与特定资源 ID 对应的 AG2Agent
,请执行以下操作:
Vertex AI SDK for Python
运行以下代码:
import vertexai
client = vertexai.Client( # For service interactions via client.agent_engines
project="PROJECT_ID",
location="LOCATION",
)
agent = client.agent_engines.get(name="projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
print(agent)
其中
PROJECT_ID
是您在其中开发和部署代理的 Google Cloud 项目 ID。LOCATION
是支持的区域之一。RESOURCE_ID
是所部署智能体的 ID,以reasoningEngine
资源的形式表示。
Python 请求库
运行以下代码:
from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests
def get_identity_token():
credentials, _ = google_auth.default()
auth_request = google_requests.Request()
credentials.refresh(auth_request)
return credentials.token
response = requests.get(
f"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
)
REST API
curl \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID
使用 Vertex AI SDK for Python 时,agent
对象对应于包含以下内容的 AgentEngine
类:
- 包含所部署智能体相关信息的
agent.api_resource
。您还可以调用agent.operation_schemas()
来返回智能体所支持操作的列表。如需了解详情,请参阅支持的操作。 - 允许同步服务交互的
agent.api_client
- 允许异步服务交互的
agent.async_api_client
本部分的其余内容假定您有一个名为 agent
的 AgentEngine
实例。
支持的操作
AG2Agent
支持以下操作:
query
:用于同步获取对查询的响应。
query
方法支持以下参数:
input
:要发送给智能体的消息。max_turns
:允许的最大对话轮数。使用工具时,至少需要max_turns=2
轮对话:一轮用于生成工具参数,另一轮用于执行工具。
查询智能体
query()
方法提供了一种与代理互动的简化方式。典型的调用如下所示:
response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?", max_turns=2)
此方法处理与代理的底层通信,并以字典形式返回代理的最终响应。它相当于以下内容(完整形式):
from autogen import ConversableAgent
import dataclasses
import json
input_message: str = "What is the exchange rate from US dollars to Swedish currency?"
max_turns: int = 2
with agent._runnable._create_or_get_executor(
tools=agent._ag2_tool_objects, # Use the agent's existing tools
agent_name="user", # Default
agent_human_input_mode="NEVER", # query() enforces this
) as executor:
chat_result = executor.initiate_chat(
agent._runnable,
message=input_message,
max_turns=max_turns,
clear_history=False, # Default
summary_method="last_msg" # Default
)
response = json.loads(
json.dumps(dataclasses.asdict(chat_result)) # query() does this conversion
)
您可以通过向 query()
传递其他关键字参数来自定义智能体在 input
和 max_turns
之外的行为。
response = agent.query(
input="What is the exchange rate from US dollars to Swedish currency?",
max_turns=2,
msg_to="user" # Start the conversation with the "user" agent
)
print(response)
如需查看可用参数的完整列表,请参阅 ConversableAgent.run
文档。不过,请注意,user_input
始终会被 AG2Agent
模板替换为 False
。