במדריך הזה נסביר איך להתחבר ל-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.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
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.
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.-
התקינו את ה-CLI של Google Cloud.
-
אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.
-
כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:
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) מטפל באימות ופועל כשרת proxy של 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 על מה שהוא רואה דרך המצלמה שלכם.