Skip to content
Open
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
99 changes: 6 additions & 93 deletions quickstarts/Get_started_LiveAPI.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,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=os.environ.get('GOOGLE_API_KEY'))"
},
{
"cell_type": "markdown",
Expand All @@ -184,14 +180,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-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}\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 @@ -680,93 +674,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_client_content(turns={\n 'role': 'user',\n 'parts': [{'text': message}]\n })\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
Loading