めもめも

このブログに記載の内容は個人の見解であり、必ずしも所属組織の立場、戦略、意見を代表するものではありません。

Agent Engine にデプロイした ADK Agent から特定リージョンの Gemini Model を使用する方法

ADK の Agent(LlmAgent オブジェクト)が使用する Gemini Model のリージョンは、vertexai.init()location オプションで設定するか、もしくは、環境変数 GOOGLE_CLOUD_LOCATION で指定します。

import os, vertexai

PROJECT_ID = 'your project id'
LOCATION = 'us-central1'

vertexai.init(project=PROJECT_ID, location=LOCATION)

os.environ['GOOGLE_CLOUD_PROJECT'] = PROJECT_ID
os.environ['GOOGLE_CLOUD_LOCATION'] = LOCATION
os.environ['GOOGLE_GENAI_USE_VERTEXAI'] = 'True'

ただし、Agent Engine にデプロイする際は、次のように、デプロイ時に指定する location オプションでデプロイ先のリージョンが決まり、デフォルトでは、デプロイ先と同じリージョンの Gemini Model が使用されます。

from google.genai.types import HttpOptions

client = vertexai.Client(
    project=PROJECT_ID,
    location=LOCATION, # ここでデプロイ先のリージョンが決まる
    http_options=HttpOptions(
        api_version='v1beta1', base_url=f'https://{LOCATION}-aiplatform.googleapis.com/'
    ),
)

config={
    'display_name': 'google search agent',
    'requirements': [
        'google-adk==1.21.0',
    ],
    'http_options': {
        'base_url': f'https://{LOCATION}-aiplatform.googleapis.com',
        'api_version': 'v1beta1',
    },
    'staging_bucket': f'gs://{PROJECT_ID}',
}

remote_agent = client.agent_engines.create(
    agent=search_agent,
    config=config,
)

vertexai.Client()http_options オプションに指定する LOCATION は、location オプションの指定と一致する必要があります。また、config オプション内の http_options 要素に指定する LOCATION は Agent Engine にデプロイされたコードが使用する API のリージョンを指定します。こちらも整合性のために location オプションの指定と一致させるようにしてください。

それでは、デプロイ先のリージョンとは異なるリージョンの Gemini Model を呼び出すにはどうすれば良いのでしょうか?

結論から言うと、LlmAgent オブジェクトを定義する際に、次のように、model オプションに API を full resource name で指定します。

friendly_agent = LlmAgent(
    name='friendly_agent',
    model=f'projects/{PROJECT_ID}/locations/asia-northeast1/publishers/google/models/gemini-2.5-flash', ### この部分で API のリージョンを指定する
    description='Friendly agent',
    instruction='Be friendly.'
)

Full resource name の仕様については、明確なドキュメントがないのですが、Gen AI SDK のソースコード内に次のような記述があります。

github.com

  async def generate_content_stream(
      self,
      *,
      model: str,
      contents: Union[types.ContentListUnion, types.ContentListUnionDict],
      config: Optional[types.GenerateContentConfigOrDict] = None,
  ) -> AsyncIterator[types.GenerateContentResponse]:
    """Makes an API request to generate content using a model and yields the model's response in chunks.

    For the `model` parameter, supported formats for Vertex AI API include:
    - The Gemini model ID, for example: 'gemini-2.0-flash'
    - The full resource name starts with 'projects/', for example:
      'projects/my-project-id/locations/us-central1/publishers/google/models/gemini-2.0-flash'
    - The partial resource name with 'publishers/', for example:
      'publishers/google/models/gemini-2.0-flash' or
    - `/` separated publisher and model name, for example:
      'google/gemini-2.0-flash'