Skip to content
Closed
Changes from all commits
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
114 changes: 11 additions & 103 deletions quickstarts/Get_started_LiveAPI.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@
"from google.colab import userdata\n",
"import os\n",
"\n",
"os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY')\n"
"GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')\n",
"os.environ['GOOGLE_API_KEY'] = GOOGLE_API_KEY\n"
]
},
{
Expand All @@ -160,16 +161,12 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"metadata": {
"id": "HghvVpbU0Uap"
},
"outputs": [],
"source": [
"from google import genai\n",
"from google.genai import types\n",
"client = genai.Client(api_key=GOOGLE_API_KEY)"
]
"source": "from google import genai\nfrom google.genai import types\nclient = genai.Client(api_key=GOOGLE_API_KEY)"
},

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I run this cell in Colab, it throws a NameError: name 'GOOGLE_API_KEY' is not defined. We need to fetch the key from the environment.

{
"cell_type": "markdown",
Expand All @@ -184,14 +181,12 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {
"id": "27Fikag0xSaB"
},
"outputs": [],
"source": [
"MODEL = 'gemini-2.5-flash-native-audio-preview-09-2025' # @param ['gemini-2.0-flash-live-001', 'gemini-live-2.5-flash-preview', 'gemini-2.5-flash-native-audio-preview-09-2025'] {allow-input: true, isTemplate: true}"
]
"source": "MODEL = 'gemini-3.1-flash-live-preview' # @param ['gemini-3.1-flash-live-preview', 'gemini-2.0-flash-live-001', 'gemini-live-2.5-flash-preview', 'gemini-2.5-flash-native-audio-preview-09-2025'] {allow-input: true, isTemplate: true}\n\n# Note: 'gemini-2.5-flash-native-audio-preview-*' models do not support TEXT\n# response modality. If you select one, make sure to use AUDIO modality only.\nif 'native-audio' in MODEL:\n print(\"Warning: native audio models only support AUDIO response modality, not TEXT.\")"
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -264,9 +259,7 @@
"async with client.aio.live.connect(model=MODEL, config=config) as session:\n",
" message = \"Hello? Gemini are you there?\"\n",
" print(\"> \", message, \"\\n\")\n",
" await session.send_client_content(\n",
" turns={\"role\": \"user\", \"parts\": [{\"text\": message}]}, turn_complete=True\n",
" )\n",
" await session.send_realtime_input(text=message)\n",
"\n",
" # For text responses, When the model's turn is complete it breaks out of the loop.\n",
" turn = session.receive()\n",
Expand Down Expand Up @@ -373,9 +366,7 @@
" with wave_file(file_name) as wav:\n",
" message = \"Hello? Gemini are you there?\"\n",
" print(\"> \", message, \"\\n\")\n",
" await session.send_client_content(\n",
" turns={\"role\": \"user\", \"parts\": [{\"text\": message}]}, turn_complete=True\n",
" )\n",
" await session.send_realtime_input(text=message)\n",
"\n",
" turn = session.receive()\n",
" async for n,response in async_enumerate(turn):\n",
Expand Down Expand Up @@ -493,9 +484,7 @@
" logger.debug('send')\n",
"\n",
" # Send the message to the model.\n",
" await self.session.send_client_content(\n",
" turns={\"role\": \"user\", \"parts\": [{\"text\": text}]}, turn_complete=True\n",
" )\n",
" await self.session.send_realtime_input(text=text)\n",
" logger.debug('sent')\n",
" yield text\n",
"\n",
Expand Down Expand Up @@ -680,93 +669,12 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": null,
"metadata": {
"id": "cbkoDa1ve_C5"
},
"outputs": [],
"source": [
"import asyncio\n",
"import traceback\n",
"from asyncio.exceptions import CancelledError\n",
"\n",
"last_handle = None\n",
"\n",
"#MODEL = \"gemini-live-2.5-flash-preview\"\n",
"\n",
"client = genai.Client(api_key=GOOGLE_API_KEY)\n",
"\n",
"async def async_enumerate(aiterable):\n",
" n=0\n",
" async for item in aiterable:\n",
" yield n, item\n",
" n+=1\n",
"\n",
"\n",
"def show_response(response):\n",
" new_handle = None\n",
" if text := response.text:\n",
" print(text, end=\"\")\n",
" else:\n",
" print(response.model_dump_json(indent=2, exclude_none=True))\n",
" if response.session_resumption_update:\n",
" new_handle = response.session_resumption_update.new_handle\n",
" return new_handle\n",
"\n",
"\n",
"async def clock():\n",
" time = 0\n",
" while True:\n",
" await asyncio.sleep(60)\n",
" time += 1\n",
" print(f\"{time}:00\")\n",
"\n",
"\n",
"async def recv(session):\n",
" global last_handle\n",
" try:\n",
" while True:\n",
" async for response in session.receive():\n",
" new_handle = show_response(response)\n",
" if new_handle:\n",
" last_handle = new_handle\n",
" except asyncio.CancelledError:\n",
" pass\n",
"\n",
"\n",
"async def send(session):\n",
" while True:\n",
" message = await asyncio.to_thread(input, \"message > \")\n",
" if message.lower() == \"q\":\n",
" break\n",
" await session.send_client_content(turns={\n",
" 'role': 'user',\n",
" 'parts': [{'text': message}]\n",
" })\n",
"\n",
"\n",
"async def async_main(last_handle=None):\n",
" config = types.LiveConnectConfig.model_validate({\n",
" \"response_modalities\": [\"TEXT\"],\n",
" \"session_resumption\": {\n",
" 'handle': last_handle,\n",
" }\n",
" })\n",
" try:\n",
" async with (\n",
" client.aio.live.connect(model=MODEL, config=config) as session,\n",
" asyncio.TaskGroup() as tg\n",
" ):\n",
" clock_task = tg.create_task(clock())\n",
" recv_task = tg.create_task(recv(session))\n",
" send_task = tg.create_task(send(session))\n",
" await send_task\n",
" raise asyncio.CancelledError()\n",
" except asyncio.CancelledError:\n",
" pass\n",
" except ExceptionGroup as EG:\n",
" traceback.print_exception(EG)"
]
"source": "import asyncio\nimport traceback\nfrom asyncio.exceptions import CancelledError\n\nlast_handle = None\n\nasync def async_enumerate(aiterable):\n n=0\n async for item in aiterable:\n yield n, item\n n+=1\n\n\ndef show_response(response):\n new_handle = None\n if text := response.text:\n print(text, end=\"\")\n else:\n print(response.model_dump_json(indent=2, exclude_none=True))\n if response.session_resumption_update:\n new_handle = response.session_resumption_update.new_handle\n return new_handle\n\n\nasync def clock():\n time = 0\n while True:\n await asyncio.sleep(60)\n time += 1\n print(f\"{time}:00\")\n\n\nasync def recv(session):\n global last_handle\n try:\n while True:\n async for response in session.receive():\n new_handle = show_response(response)\n if new_handle:\n last_handle = new_handle\n except asyncio.CancelledError:\n pass\n\n\nasync def send(session):\n while True:\n message = await asyncio.to_thread(input, \"message > \")\n if message.lower() == \"q\":\n break\n await session.send_realtime_input(text=message)\n\n\nasync def async_main(last_handle=None):\n config = types.LiveConnectConfig.model_validate({\n \"response_modalities\": [\"TEXT\"],\n \"session_resumption\": {\n 'handle': last_handle,\n }\n })\n try:\n async with (\n client.aio.live.connect(model=MODEL, config=config) as session,\n asyncio.TaskGroup() as tg\n ):\n clock_task = tg.create_task(clock())\n recv_task = tg.create_task(recv(session))\n send_task = tg.create_task(send(session))\n await send_task\n raise asyncio.CancelledError()\n except asyncio.CancelledError:\n pass\n except ExceptionGroup as EG:\n traceback.print_exception(EG)"
},
{
"cell_type": "markdown",
Expand Down