Gemini Live fixes, plus additional docstrings

This commit is contained in:
Mark Backman
2025-07-02 07:41:15 -07:00
parent e71cb3ba68
commit f590a476e7
2 changed files with 55 additions and 20 deletions

View File

@@ -4,6 +4,13 @@
# SPDX-License-Identifier: BSD 2-Clause License # SPDX-License-Identifier: BSD 2-Clause License
# #
"""Gemini File API client for uploading and managing files.
This module provides a client for Google's Gemini File API, enabling file
uploads, metadata retrieval, listing, and deletion. Files uploaded through
this API can be referenced in Gemini generative model calls.
"""
import mimetypes import mimetypes
from typing import Any, Dict, Optional from typing import Any, Dict, Optional

View File

@@ -72,7 +72,6 @@ from pipecat.utils.time import time_now_iso8601
from pipecat.utils.tracing.service_decorators import traced_gemini_live, traced_stt from pipecat.utils.tracing.service_decorators import traced_gemini_live, traced_stt
from . import events from . import events
from .file_api import GeminiFileAPI from .file_api import GeminiFileAPI
try: try:
@@ -237,7 +236,9 @@ class GeminiMultimodalLiveContext(OpenAILLMContext):
parts.append({"type": "text", "text": text}) parts.append({"type": "text", "text": text})
# Add file reference part # Add file reference part
parts.append({"type": "file_data", "file_data": {"mime_type": mime_type, "file_uri": file_uri}}) parts.append(
{"type": "file_data", "file_data": {"mime_type": mime_type, "file_uri": file_uri}}
)
# Add to messages # Add to messages
message = {"role": "user", "content": parts} message = {"role": "user", "content": parts}
@@ -270,12 +271,14 @@ class GeminiMultimodalLiveContext(OpenAILLMContext):
parts.append({"text": part.get("text")}) parts.append({"text": part.get("text")})
elif part.get("type") == "file_data": elif part.get("type") == "file_data":
file_data = part.get("file_data", {}) file_data = part.get("file_data", {})
parts.append({ parts.append(
"fileData": { {
"mimeType": file_data.get("mime_type"), "fileData": {
"fileUri": file_data.get("file_uri") "mimeType": file_data.get("mime_type"),
"fileUri": file_data.get("file_uri"),
}
} }
}) )
else: else:
logger.warning(f"Unsupported content type: {str(part)[:80]}") logger.warning(f"Unsupported content type: {str(part)[:80]}")
else: else:
@@ -379,7 +382,14 @@ class GeminiMultimodalModalities(Enum):
class GeminiMediaResolution(str, Enum): class GeminiMediaResolution(str, Enum):
"""Media resolution options for Gemini Multimodal Live.""" """Media resolution options for Gemini Multimodal Live.
Parameters:
UNSPECIFIED: Use default resolution setting.
LOW: Low resolution with 64 tokens.
MEDIUM: Medium resolution with 256 tokens.
HIGH: High resolution with zoomed reframing and 256 tokens.
"""
UNSPECIFIED = "MEDIA_RESOLUTION_UNSPECIFIED" # Use default UNSPECIFIED = "MEDIA_RESOLUTION_UNSPECIFIED" # Use default
LOW = "MEDIA_RESOLUTION_LOW" # 64 tokens LOW = "MEDIA_RESOLUTION_LOW" # 64 tokens
@@ -496,6 +506,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
params: Configuration parameters for the model. Defaults to InputParams(). params: Configuration parameters for the model. Defaults to InputParams().
inference_on_context_initialization: Whether to generate a response when context inference_on_context_initialization: Whether to generate a response when context
is first set. Defaults to True. is first set. Defaults to True.
file_api_base_url: Base URL for the Gemini File API. Defaults to the official endpoint.
**kwargs: Additional arguments passed to parent LLMService. **kwargs: Additional arguments passed to parent LLMService.
""" """
super().__init__(base_url=base_url, **kwargs) super().__init__(base_url=base_url, **kwargs)
@@ -757,6 +768,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
await self._ws_send(event.model_dump(exclude_none=True)) await self._ws_send(event.model_dump(exclude_none=True))
async def _connect(self): async def _connect(self):
"""Establish WebSocket connection to Gemini Live API."""
if self._websocket: if self._websocket:
# Here we assume that if we have a websocket, we are connected. We # Here we assume that if we have a websocket, we are connected. We
# handle disconnections in the send/recv code paths. # handle disconnections in the send/recv code paths.
@@ -861,6 +873,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
self._websocket = None self._websocket = None
async def _disconnect(self): async def _disconnect(self):
"""Disconnect from Gemini Live API and clean up resources."""
logger.info("Disconnecting from Gemini service") logger.info("Disconnecting from Gemini service")
try: try:
self._disconnecting = True self._disconnecting = True
@@ -877,6 +890,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
logger.error(f"{self} error disconnecting: {e}") logger.error(f"{self} error disconnecting: {e}")
async def _ws_send(self, message): async def _ws_send(self, message):
"""Send a message to the WebSocket connection."""
# logger.debug(f"Sending message to websocket: {message}") # logger.debug(f"Sending message to websocket: {message}")
try: try:
if self._websocket: if self._websocket:
@@ -897,6 +911,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
# #
async def _receive_task_handler(self): async def _receive_task_handler(self):
"""Handle incoming messages from the WebSocket connection."""
async for message in WatchdogAsyncIterator(self._websocket, manager=self.task_manager): async for message in WatchdogAsyncIterator(self._websocket, manager=self.task_manager):
evt = events.parse_server_event(message) evt = events.parse_server_event(message)
# logger.debug(f"Received event: {message[:500]}") # logger.debug(f"Received event: {message[:500]}")
@@ -925,6 +940,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
# #
async def _send_user_audio(self, frame): async def _send_user_audio(self, frame):
"""Send user audio frame to Gemini Live API."""
if self._audio_input_paused: if self._audio_input_paused:
return return
# Send all audio to Gemini # Send all audio to Gemini
@@ -941,6 +957,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
self._user_audio_buffer = self._user_audio_buffer[-length:] self._user_audio_buffer = self._user_audio_buffer[-length:]
async def _send_user_video(self, frame): async def _send_user_video(self, frame):
"""Send user video frame to Gemini Live API."""
if self._video_input_paused: if self._video_input_paused:
return return
@@ -954,6 +971,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
await self.send_client_event(evt) await self.send_client_event(evt)
async def _create_initial_response(self): async def _create_initial_response(self):
"""Create initial response based on context history."""
if not self._api_session_ready: if not self._api_session_ready:
self._run_llm_when_api_session_ready = True self._run_llm_when_api_session_ready = True
return return
@@ -979,6 +997,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
self._needs_turn_complete_message = True self._needs_turn_complete_message = True
async def _create_single_response(self, messages_list): async def _create_single_response(self, messages_list):
"""Create a single response from a list of messages."""
# Refactor to combine this logic with same logic in GeminiMultimodalLiveContext # Refactor to combine this logic with same logic in GeminiMultimodalLiveContext
messages = [] messages = []
for item in messages_list: for item in messages_list:
@@ -1000,12 +1019,14 @@ class GeminiMultimodalLiveLLMService(LLMService):
parts.append({"text": part.get("text")}) parts.append({"text": part.get("text")})
elif part.get("type") == "file_data": elif part.get("type") == "file_data":
file_data = part.get("file_data", {}) file_data = part.get("file_data", {})
parts.append({ parts.append(
"fileData": { {
"mimeType": file_data.get("mime_type"), "fileData": {
"fileUri": file_data.get("file_uri") "mimeType": file_data.get("mime_type"),
"fileUri": file_data.get("file_uri"),
}
} }
}) )
else: else:
logger.warning(f"Unsupported content type: {str(part)[:80]}") logger.warning(f"Unsupported content type: {str(part)[:80]}")
else: else:
@@ -1029,6 +1050,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
@traced_gemini_live(operation="llm_tool_result") @traced_gemini_live(operation="llm_tool_result")
async def _tool_result(self, tool_result_message): async def _tool_result(self, tool_result_message):
"""Send tool result back to the API."""
# For now we're shoving the name into the tool_call_id field, so this # For now we're shoving the name into the tool_call_id field, so this
# will work until we revisit that. # will work until we revisit that.
id = tool_result_message.get("tool_call_id") id = tool_result_message.get("tool_call_id")
@@ -1054,6 +1076,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
@traced_gemini_live(operation="llm_setup") @traced_gemini_live(operation="llm_setup")
async def _handle_evt_setup_complete(self, evt): async def _handle_evt_setup_complete(self, evt):
"""Handle the setup complete event."""
# If this is our first context frame, run the LLM # If this is our first context frame, run the LLM
self._api_session_ready = True self._api_session_ready = True
# Now that we've configured the session, we can run the LLM if we need to. # Now that we've configured the session, we can run the LLM if we need to.
@@ -1062,6 +1085,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
await self._create_initial_response() await self._create_initial_response()
async def _handle_evt_model_turn(self, evt): async def _handle_evt_model_turn(self, evt):
"""Handle the model turn event."""
part = evt.serverContent.modelTurn.parts[0] part = evt.serverContent.modelTurn.parts[0]
if not part: if not part:
return return
@@ -1103,6 +1127,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
@traced_gemini_live(operation="llm_tool_call") @traced_gemini_live(operation="llm_tool_call")
async def _handle_evt_tool_call(self, evt): async def _handle_evt_tool_call(self, evt):
"""Handle tool call events."""
function_calls = evt.toolCall.functionCalls function_calls = evt.toolCall.functionCalls
if not function_calls: if not function_calls:
return return
@@ -1123,6 +1148,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
@traced_gemini_live(operation="llm_response") @traced_gemini_live(operation="llm_response")
async def _handle_evt_turn_complete(self, evt): async def _handle_evt_turn_complete(self, evt):
"""Handle the turn complete event."""
self._bot_is_speaking = False self._bot_is_speaking = False
text = self._bot_text_buffer text = self._bot_text_buffer
@@ -1206,6 +1232,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
) )
async def _handle_evt_output_transcription(self, evt): async def _handle_evt_output_transcription(self, evt):
"""Handle the output transcription event."""
if not evt.serverContent.outputTranscription: if not evt.serverContent.outputTranscription:
return return
@@ -1224,6 +1251,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
await self.push_frame(TTSTextFrame(text=text)) await self.push_frame(TTSTextFrame(text=text))
async def _handle_evt_usage_metadata(self, evt): async def _handle_evt_usage_metadata(self, evt):
"""Handle the usage metadata event."""
if not evt.usageMetadata: if not evt.usageMetadata:
return return