使用 LlamaIndex 查詢管道代理程式

事前準備

本教學課程假設您已詳閱並按照下列教學課程的指示操作:

取得代理程式的執行個體

如要查詢 LlamaIndexQueryPipelineAgent,請先建立新執行個體取得現有執行個體

如要取得特定資源 ID 對應的 LlamaIndexQueryPipelineAgent

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)

其中

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

使用 Python 適用的 Vertex AI SDK 時,agent 物件會對應至 AgentEngine 類別,其中包含下列項目:

  • 包含已部署代理程式相關資訊的 agent.api_resource。 您也可以呼叫 agent.operation_schemas(),傳回代理程式支援的作業清單。詳情請參閱「支援的作業」。
  • agent.api_client,可進行同步服務互動
  • agent.async_api_client,可進行非同步服務互動

本節其餘部分假設您有名為 agentAgentEngine 執行個體。

支援的作業

LlamaIndexQueryPipelineAgent 支援下列作業:

  • query:用於同步取得查詢的回覆。

query 方法支援下列引數類型:

  • input:要傳送給服務專員的訊息。

查詢代理程式

指令:

agent.query(input="What is Paul Graham's life in college?")

等同於下列項目 (完整形式):

agent.query(input={"input": "What is Paul Graham's life in college?"})

如要自訂輸入字典,請參閱「自訂提示範本」。

您也可以將其他關鍵字引數傳遞至 query(),進一步自訂代理程式的行為 (超出 input 的範圍)。

response = agent.query(
    input={
      "input" = [
        "What is Paul Graham's life in college?",
        "How did Paul Graham's college experience shape his career?",
        "How did Paul Graham's college experience shape his entrepreneurial mindset?",
      ],
    },
    batch=True  # run the pipeline in batch mode and pass a list of inputs.
)
print(response)

如需可用參數的完整清單,請參閱 QueryPipeline.run 程式碼

後續步驟