Google Gen AI SDK を使用して Gemini Live API を使ってみる

このチュートリアルでは、Google Gen AI SDK for Python を使用して Gemini Live API に接続する方法について説明します。このチュートリアルでは、API 接続を処理する堅牢な Python バックエンドを使用して、リアルタイムのマルチモーダル アプリケーションを構築します。

始める前に

環境を設定するには、次の手順を実行します。

  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.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.

  5. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  6. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Verify that billing is enabled for your Google Cloud project.

  9. Install the Google Cloud CLI.

  10. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  11. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  12. Git のインストール
  13. Python 3 をインストールします
  14. デモアプリのクローンを作成する

    デモアプリ リポジトリのクローンを作成し、そのディレクトリに移動します。

    git clone https://github.com/GoogleCloudPlatform/generative-ai.git &&
    cd generative-ai/gemini/multimodal-live-api/native-audio-websocket-demo-apps/plain-js-python-sdk-demo-app
    

    プロジェクトの構造

    このアプリケーションには次のファイルが含まれています。

    /
    ├── main.py                 # FastAPI server and WebSocket endpoint
    ├── gemini_live.py          # Gemini Live API wrapper using Gen AI SDK
    ├── requirements.txt        # Python dependencies
    └── frontend/
        ├── index.html          # User Interface
        ├── main.js             # Application logic
        ├── gemini-client.js    # WebSocket client for backend communication
        ├── media-handler.js    # Audio/Video capture and playback
        └── pcm-processor.js    # AudioWorklet for PCM processing
    

    環境変数を構成する

    このデモでは、 Google Cloud プロジェクトの ID を定義する環境変数のみを構成する必要があります。次のコマンドは、環境変数 PROJECT_ID を設定する .env ファイルを作成します。PROJECT_ID は、 Google Cloud プロジェクトのプロジェクト ID に置き換えます。

    echo "PROJECT_ID=PROJECT_ID" > .env
    

    バックエンド サーバーを実行する

    バックエンド(main.py)は、クライアントと Gemini Live API 間の接続を処理します。エントリ ポイントは、WebSocket エンドポイントを公開する FastAPI サーバーです。フロントエンドから音声と動画のチャンクを受け取り、GeminiLive セッションに転送します。gemini_live.pyGeminiLive クラスは、セッションを管理するために genai.Client をラップします。

    # Connects using the SDK
    async with self.client.aio.live.connect(model=self.model, config=config) as session:
        # Manages input/output queues
        await asyncio.gather(
            send_audio(),
            send_video(),
            receive_responses()
        )
    

    次のコマンドを実行して、バックエンド サーバーを実行します。

    1. 依存関係をインストールします。

      pip3 install -r requirements.txt
      
    2. Google Cloudで認証します。

      gcloud auth application-default login
      
    3. サーバーを開始します。

      python3 main.py
      

    フロントエンド UI を開き、Gemini に接続する

    フロントエンドは、音声と動画のキャプチャと再生を管理します。gemini-client.js ファイルは、バックエンドへの WebSocket 接続を処理します。base64 でエンコードされたメディア チャンクをバックエンドに送信し、Gemini Live API から音声レスポンスを受信して、ユーザーに再生します。

    フロントエンド UI を開いて Gemini に接続する手順は次のとおりです。

    1. ブラウザを開き、http://localhost:8000 に移動します。
    2. [接続] をクリックします。

    Gemini とやり取りする

    次の手順をお試しください。

    • テキスト入力: メッセージ フィールドにメッセージを入力して [送信] をクリックすると、Gemini にテキスト メッセージを送信できます。Gemini が音声でメッセージに返信します。
    • 音声入力: Gemini に話しかけるには、[マイクを開始] をクリックします。Gemini が音声でプロンプトに応答します。
    • 動画入力: Gemini がカメラを通して見えるようにするには、[カメラを開始] をクリックします。カメラに映っているものについて Gemini に話しかけることができます。

    次のステップ