このページでは、エージェントの使用に関する一般的な手順に加えて、AG2Agent に固有の機能について説明します。
始める前に
このチュートリアルは、次の手順を読んで理解していることを前提としています。
- AG2 エージェントを開発する:
AG2Agentのインスタンスとしてagentを開発します。 - ユーザー認証: エージェントにクエリを実行するユーザーとして認証します。
- 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は、reasoningEngineリソースとしてのデプロイ済みエージェントの ID です。
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_IDVertex 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が必要です。1 回のターンでツール引数を生成し、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 ドキュメントをご覧ください。ただし、AG2Agent テンプレートでは user_input は常に False にオーバーライドされます。