Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions quickstarts/Get_started_LiveAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@
```
"""

import os
import asyncio
import base64
import io
import os
import sys
import traceback

import cv2
Expand All @@ -60,26 +59,39 @@
import argparse

from google import genai

if sys.version_info < (3, 11, 0):
import taskgroup, exceptiongroup

asyncio.TaskGroup = taskgroup.TaskGroup
asyncio.ExceptionGroup = exceptiongroup.ExceptionGroup
from google.genai import types

FORMAT = pyaudio.paInt16
CHANNELS = 1
SEND_SAMPLE_RATE = 16000
RECEIVE_SAMPLE_RATE = 24000
CHUNK_SIZE = 1024

MODEL = "models/gemini-2.0-flash-live-001"
MODEL = "models/gemini-live-2.5-flash-preview"

DEFAULT_MODE = "camera"

client = genai.Client(http_options={"api_version": "v1beta"})
client = genai.Client(
http_options={"api_version": "v1beta"},
api_key=os.environ.get("GEMINI_API_KEY")
)


CONFIG = {"response_modalities": ["AUDIO"]}
CONFIG = types.LiveConnectConfig(
response_modalities=[
"AUDIO",
],
media_resolution="MEDIA_RESOLUTION_MEDIUM",
speech_config=types.SpeechConfig(
voice_config=types.VoiceConfig(
prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name="Zephyr")
)
),
context_window_compression=types.ContextWindowCompressionConfig(
trigger_tokens=25600,
sliding_window=types.SlidingWindow(target_tokens=12800),
),
)

pya = pyaudio.PyAudio()

Expand All @@ -105,7 +117,7 @@ async def send_text(self):
)
if text.lower() == "q":
break
await self.session.send(input=text or ".", end_of_turn=True)
await self.session.send_client_content(turns={"role": "user", "parts": [{"text": text or "."}]}, turn_complete=True)

def _get_frame(self, cap):
# Read the frameq
Expand Down Expand Up @@ -178,7 +190,7 @@ async def get_screen(self):
async def send_realtime(self):
while True:
msg = await self.out_queue.get()
await self.session.send(input=msg)
await self.session.send_realtime_input(media=msg)

async def listen_audio(self):
mic_info = pya.get_default_input_device_info()
Expand Down