LlamaIndex Query Pipeline エージェントを使用する

始める前に

このチュートリアルは、次の手順を読んで理解していることを前提としています。

エージェントのインスタンスを取得する

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 リクエスト ライブラリ

次のコードを実行します。

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

Vertex AI SDK for Python を使用する場合、agent オブジェクトは、次のものを含む AgentEngine クラスに対応します。

  • デプロイされたエージェントに関する情報を含む agent.api_resourceagent.operation_schemas() を呼び出して、エージェントがサポートするオペレーションのリストを返すこともできます。詳しくは、サポートされているオペレーションをご覧ください。
  • 同期サービス インタラクションを可能にする agent.api_client
  • 非同期サービス インタラクションを可能にする agent.async_api_client

このセクションの残りの部分では、agent という名前の AgentEngine インスタンスがあることを前提としています。

サポートされているオペレーション

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 コードをご覧ください。

次のステップ