Mulai menggunakan Gemini Live API menggunakan Google Gen AI SDK

Tutorial ini menunjukkan cara terhubung ke Gemini Live API menggunakan Google Gen AI SDK for Python. Dalam tutorial ini, Anda akan membangun aplikasi multimodal real-time dengan backend Python yang tangguh untuk menangani koneksi API.

Sebelum memulai

Selesaikan langkah-langkah berikut untuk menyiapkan lingkungan Anda.

  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. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  6. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    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. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  11. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  12. Instal Git.
  13. Instal Python 3.
  14. Meng-clone aplikasi demo

    Buat clone repositori aplikasi demo dan buka direktori tersebut:

    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
    

    Struktur project

    Aplikasi ini mencakup file berikut:

    /
    ├── 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
    

    Mengonfigurasi variabel lingkungan

    Untuk tujuan demo ini, satu-satunya variabel lingkungan yang perlu kita konfigurasi adalah variabel yang menentukan ID project Google Cloud Anda. Perintah berikut membuat file .env yang menetapkan variabel lingkungan PROJECT_ID. Ganti PROJECT_ID dengan project ID Google Cloud project Anda.

    echo "PROJECT_ID=PROJECT_ID" > .env
    

    Menjalankan server backend

    Backend (main.py) menangani koneksi antara klien dan Gemini Live API. Titik entri adalah server FastAPI yang mengekspos endpoint WebSocket. Layanan ini menerima potongan audio dan video dari frontend dan meneruskannya ke sesi GeminiLive. Class GeminiLive di gemini_live.py membungkus genai.Client untuk mengelola sesi.

    # 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()
        )
    

    Untuk menjalankan server backend, jalankan perintah berikut:

    1. Instal dependensi:

      pip3 install -r requirements.txt
      
    2. Mengautentikasi dengan Google Cloud:

      gcloud auth application-default login
      
    3. Mulai server:

      python3 main.py
      

    Buka UI frontend dan hubungkan dengan Gemini

    Frontend mengelola perekaman dan pemutaran audio dan video. File gemini-client.js menangani koneksi WebSocket ke backend. Aplikasi ini mengirimkan potongan media berenkode base64 ke backend dan menerima respons audio dari Gemini Live API, yang kemudian diputar kembali kepada pengguna.

    Untuk membuka UI frontend dan terhubung dengan Gemini, lakukan hal berikut:

    1. Buka browser Anda dan buka http://localhost:8000.
    2. Klik Hubungkan.

    Berinteraksi dengan Gemini

    Coba lakukan hal berikut:

    • Input teks: Anda dapat menulis pesan teks ke Gemini dengan memasukkan pesan Anda di kolom pesan, lalu mengklik Kirim. Gemini merespons pesan menggunakan audio.
    • Input suara: Untuk berbicara dengan Gemini, klik Mulai mikrofon. Gemini merespons perintah menggunakan audio.
    • Input video: Agar Gemini dapat melihat melalui kamera Anda, klik Mulai kamera. Anda dapat berbicara dengan Gemini tentang apa yang dilihatnya melalui kamera Anda.

    Langkah berikutnya