チュートリアル: エクスプレス モードの Agent Platform API

エクスプレス モードの Gemini Enterprise Agent Platform では、Agent Platform で利用可能なコア生成 AI 機能を試すことができます。このチュートリアルでは、エクスプレス モードで Agent Platform API を使用して次のタスクを行う方法について説明します。

  • エクスプレス モード用の Google Gen AI SDK をインストールして初期化します。
  • 次のものを含むリクエストを Gemini for Google Cloud API に送信します。

    • ストリーミング リクエスト
    • 非ストリーミング リクエスト
    • 関数呼び出しリクエスト

始める前に

このドキュメントで説明するタスクを実行する前に、エクスプレス モードに登録してください。

エクスプレス モード用の SDK をインストールして初期化する

Google Gen AI SDK を使用すると、Google 生成 AI モデルと機能を使用して、AI 搭載アプリケーションを構築できます。エクスプレス モードで Agent Platform を使用する場合は、生成した API キーを使用して認証するために google-genai パッケージをインストールして初期化します。

インストール

エクスプレス モード用の Google Gen AI SDK をインストールするには、次のコマンドを実行します。

# Developer TODO: If you're using Colab, uncomment the following lines:
# from google.colab import auth
# auth.authenticate_user()

!pip install --upgrade google-genai

Colaboratory を使用している場合は、インストール後に再起動を求めるメッセージが表示されたら、ランタイムを再起動します。

初期化

エクスプレス モードの API キーを構成し、enterprise=True でクライアントを初期化します。API キーの取得の詳細については、エクスプレス モードの Agent Platform の概要をご覧ください。

from google import genai
from google.genai import types

# Developer TODO: Replace YOUR_API_KEY with your API key.
API_KEY = "YOUR_API_KEY"

client = genai.Client(
    enterprise=True, api_key=API_KEY
)

Gemini for Google Cloud API にリクエストを送信する

Gemini for Google Cloud API には、ストリーミング リクエストまたは非ストリーミング リクエストを送信できます。ストリーミング リクエストは、リクエストの処理中にレスポンスをチャンクで返します。非ストリーミング リクエストは、処理が完了した後に完全なレスポンスを返します。

ストリーミング リクエスト

ストリーミング リクエストを送信するには、client.models.generate_content_stream() を呼び出し、チャンクが到着するたびに各チャンクを出力します。

from google import genai
from google.genai import types

def generate():
  client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(
          thinking_level="MEDIUM",
      ),
      seed=5,
      max_output_tokens=1024,
      stop_sequences=["STOP!"],
      safety_settings=[
          types.SafetySetting(
              category="HARM_CATEGORY_HATE_SPEECH",
              threshold="BLOCK_ONLY_HIGH",
          )
      ],
  )
  for chunk in client.models.generate_content_stream(
      model="gemini-3.8-flash",
      contents="Explain bubble sort to me",
      config=config,
  ):
    print(chunk.text, end="")

generate()

非ストリーミング リクエスト

次のコードサンプルは、Gemini 3.8 Flash(gemini-3.8-flash)にストリーミング以外のリクエストを送信する関数を定義しています。このサンプルは、思考レベル、出力パラメータ、安全性設定を構成する方法を示しています。

from google import genai
from google.genai import types

def generate():
  client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")

  config = types.GenerateContentConfig(
      thinking_config=types.ThinkingConfig(
          thinking_level="MEDIUM",
      ),
      seed=5,
      max_output_tokens=1024,
      stop_sequences=["STOP!"],
      safety_settings=[
          types.SafetySetting(
              category="HARM_CATEGORY_HATE_SPEECH",
              threshold="BLOCK_ONLY_HIGH",
          )
      ],
  )
  response = client.models.generate_content(
      model="gemini-3.8-flash",
      contents="Explain bubble sort to me",
      config=config,
  )
  print(response.text)

generate()

関数呼び出しリクエスト

次のコードサンプルは、関数ツールを宣言し、3.8 Flash に最初のプロンプトを送信し、レスポンスで関数呼び出し部分を受け取り、一致する FunctionResponse をモデルに送り返して最終レスポンスを生成します。

from google import genai
from google.genai import types

client = genai.Client(enterprise=True, api_key="YOUR_API_KEY")

get_weather_declaration = types.FunctionDeclaration(
    name="get_current_weather",
    description="Gets the current weather in a given city.",
    parameters={
        "type": "OBJECT",
        "properties": {
            "location": {
                "type": "STRING",
                "description": "The city and state, such as Boston, MA.",
            },
            "unit": {
                "type": "STRING",
                "enum": ["C", "F"],
            },
        },
        "required": ["location"],
    },
)
tools = [types.Tool(function_declarations=[get_weather_declaration])]

prompt = "What is the weather in Boston?"
first_response = client.models.generate_content(
    model="gemini-3.8-flash",
    contents=prompt,
    config=types.GenerateContentConfig(tools=tools),
)

function_call = first_response.function_calls[0]
contents = [
    types.Content(role="user", parts=[types.Part(text=prompt)]),
    first_response.candidates[0].content,
    types.Content(
        role="user",
        parts=[
            types.Part(
                function_response=types.FunctionResponse(
                    id=function_call.id,
                    name=function_call.name,
                    response={"weather": "sunny", "temperature": "72F"},
                )
            )
        ],
    ),
]

response = client.models.generate_content(
    model="gemini-3.8-flash",
    contents=contents,
    config=types.GenerateContentConfig(tools=tools),
)
print(response.text)

クリーンアップ

このチュートリアルでは Google Cloud リソースは作成されないため、料金が発生しないようにクリーンアップする必要はありません。

次のステップ