במדריך הזה נסביר איך להתחבר ל-Gemini Live API באמצעות Google Gen AI SDK ל-Python. במדריך הזה תלמדו איך ליצור אפליקציה מולטימודלית בזמן אמת עם קצה עורפי חזק ב-Python שמטפל בחיבור ל-API.
לפני שמתחילים
כדי להגדיר את הסביבה, צריך לבצע את השלבים הבאים.
- נכנסים לחשבון 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-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 הפרויקט. הפקודה הבאה יוצרת קובץ .env שמגדיר את משתנה הסביבה PROJECT_ID.
מחליפים את PROJECT_ID במזהה הפרויקט של פרויקט Google Cloud .
echo "PROJECT_ID=PROJECT_ID" > .env
הפעלת השרת העורפי
הקצה העורפי (main.py) מטפל בחיבור בין הלקוח לבין Gemini Live API. נקודת הכניסה היא שרת FastAPI שחושף נקודת קצה של WebSocket. הוא מקבל נתחי אודיו ווידאו מהחלק הקדמי של האפליקציה ומעביר אותם לGeminiLive הסשן. המחלקות GeminiLive ב-gemini_live.py עוטפות את 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()
)
כדי להריץ את שרת הקצה העורפי, מריצים את הפקודות הבאות:
יחסי תלות של התקנות:
pip3 install -r requirements.txtאימות באמצעות Google Cloud:
gcloud auth application-default loginמפעילים את השרת:
python3 main.py
פתיחת ממשק המשתמש של ה-frontend והתחברות ל-Gemini
החלק הקדמי של האפליקציה מנהל את הצילום וההפעלה של האודיו והווידאו. הקובץ gemini-client.js מטפל בחיבור ה-WebSocket לבק-אנד. הוא שולח נתונים של מדיה בקידוד base64 אל ה-backend ומקבל תגובות קוליות מ-Gemini Live API, שמופעלות עבור המשתמש.
כדי לפתוח את ממשק המשתמש של הקצה הקדמי ולהתחבר ל-Gemini:
- פותחים את הדפדפן ועוברים לכתובת http://localhost:8000.
- לוחצים על Connect.
איך משתמשים ב-Gemini
אפשר לנסות את הפעולות הבאות:
- הזנת טקסט: כדי לכתוב הודעת טקסט ל-Gemini, מזינים את ההודעה בשדה ההודעה ולוחצים על שליחה. Gemini מגיב להודעה באמצעות אודיו.
- קלט קולי: כדי לדבר עם Gemini, לוחצים על הפעלת המיקרופון. Gemini מגיב להנחיה באמצעות אודיו.
- קלט וידאו: כדי לאפשר ל-Gemini לראות דרך המצלמה שלכם, לוחצים על הפעלת המצלמה. אתם יכולים לדבר עם Gemini על מה שהוא רואה דרך המצלמה שלכם.