-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Edits for pre-compile regex(faster-compile) #5015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
b2fa155
735c0f1
ba1506c
7b0bdbf
8773019
596eff3
691a5bf
257fc95
d4daf5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,13 @@ def __init__( | |
| self._api_backend = api_backend | ||
| self._model_version = model_version | ||
|
|
||
| def _is_gemini_31_live(self) -> bool: | ||
| mv = (self._model_version or '').lower() | ||
| return 'gemini-3.1' in mv and 'live' in mv | ||
|
|
||
| def _is_gemini_31_live(self) -> bool: | ||
| return self._isgemini_31_live() | ||
|
|
||
| async def send_history(self, history: list[types.Content]): | ||
| """Sends the conversation history to the gemini model. | ||
|
|
||
|
|
@@ -111,13 +118,36 @@ async def send_content(self, content: types.Content): | |
| ), | ||
| ) | ||
| else: | ||
| logger.debug('Sending LLM new content %s', content) | ||
| await self._gemini_session.send( | ||
| input=types.LiveClientContent( | ||
| turns=[content], | ||
| turn_complete=True, | ||
| # Gemini 3.1 live models require in-session text to go through | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not part of scope of this PR. Please create a separate PR for this.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok I will create a new PR thanks for the heads-up by the way I'm sorry for any mess ups this is actually my first time committing code to google(I am doing this for experience) |
||
| # send_realtime_input(text=...) instead of LiveClientContent. | ||
| if self._is_gemini_31_live(): | ||
| text_chunks = [] | ||
| unsupported_parts = [] | ||
|
|
||
| for part in content.parts: | ||
| if part.text: | ||
| text_chunks.append(part.text) | ||
| else: | ||
| unsupported_parts.append(part) | ||
|
|
||
| if unsupported_parts: | ||
| raise ValueError( | ||
| 'Gemini 3.1 live only supports in-session text via ' | ||
| 'send_realtime_input(text=...). Non-text parts should be sent ' | ||
| 'through realtime audio/video paths.' | ||
| ) | ||
| ) | ||
|
|
||
| combined_text = ''.join(text_chunks) | ||
| logger.debug('Sending Gemini 3.1 live realtime text: %s', combined_text) | ||
| await self._gemini_session.send_realtime_input(text=combined_text) | ||
| else: | ||
| logger.debug('Sending LLM new content %s', content) | ||
| await self._gemini_session.send( | ||
| input=types.LiveClientContent( | ||
| turns=[content], | ||
| turn_complete=True, | ||
| ) | ||
| ) | ||
|
|
||
| async def send_realtime(self, input: RealtimeInput): | ||
| """Sends a chunk of audio or a frame of video to the model in realtime. | ||
|
|
@@ -128,7 +158,10 @@ async def send_realtime(self, input: RealtimeInput): | |
| if isinstance(input, types.Blob): | ||
| # The blob is binary and is very large. So let's not log it. | ||
| logger.debug('Sending LLM Blob.') | ||
| await self._gemini_session.send_realtime_input(media=input) | ||
| if self._is_gemini_31_live(): | ||
| await self._gemini_session.send_realtime_input(audio=input) | ||
| else: | ||
| await self._gemini_session.send_realtime_input(media=input) | ||
|
|
||
| elif isinstance(input, types.ActivityStart): | ||
| logger.debug('Sending LLM activity start signal.') | ||
|
|
@@ -204,13 +237,24 @@ async def receive(self) -> AsyncGenerator[LlmResponse, None]: | |
| llm_response.grounding_metadata = ( | ||
| message.server_content.grounding_metadata | ||
| ) | ||
| if content.parts[0].text: | ||
| text += content.parts[0].text | ||
|
|
||
| saw_text = False | ||
| saw_non_text_non_inline = False | ||
|
|
||
| for part in content.parts: | ||
| if part.text: | ||
| text += part.text | ||
| saw_text = True | ||
| # don't yield the merged text event when receiving audio data | ||
| elif not part.inline_data: | ||
| saw_non_text_non_inline = True | ||
|
|
||
| if saw_text: | ||
| llm_response.partial = True | ||
| # don't yield the merged text event when receiving audio data | ||
| elif text and not content.parts[0].inline_data: | ||
| elif text and saw_non_text_non_inline: | ||
| yield self.__build_full_text_response(text) | ||
| text = '' | ||
|
|
||
| yield llm_response | ||
| # Note: in some cases, tool_call may arrive before | ||
| # generation_complete, causing transcription to appear after | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is_gemini_31_liveis defined twice and return statement has a typo