איך מתחילים להשתמש ב-Gemini Live API באמצעות WebSockets

במדריך הזה נסביר איך להתחבר ל-Gemini Live API באמצעות WebSockets. במדריך הזה נסביר איך ליצור אפליקציה מולטי-מודאלית בזמן אמת עם קצה קדמי של JavaScript רגיל ושרת Python שמטפל באימות ובפרוקסי.

לפני שמתחילים

כדי להגדיר את הסביבה, צריך לבצע את השלבים הבאים.

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  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. Enable the Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. התקינו את ה-CLI של Google Cloud.

  6. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  7. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  8. 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

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

  10. Enable the Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  11. התקינו את ה-CLI של Google Cloud.

  12. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  13. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  14. התקנה של Git.
  15. מתקינים את Python 3.

שיבוט של אפליקציית ההדגמה

משכפלים את מאגר אפליקציית ההדגמה ועוברים לספרייה הזו:

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

מבנה הפרויקט

האפליקציה כוללת את הקבצים הבאים:

/
├── server.py            # WebSocket proxy + HTTP server
├── requirements.txt     # Python dependencies
└── frontend/
    ├── index.html       # UI
    ├── geminilive.js    # Gemini API client
    ├── mediaUtils.js    # Audio/video streaming
    ├── tools.js         # Custom tool definitions
    └── script.js        # App logic

הפעלת השרת העורפי

הקצה העורפי (server.py) מטפל באימות ופועל כשרת proxy של WebSocket בין הלקוח לבין Gemini Live API.

כדי להריץ את שרת הקצה העורפי, מריצים את הפקודות הבאות:

  1. יחסי תלות של התקנות:

    pip3 install -r requirements.txt
    
  2. אימות באמצעות Google Cloud:

    gcloud auth application-default login
    
  3. מפעילים את השרת:

    python3 server.py
    

פתיחת ממשק המשתמש של ה-frontend והתחברות ל-Gemini

הפרונט-אנד מנהל את הצילום וההפעלה של אודיו ווידאו. הקובץ geminilive.js מטפל בחיבור WebSocket לבק-אנד.

const client = new GeminiLiveAPI(proxyUrl, projectId, model);
client.addFunction(toolInstance); // Add custom tools
client.connect(accessToken); // Connect (token optional with proxy)

כדי לפתוח את ממשק המשתמש של הקצה הקדמי ולהתחבר ל-Gemini:

  1. פותחים את הדפדפן ועוברים לכתובת http://localhost:8000.
  2. בשדה Project ID (מזהה הפרויקט), מזינים את המזהה של הפרויקט Google Cloud .
  3. לוחצים על Connect.

איך משתמשים ב-Gemini

אפשר לנסות את הפעולות הבאות:

  • הזנת טקסט: כדי לכתוב הודעת טקסט ל-Gemini, מזינים את ההודעה בשדה ההודעה ולוחצים על שליחה. Gemini מגיב להודעה באמצעות אודיו.
  • קלט קולי: כדי לדבר עם Gemini, לוחצים על הפעלת המיקרופון. ‫Gemini מגיב להנחיה באמצעות אודיו.
  • קלט וידאו: כדי לאפשר ל-Gemini לראות דרך המצלמה שלכם, לוחצים על הפעלת המצלמה. אתם יכולים לדבר עם Gemini על מה שהוא רואה דרך המצלמה שלכם.

המאמרים הבאים