無論代理程式是本機執行還是遠端部署,查詢代理程式的程式碼都相同。因此,在本頁中,agent
一詞可互換指稱 local_agent
或 remote_agent
。由於不同架構支援的作業集合各異,我們會提供架構專屬範本的使用說明:
架構 | 說明 |
---|---|
代理開發套件 (預先發布版) | 這項服務是根據 Google 內部最佳做法設計,適用於需要快速製作原型並部署可靠的服務代理程式解決方案的開發人員或團隊。 |
LangChain | 預先定義的設定和抽象概念,可讓您更輕鬆地使用基本用途。 |
LangGraph | 以圖形為基礎的方法定義工作流程,並提供進階的人機迴圈和快轉/重播功能。 |
AG2 (舊稱 AutoGen) | AG2 提供多代理對話框架,做為建構 LLM 工作流程的高層級抽象。 |
LlamaIndex (預先發布版) | LlamaIndex 的查詢管道提供高階介面,可用來建立檢索增強生成 (RAG) 工作流程。 |
如果自訂代理程式並非以特定架構範本為依據,您可以按照下列步驟操作:
- 使用者驗證。
- 取得代理程式例項。
- 查詢支援的作業。
- 查詢代理程式。
- (如適用) 串流傳輸服務專員的回應。
步驟 1:使用者驗證
請按照設定環境的操作說明操作。
步驟 2:取得代理程式執行個體
如要查詢代理程式,您必須先取得代理程式的執行個體。您可以建立新的代理程式執行個體,或取得現有的代理程式執行個體。
如要取得與特定資源 ID 對應的服務專員,請按照下列步驟操作:
Python 適用的 Vertex AI SDK
請執行下列程式碼:
from vertexai import agent_engines
agent = agent_engines.get("RESOURCE_ID")
或者,您也可以提供代理程式的完整資源名稱:
agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
要求
請執行下列程式碼:
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
本節其餘部分假設您有一個名為 agent
的執行個體。
步驟 3:支援的作業
在本機開發服務時,您可以存取並瞭解服務支援的作業。如要使用已部署的代理程式,您可以列舉其支援的作業:
Python 適用的 Vertex AI SDK
請執行下列程式碼:
agent.operation_schemas()
要求
請執行下列程式碼:
import json
json.loads(response.content).get("spec").get("classMethods")
REST
在 curl 要求的回應中以 spec.class_methods
表示。
每項作業的結構定義是字典,可記錄可呼叫的代理程式方法資訊。以下是同步作業的作業結構定義範例:
以下指令會以 JSON 格式提供與 remote_app
物件作業相對應的結構定義清單:
agent.operation_schemas()
舉例來說,以下是 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 結構定義格式。
步驟 4:查詢服務專員
如要使用支援的作業 (例如 query
) 查詢服務代理,請按照下列步驟操作:
Python 適用的 Vertex AI SDK
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."}
步驟 5:串流傳輸服務專員的回覆
如有需要,您可以使用代理程式其中一個作業 (例如 stream_query
) 串流傳輸回應:
Python 適用的 Vertex AI SDK
agent = agent_engines.get("projects/PROJECT_ID/locations/LOCATION/reasoningEngines/RESOURCE_ID")
agent.stream_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: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
後續步驟
- 使用 LangChain 代理程式。
- 使用 LangGraph 代理程式。
- 使用 AG2 代理程式。
- 使用 LlamaIndex 查詢管道代理程式。
- 評估服務專員。
- 管理已部署的服務專員。
- 取得支援。