除了使用代理程式的一般操作說明外,本頁面也會說明 AdkApp
的專屬功能。
事前準備
本教學課程假設您已詳閱並按照下列教學課程的指示操作:
- 開發 Agent Development Kit 代理程式:將
agent
開發為AdkApp
的例項。 - 使用者驗證:以使用者身分進行驗證,以便查詢代理程式。
如要查詢 ADK 應用程式,您必須先建立新的 ADK 應用程式執行個體,或取得現有執行個體。
如要取得與特定資源 ID 相對應的 ADK 應用程式:
Python 適用的 Vertex AI SDK
請執行下列程式碼:
from vertexai import agent_engines
adk_app = agent_engines.get(RESOURCE_ID)
或者,您也可以提供代理程式的完整資源名稱:
adk_app = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
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
支援的作業
AdkApp
支援下列作業:
async_stream_query
:用於串流查詢的回覆。async_create_session
:用於建立新工作階段。async_list_sessions
:列出可用的工作階段。async_get_session
:用於擷取特定工作階段。async_delete_session
:刪除特定工作階段。
如要列出所有支援的作業,請執行下列指令:
Python 適用的 Vertex AI SDK
請執行下列程式碼:
adk_app.operation_schemas()
Python requests 程式庫
請執行下列程式碼:
import json
json.loads(response.content).get("spec").get("classMethods")
REST API
以 spec.class_methods
表示,來自 curl 要求的相關回應。
管理工作階段
AdkApp
會在您將代理程式部署至 Vertex AI Agent Engine 後,使用雲端式代管工作階段。本節說明如何使用受管理的工作階段。
建立工作階段
如要為使用者建立工作階段,請按照下列步驟操作:
Python 適用的 Vertex AI SDK
session = await adk_app.async_create_session(user_id="USER_ID")
Python requests 程式庫
請執行下列程式碼:
from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests
import json
def get_identity_token():
credentials, _ = google_auth.default()
auth_request = google_requests.Request()
credentials.refresh(auth_request)
return credentials.token
response = requests.post(
f"https://{adk_app.api_client.api_endpoint}/v1/{adk_app.resource_name}:query",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "async_create_session",
"input": {"user_id": "USER_ID"},
}),
)
print(response.content)
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:query -d '{"class_method": "async_create_session", "input": {"user_id": "USER_ID"},}'
其中 USER_ID 是使用者定義的 ID,字元上限為 128 個。
列出工作階段
如要列出使用者的工作階段,請按照下列步驟操作:
Python 適用的 Vertex AI SDK
await adk_app.async_list_sessions(user_id="USER_ID")
要求
請執行下列程式碼:
from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests
import json
def get_identity_token():
credentials, _ = google_auth.default()
auth_request = google_requests.Request()
credentials.refresh(auth_request)
return credentials.token
response = requests.post(
f"https://{adk_app.api_client.api_endpoint}/v1/{adk_app.resource_name}:query",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "async_list_sessions",
"input": {"user_id": "USER_ID"},
}),
)
print(response.content)
REST
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:query -d '{"class_method": "async_list_sessions", "input": {"user_id": "USER_ID"},}'
其中 USER_ID 是使用者定義的 ID,字元上限為 128 個。
取得工作階段
如要取得特定工作階段,您需要使用者 ID 和工作階段 ID:
Python 適用的 Vertex AI SDK
session = await adk_app.async_get_session(user_id="USER_ID", session_id="SESSION_ID")
要求
請執行下列程式碼:
from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests
import json
def get_identity_token():
credentials, _ = google_auth.default()
auth_request = google_requests.Request()
credentials.refresh(auth_request)
return credentials.token
response = requests.post(
f"https://{adk_app.api_client.api_endpoint}/v1/{adk_app.resource_name}:query",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "async_get_session",
"input": {"user_id": "USER_ID", "session_id": "SESSION_ID"},
}),
)
print(response.content)
REST
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:query -d '{"class_method": "async_get_session", "input": {"user_id": "USER_ID", "session_id": "SESSION_ID"},}'
刪除工作階段
如要刪除工作階段,您需要使用者 ID 和工作階段 ID:
Python 適用的 Vertex AI SDK
await adk_app.async_delete_session(user_id="USER_ID", session_id="SESSION_ID")
要求
請執行下列程式碼:
from google import auth as google_auth
from google.auth.transport import requests as google_requests
import requests
import json
def get_identity_token():
credentials, _ = google_auth.default()
auth_request = google_requests.Request()
credentials.refresh(auth_request)
return credentials.token
response = requests.post(
f"https://{adk_app.api_client.api_endpoint}/v1/{adk_app.resource_name}:query",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "async_delete_session",
"input": {"user_id": "USER_ID", "session_id": "SESSION_ID"},
}),
)
print(response.content)
REST
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:query -d '{"class_method": "async_delete_session", "input": {"user_id": "USER_ID", "session_id": "SESSION_ID"},}'
串流查詢的回覆
如要在工作階段中串流播放代理程式的回覆:
Python 適用的 Vertex AI SDK
async for event in adk_app.async_stream_query(
user_id="USER_ID",
session_id="SESSION_ID", # Optional
message="What is the exchange rate from US dollars to SEK today?",
):
print(event)
要求
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
requests.post(
f"https://{adk_app.api_client.api_endpoint}/v1/{adk_app.resource_name}:streamQuery",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "async_stream_query",
"input": {
"user_id": "USER_ID",
"session_id": "SESSION_ID",
"message": "What is the exchange rate from US dollars to SEK today?",
},
}),
stream=True,
)
REST
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:streamQuery?alt=sse -d '{
"class_method": "async_stream_query",
"input": {
"user_id": "USER_ID",
"session_id": "SESSION_ID",
"message": "What is the exchange rate from US dollars to SEK today?",
}
}'
後續步驟
- 使用代理程式。
- 評估代理程式。
- 管理已部署的代理程式。
- 取得支援。