使用 AG2 代理程式

除了使用代理程式的一般操作說明外,本頁面還說明 AG2Agent 的專屬功能。

事前準備

本教學課程假設您已詳閱並按照下列教學課程的指示操作:

取得代理程式的執行個體

如要查詢 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)

其中

Python requests 程式庫

請執行下列程式碼:

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

使用 Python 適用的 Vertex AI SDK 時,agent 物件會對應至 AgentEngine 類別,其中包含下列項目:

  • 包含已部署代理程式相關資訊的 agent.api_resource。 您也可以呼叫 agent.operation_schemas(),傳回代理程式支援的作業清單。詳情請參閱「支援的作業」。
  • agent.api_client,可進行同步服務互動
  • agent.async_api_client,可進行非同步服務互動

本節其餘部分假設您有名為 agentAgentEngine 執行個體。

支援的作業

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(),而不只是 inputmax_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 說明文件。不過請注意,AG2Agent 範本一律會將 user_input 覆寫為 False

後續步驟