Skip to content

Commit b02d26f

Browse files
Update script (#954)
1 parent 8f9eb8f commit b02d26f

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

quickstarts/Get_started_LiveAPI_NativeAudio.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
# limitations under the License.
1515

1616
"""
17+
This script lets you to talk to a Gemini native audio model using the Live API.
18+
19+
Important: **Use headphones**. This script uses the system default audio
20+
input and output, which often won't include echo cancellation. So to prevent
21+
the model from interrupting itself it is important that you use headphones.
22+
1723
## Setup
1824
1925
To install the dependencies for this script, run:
@@ -23,9 +29,11 @@
2329
pip install -U google-genai pyaudio
2430
```
2531
32+
If Python < 3.11, also install `pip install taskgroup`.
33+
2634
## API key
2735
28-
Ensure the `GOOGLE_API_KEY` environment variable is set to the api-key
36+
Ensure the `GEMINI_API_KEY` environment variable is set to the api-key
2937
you obtained from Google AI Studio.
3038
3139
## Run
@@ -53,6 +61,7 @@
5361
asyncio.TaskGroup = taskgroup.TaskGroup
5462
asyncio.ExceptionGroup = exceptiongroup.ExceptionGroup
5563

64+
5665
FORMAT = pyaudio.paInt16
5766
CHANNELS = 1
5867
SEND_SAMPLE_RATE = 16000
@@ -62,21 +71,29 @@
6271
pya = pyaudio.PyAudio()
6372

6473

65-
client = genai.Client() # GOOGLE_API_KEY must be set as env variable
74+
client = genai.Client(http_options={"api_version": "v1alpha"}) # GEMINI_API_KEY must be set as env variable
6675

67-
MODEL = "gemini-2.5-flash-preview-native-audio-dialog"
68-
CONFIG = {"response_modalities": ["AUDIO"]}
76+
system_instruction = """
77+
You are a helpful and friendly AI assistant.
78+
Your default tone is helpful, engaging, and clear, with a touch of optimistic wit.
79+
Anticipate user needs by clarifying ambiguous questions and always conclude your responses
80+
with an engaging follow-up question to keep the conversation flowing.
81+
"""
82+
83+
MODEL = "gemini-2.5-flash-native-audio-preview-09-2025"
84+
CONFIG = {
85+
"system_instruction": system_instruction,
86+
"response_modalities": ["AUDIO"],
87+
"proactivity": {'proactive_audio': True}
88+
}
6989

7090

7191
class AudioLoop:
7292
def __init__(self):
7393
self.audio_in_queue = None
7494
self.out_queue = None
75-
7695
self.session = None
77-
7896
self.audio_stream = None
79-
8097
self.receive_audio_task = None
8198
self.play_audio_task = None
8299

@@ -152,10 +169,10 @@ async def run(self):
152169
tg.create_task(self.play_audio())
153170
except asyncio.CancelledError:
154171
pass
155-
except ExceptionGroup as EG:
172+
except asyncio.ExceptionGroup as eg:
156173
if self.audio_stream:
157174
self.audio_stream.close()
158-
traceback.print_exception(EG)
175+
traceback.print_exception(eg)
159176

160177

161178
if __name__ == "__main__":

0 commit comments

Comments
 (0)