Refactor whitespace and formatting in multiple files

- Clean up unnecessary whitespace in `gemini.py`, `events.py`, and `file_api.py`
- Ensure consistent formatting in `26g-gemini-multimodal-live-groundingMetadata.py`
- Improve readability by aligning code and removing trailing spaces
This commit is contained in:
Pete
2025-07-20 18:38:59 -04:00
parent e165d38277
commit b1a5cddde4
4 changed files with 30 additions and 26 deletions

View File

@@ -256,22 +256,26 @@ class Config(BaseModel):
class SearchEntryPoint(BaseModel): class SearchEntryPoint(BaseModel):
"""Represents the search entry point with rendered content for search suggestions.""" """Represents the search entry point with rendered content for search suggestions."""
renderedContent: Optional[str] = None renderedContent: Optional[str] = None
class WebSource(BaseModel): class WebSource(BaseModel):
"""Represents a web source from grounding chunks.""" """Represents a web source from grounding chunks."""
uri: Optional[str] = None uri: Optional[str] = None
title: Optional[str] = None title: Optional[str] = None
class GroundingChunk(BaseModel): class GroundingChunk(BaseModel):
"""Represents a grounding chunk containing web source information.""" """Represents a grounding chunk containing web source information."""
web: Optional[WebSource] = None web: Optional[WebSource] = None
class GroundingSegment(BaseModel): class GroundingSegment(BaseModel):
"""Represents a segment of text that is grounded.""" """Represents a segment of text that is grounded."""
startIndex: Optional[int] = None startIndex: Optional[int] = None
endIndex: Optional[int] = None endIndex: Optional[int] = None
text: Optional[str] = None text: Optional[str] = None
@@ -279,6 +283,7 @@ class GroundingSegment(BaseModel):
class GroundingSupport(BaseModel): class GroundingSupport(BaseModel):
"""Represents support information for grounded text segments.""" """Represents support information for grounded text segments."""
segment: Optional[GroundingSegment] = None segment: Optional[GroundingSegment] = None
groundingChunkIndices: Optional[List[int]] = None groundingChunkIndices: Optional[List[int]] = None
confidenceScores: Optional[List[float]] = None confidenceScores: Optional[List[float]] = None
@@ -286,6 +291,7 @@ class GroundingSupport(BaseModel):
class GroundingMetadata(BaseModel): class GroundingMetadata(BaseModel):
"""Represents grounding metadata from Google Search.""" """Represents grounding metadata from Google Search."""
searchEntryPoint: Optional[SearchEntryPoint] = None searchEntryPoint: Optional[SearchEntryPoint] = None
groundingChunks: Optional[List[GroundingChunk]] = None groundingChunks: Optional[List[GroundingChunk]] = None
groundingSupports: Optional[List[GroundingSupport]] = None groundingSupports: Optional[List[GroundingSupport]] = None
@@ -476,8 +482,6 @@ class ServerEvent(BaseModel):
usageMetadata: Optional[UsageMetadata] = None usageMetadata: Optional[UsageMetadata] = None
def parse_server_event(str): def parse_server_event(str):
"""Parse a server event from JSON string. """Parse a server event from JSON string.

View File

@@ -1024,10 +1024,9 @@ 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.""" """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:
role = item.get("role") role = item.get("role")
@@ -1206,7 +1205,9 @@ class GeminiMultimodalLiveLLMService(LLMService):
# Process grounding metadata if we have accumulated any # Process grounding metadata if we have accumulated any
if self._accumulated_grounding_metadata: if self._accumulated_grounding_metadata:
await self._process_grounding_metadata(self._accumulated_grounding_metadata, self._search_result_buffer) await self._process_grounding_metadata(
self._accumulated_grounding_metadata, self._search_result_buffer
)
# Reset grounding tracking for next response # Reset grounding tracking for next response
self._search_result_buffer = "" self._search_result_buffer = ""
@@ -1305,14 +1306,19 @@ class GeminiMultimodalLiveLLMService(LLMService):
# Process the grounding metadata immediately # Process the grounding metadata immediately
await self._process_grounding_metadata(grounding_metadata, self._search_result_buffer) await self._process_grounding_metadata(grounding_metadata, self._search_result_buffer)
async def _process_grounding_metadata(self, grounding_metadata: events.GroundingMetadata, search_result: str = ""): async def _process_grounding_metadata(
self, grounding_metadata: events.GroundingMetadata, search_result: str = ""
):
"""Process grounding metadata and emit LLMSearchResponseFrame.""" """Process grounding metadata and emit LLMSearchResponseFrame."""
if not grounding_metadata: if not grounding_metadata:
return return
# Extract rendered content for search suggestions # Extract rendered content for search suggestions
rendered_content = None rendered_content = None
if grounding_metadata.searchEntryPoint and grounding_metadata.searchEntryPoint.renderedContent: if (
grounding_metadata.searchEntryPoint
and grounding_metadata.searchEntryPoint.renderedContent
):
rendered_content = grounding_metadata.searchEntryPoint.renderedContent rendered_content = grounding_metadata.searchEntryPoint.renderedContent
# Convert grounding chunks and supports to LLMSearchOrigin format # Convert grounding chunks and supports to LLMSearchOrigin format
@@ -1325,9 +1331,7 @@ class GeminiMultimodalLiveLLMService(LLMService):
for index, chunk in enumerate(grounding_metadata.groundingChunks): for index, chunk in enumerate(grounding_metadata.groundingChunks):
if chunk.web: if chunk.web:
origin = LLMSearchOrigin( origin = LLMSearchOrigin(
site_uri=chunk.web.uri, site_uri=chunk.web.uri, site_title=chunk.web.title, results=[]
site_title=chunk.web.title,
results=[]
) )
chunk_to_origin[index] = origin chunk_to_origin[index] = origin
origins.append(origin) origins.append(origin)
@@ -1341,20 +1345,16 @@ class GeminiMultimodalLiveLLMService(LLMService):
# Add this result to all origins referenced by this support # Add this result to all origins referenced by this support
for chunk_index in support.groundingChunkIndices: for chunk_index in support.groundingChunkIndices:
if chunk_index in chunk_to_origin: if chunk_index in chunk_to_origin:
result = LLMSearchResult( result = LLMSearchResult(text=text, confidence=confidence_scores)
text=text,
confidence=confidence_scores
)
chunk_to_origin[chunk_index].results.append(result) chunk_to_origin[chunk_index].results.append(result)
# Create and push the search response frame # Create and push the search response frame
search_frame = LLMSearchResponseFrame( search_frame = LLMSearchResponseFrame(
search_result=search_result, search_result=search_result, origins=origins, rendered_content=rendered_content
origins=origins,
rendered_content=rendered_content
) )
await self.push_frame(search_frame) await self.push_frame(search_frame)
async def _handle_evt_usage_metadata(self, evt): async def _handle_evt_usage_metadata(self, evt):
"""Handle the usage metadata event.""" """Handle the usage metadata event."""
if not evt.usageMetadata: if not evt.usageMetadata: