Agent Engine でエージェントを開発してデプロイする

このページでは、指定した日付の 2 つの通貨間の為替レートを返すエージェントを作成し、デプロイする方法について説明します。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI and Cloud Storage APIs.

    Enable the APIs

Agent Engine の使用に必要な権限を取得するには、プロジェクトに対する次の IAM ロールを付与するよう管理者に依頼してください。

ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。

必要な権限は、カスタムロールや他の事前定義ロールから取得することもできます。

Vertex AI SDK for Python をインストールして初期化する

  1. 次のコマンドを実行して、Vertex AI SDK for Python とその他の必要なパッケージをインストールします。

    pip install --upgrade --quiet \
        "google-cloud-aiplatform[langchain,reasoningengine]" \
        cloudpickle==3.0.0 \
        "pydantic>=2.10" \
        requests
  2. ユーザーとして認証します。

    Colab

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

    from google.colab import auth
    
    auth.authenticate_user()
    

    Cloud Shell

    対応は不要です。

    ローカルシェル

    次のコマンドを実行します。

    gcloud auth application-default login
  3. 次のコードを実行して、Agent Engine をインポートし、SDK を初期化します。

    import vertexai
    from vertexai import agent_engines
    
    vertexai.init(
        project="PROJECT_ID",               # Your project ID.
        location="LOCATION",                # Your cloud region.
        staging_bucket="gs://BUCKET_NAME",  # Your staging bucket.
    )
    

エージェントを開発する

  1. LangchainAgent テンプレートを使用してエージェントをインスタンス化します。

    def get_exchange_rate(
        currency_from: str = "USD",
        currency_to: str = "EUR",
        currency_date: str = "latest",
    ):
        """Retrieves the exchange rate between two currencies on a specified date."""
        import requests
    
        response = requests.get(
            f"https://api.frankfurter.app/{currency_date}",
            params={"from": currency_from, "to": currency_to},
        )
        return response.json()
    
    agent = agent_engines.LangchainAgent(
        model="gemini-1.5-pro",
        tools=[get_exchange_rate],  # Optional.
        model_kwargs={
            "temperature": 0.28,
            "max_output_tokens": 1000,
            "top_p": 0.95,
        },
    )
    
  2. ローカルでエージェントをテストします。

    agent.query(
        input="What's the exchange rate from US dollars to Swedish currency today?"
    )
    

エージェントをデプロイする

  1. エージェントを Agent Engine マネージド ランタイムにデプロイします。

    remote_agent = agent_engines.create(
        agent,
        requirements=[
            "google-cloud-aiplatform[agent_engines,langchain]",
            "cloudpickle==3.0.0",
            "pydantic>=2.10",
            "requests",
        ],
    )
    

    これにより、Vertex AI に reasoningEngine リソースが作成されます。

  2. クエリを送信してリモート エージェントをテストします。

    remote_agent.query(
        input="What's the exchange rate from US dollars to Swedish currency today?"
    )
    

クリーンアップ

このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。

remote_agent.delete()

次のステップ