事前準備
本教學課程假設您已詳閱並按照下列教學課程的指示操作:
- 開發自訂代理程式:開發自訂
agent
。 - 使用者驗證:以使用者身分進行驗證,以便查詢代理程式。
- 匯入並初始化 SDK,以便初始化用戶端來取得已部署的執行個體 (如有需要)。
取得代理程式的執行個體
如要查詢代理程式,首先需要代理程式的執行個體。您可以建立新的代理程式執行個體,或取得現有的代理程式執行個體。
如要取得與特定資源 ID 相對應的服務專員:
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
資源。
要求
請執行下列程式碼:
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
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
,可進行非同步服務互動
本節其餘部分假設您有名為 agent
的執行個體。
列出支援的作業
在本機開發代理程式時,您可以存取並瞭解代理程式支援的作業。如要使用已部署的代理程式,您可以列舉代理程式支援的作業:
Vertex AI SDK for Python
請執行下列程式碼:
print(agent.operation_schemas())
要求
請執行下列程式碼:
import json
json.loads(response.content).get("spec").get("classMethods")
REST
以 spec.class_methods
表示,來自 curl 要求的相關回應。
每項作業的結構定義都是字典,其中記錄了可呼叫的代理程式方法資訊。支援的作業集取決於您用來開發代理程式的架構:
舉例來說,以下是 LangchainAgent
的 query
作業結構定義:
{'api_mode': '',
'name': 'query',
'description': """Queries the Agent with the given input and config.
Args:
input (Union[str, Mapping[str, Any]]):
Required. The input to be passed to the Agent.
config (langchain_core.runnables.RunnableConfig):
Optional. The config (if any) to be used for invoking the Agent.
Returns:
The output of querying the Agent with the given input and config.
""", ' ',
'parameters': {'$defs': {'RunnableConfig': {'description': 'Configuration for a Runnable.',
'properties': {'configurable': {...},
'run_id': {...},
'run_name': {...},
...},
'type': 'object'}},
'properties': {'config': {'nullable': True},
'input': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}},
'required': ['input'],
'type': 'object'}}
其中
name
是作業的名稱 (即名為query
的作業的agent.query
)。api_mode
是作業的 API 模式 (""
代表同步,"stream"
代表串流)。description
是根據方法 docstring 的作業說明。parameters
是 OpenAPI 結構定義格式的輸入引數結構定義。
使用支援的作業查詢代理程式
如果是自訂代理程式,您可以使用開發代理程式時定義的任何查詢或串流作業:
請注意,部分架構僅支援特定查詢或串流作業:
架構 | 支援的查詢作業 |
---|---|
代理開發套件 | async_stream_query |
LangChain | query 、stream_query |
LangGraph | query 、stream_query |
AG2 | query |
LlamaIndex | query |
查詢代理程式
使用 query
作業查詢代理程式:
Vertex AI SDK for Python
agent.query(input="What is the exchange rate from US dollars to Swedish Krona today?")
要求
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID:query",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "query",
"input": {
"input": "What is the exchange rate from US dollars to Swedish Krona today?"
}
})
)
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": "query",
"input": {
"input": "What is the exchange rate from US dollars to Swedish Krona today?"
}
}'
查詢回應是類似本機應用程式測試輸出的字串:
{"input": "What is the exchange rate from US dollars to Swedish Krona today?",
# ...
"output": "For 1 US dollar you will get 10.7345 Swedish Krona."}
串流傳送服務專員的回覆
使用 stream_query
作業,從代理程式串流傳輸回覆:
Vertex AI SDK for Python
agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
for response in agent.stream_query(
input="What is the exchange rate from US dollars to Swedish Krona today?"
):
print(response)
要求
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://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID:streamQuery",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {get_identity_token()}",
},
data=json.dumps({
"class_method": "stream_query",
"input": {
"input": "What is the exchange rate from US dollars to Swedish Krona 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": "stream_query",
"input": {
"input": "What is the exchange rate from US dollars to Swedish Krona today?"
}
}'
Vertex AI Agent Engine 會以疊代生成的物件序列形式,串流傳輸回覆內容。舉例來說,一組三個回覆可能如下所示:
{'actions': [{'tool': 'get_exchange_rate', ...}]} # first response
{'steps': [{'action': {'tool': 'get_exchange_rate', ...}}]} # second response
{'output': 'The exchange rate is 11.0117 SEK per USD as of 2024-12-03.'} # final response
非同步查詢代理程式
如果您在開發代理程式時定義了 async_query
作業,Python 適用的 Vertex AI SDK 支援代理程式的用戶端非同步查詢:
Vertex AI SDK for Python
agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
response = await agent.async_query(
input="What is the exchange rate from US dollars to Swedish Krona today?"
)
print(response)
查詢回應是與本機測試輸出內容相同的字典:
{"input": "What is the exchange rate from US dollars to Swedish Krona today?",
# ...
"output": "For 1 US dollar you will get 10.7345 Swedish Krona."}
從代理程式非同步串流回應
如果您在開發代理程式時定義了 async_stream_query
作業,可以使用其中一項作業 (例如 async_stream_query
) 從代理程式非同步串流回應:
Vertex AI SDK for Python
agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
async for response in agent.async_stream_query(
input="What is the exchange rate from US dollars to Swedish Krona today?"
):
print(response)
async_stream_query
作業會在幕後呼叫相同的 streamQuery
端點,並以非同步方式串流回應,做為一系列反覆產生的物件。舉例來說,一組三個回覆可能如下所示:
{'actions': [{'tool': 'get_exchange_rate', ...}]} # first response
{'steps': [{'action': {'tool': 'get_exchange_rate', ...}}]} # second response
{'output': 'The exchange rate is 11.0117 SEK per USD as of 2024-12-03.'} # final response
回覆內容應與本機測試期間產生的內容相同。
後續步驟
- 使用代理人。
- 評估代理程式。
- 管理已部署的代理程式。
- 取得支援。