במדריך הזה נסביר איך להתחבר ל-Gemini Live API באמצעות WebSockets. במדריך הזה תלמדו איך ליצור אפליקציה מולטי-מודאלית בזמן אמת עם קצה קדמי של JavaScript רגיל ושרת Python שמטפל באימות ובפרוקסי.
לפני שמתחילים
כדי להגדיר את הסביבה, צריך לבצע את השלבים הבאים.
- נכנסים לחשבון Google Cloud . אנחנו ממליצים למשתמשים חדשים ב- Google Cloud ליצור חשבון כדי שיוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. 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.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - מתקינים את Git.
- מתקינים את 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) מטפל באימות ופועל כפרוקסי של WebSocket בין הלקוח לבין Gemini Live API.
כדי להריץ את שרת הקצה העורפי, מריצים את הפקודות הבאות:
יחסי תלות של התקנות:
pip3 install -r requirements.txtאימות באמצעות Google Cloud:
gcloud auth application-default loginמפעילים את השרת:
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:
- פותחים את הדפדפן ועוברים לכתובת http://localhost:8000.
- בשדה Project ID (מזהה הפרויקט), מזינים את המזהה של Google Cloud הפרויקט.
- לוחצים על Connect.
איך משתמשים ב-Gemini
אפשר לנסות את הפעולות הבאות:
- הזנת טקסט: אתם יכולים לכתוב הודעת טקסט ל-Gemini על ידי הזנת ההודעה בשדה ההודעה ולחיצה על שליחה. Gemini עונה להודעה באמצעות אודיו.
- קלט קולי: כדי לדבר עם Gemini, לוחצים על הפעלת המיקרופון. Gemini מגיב לפרומפט באמצעות אודיו.
- קלט וידאו: כדי לאפשר ל-Gemini לראות דרך המצלמה שלכם, לוחצים על הפעלת המצלמה. אתם יכולים לדבר עם Gemini על מה שהוא רואה דרך המצלמה שלכם.