このページでは、エージェントの使用に関する一般的な手順に加えて、A2A エージェントに固有の機能について説明します。
始める前に
このチュートリアルは、次の手順を読んで理解していることを前提としています。
- Agent2Agent エージェントを開発する:
A2aAgent
のインスタンスとしてエージェントを開発します。 - ユーザー認証: エージェントにクエリを実行するユーザーとして認証します。
サポートされているオペレーション
Agent Engine でホストされている A2A エージェントは、A2A プロトコルの API エンドポイントに直接対応する一連のオペレーションを公開します。
on_message_send
: エージェントに新しいメッセージを送信してタスクを開始します。on_get_task
: 既存のタスクのステータスとアーティファクトを取得します。on_cancel_task
: 実行中のタスクをキャンセルします。handle_authenticated_agent_card
: エージェントの完全な機能とスキルを取得します。
設定
Vertex AI SDK for Python
同じ構文を使用して、Vertex AI SDK for Python を使用して Agent Engine にデプロイされた A2A エージェントを操作できます。
Agent Engine で A2A を設定するには、デプロイされたエージェントのインスタンスを取得します。このインスタンスは、基盤となる A2A エンドポイントをラップし、エンドポイントを Python メソッドとして呼び出すことができます。
import vertexai
from google.genai import types
# Replace with your actual values
PROJECT_ID = "your-project-id"
LOCATION = "your-location"
REASONING_ENGINE_ID = "your-reasoning-engine-id"
AGENT_ENGINE_RESOURCE = f"projects/{PROJECT_ID}/locations/{LOCATION}/reasoningEngines/{REASONING_ENGINE_ID}"
client = vertexai.Client(
project=PROJECT_ID,
location=LOCATION,
http_options=types.HttpOptions(
api_version="v1beta1")
)
remote_agent = client.agent_engines.get(
name=AGENT_ENGINE_RESOURCE,
)
A2A Python SDK
このメソッドでは、A2A 準拠のエージェントを操作するためのクライアント ライブラリを提供する公式の A2A Python SDK を使用します。詳細については、A2A Python SDK のドキュメントをご覧ください。
まず、SDK をインストールします。
pip install a2a-sdk>=0.3.4
次に、エージェントのカードを取得してクライアント インスタンスを作成します。A2AClient
は、ディスカバリと通信を処理します。
from google.auth import default
from google.auth.transport.requests import Request
from a2a.client import ClientConfig, ClientFactory
from a2a.types import TransportProtocol
import httpx
# We assume 'agent_card' is an existing AgentCard object.
# Fetch credentials for authentication for demo purpose. Use your own auth
credentials, _ = default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
credentials.refresh(Request())
# Create the client by chaining the factory and config initialization.
factory = ClientFactory(
ClientConfig(
supported_transports=[TransportProtocol.http_json], # only support http_json
use_client_preference=True,
httpx_client=httpx.AsyncClient(
headers={
"Authorization": f"Bearer {credentials.token}",
"Content-Type": "application/json",
}
),
)
)
a2a_client = factory.create(agent_card)
Python requests ライブラリ
A2A プロトコルは標準の HTTP エンドポイント上に構築されています。これらのエンドポイントは、任意の HTTP クライアントを使用して操作できます。
エージェントカードから A2A URL を取得し、リクエスト ヘッダーを定義します。
from google.auth import default
from google.auth.transport.requests import Request
# We assume 'agent_card' is an existing object
a2a_url = agent_card.url
# Get an authentication token for demonstration purposes. Use your own authentication mechanism.
credentials, _ = default(scopes=['https://www.googleapis.com/auth/cloud-platform'])
credentials.refresh(Request())
headers = {
"Authorization": f"Bearer {credentials.token}",
"Content-Type": "application/json",
}
エージェント カードを取得する
Agent Engine は一般公開のエージェントカードを提供しません。認証済みエージェント カードを取得するには:
Vertex AI SDK for Python
response = await remote_agent.handle_authenticated_agent_card()
A2A Python SDK
response = await a2a_client.get_card()
Python requests ライブラリ
card_endpoint = f"{a2a_url}/v1/card"
response = httpx.get(card_endpoint, headers=headers)
print(json.dumps(response.json(), indent=4))
メッセージを送信する
メッセージを送信するには:
Vertex AI SDK for Python
message_data = {
"messageId": "remote-agent-message-id",
"role": "user",
"parts": [{"kind": "text", "text": "What is the exchange rate from USD to EUR today?"}],
}
response = await remote_agent.on_message_send(**message_data)
A2A Python SDK
from a2a.types import Message, Part, TextPart
import pprint
message = Message(
message_id="remote-agent-message-id",
role="user",
parts=[Part(root=TextPart(text="What's the currency rate of USD and EUR"))],
)
response_iterator = a2a_client.send_message(message)
async for chunk in response_iterator:
pprint.pp(chunk)
Python requests ライブラリ
import httpx
import json
endpoint = f"{a2a_url}/v1/message:send"
payload = {
"message": {
"messageId": "remote-agent-message-id",
"role": "1",
"content": [{"text": "What is the exchange rate from USD to EUR today?"}],
},
"metadata": {"source": "python_script"},
}
response = httpx.post(endpoint, json=payload, headers=headers)
print(json.dumps(response.json(), indent=4))
タスクを取得する
タスクとそのステータスを取得するには
Vertex AI SDK for Python
task_data = {
"id": task_id,
}
response = await remote_agent.on_get_task(**task_data)
A2A Python SDK
from a2a.types import TaskQueryParams
task_data ={
"id":task_id,
}
response = await a2a_client.get_task(TaskQueryParams(**task_data))
Python requests ライブラリ
task_end_point = f"{a2a_url}/v1/tasks/{task_id}"
response = httpx.get(task_end_point, headers=headers)
print(json.dumps(response.json(), indent=4))
タスクをキャンセルする
タスクをキャンセルするには:
Vertex AI SDK for Python
task_data = {
"id": task_id,
}
response = await remote_agent.on_cancel_task(**task_data)
A2A Python SDK
from a2a.types import TaskQueryParams
task_data ={
"id":task_id,
}
response = await a2a_client.cancel_task(TaskQueryParams(**task_data))
Python requests ライブラリ
task_end_point = f"{a2a_url}/v1/tasks/{task_id}:cancel"
response = httpx.post(task_end_point, headers=headers)
print(json.dumps(response.json(), indent=4))