-
Notifications
You must be signed in to change notification settings - Fork 4
VER-290: Save Gemini thought summaries during stage 3 analysis #45
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 all commits
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 | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -80,6 +80,7 @@ def update_snippet_in_supabase( | |||||||||||||||||||||||||||||||||||
| snippet_id, | ||||||||||||||||||||||||||||||||||||
| gemini_response, | ||||||||||||||||||||||||||||||||||||
| grounding_metadata, | ||||||||||||||||||||||||||||||||||||
| thought_summaries, | ||||||||||||||||||||||||||||||||||||
| analyzed_by, | ||||||||||||||||||||||||||||||||||||
| status, | ||||||||||||||||||||||||||||||||||||
| error_message, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -99,6 +100,7 @@ def update_snippet_in_supabase( | |||||||||||||||||||||||||||||||||||
| context=gemini_response["context"], | ||||||||||||||||||||||||||||||||||||
| political_leaning=gemini_response["political_leaning"], | ||||||||||||||||||||||||||||||||||||
| grounding_metadata=grounding_metadata, | ||||||||||||||||||||||||||||||||||||
| thought_summaries=thought_summaries, | ||||||||||||||||||||||||||||||||||||
| analyzed_by=analyzed_by, | ||||||||||||||||||||||||||||||||||||
| status=status, | ||||||||||||||||||||||||||||||||||||
| error_message=error_message, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -215,6 +217,7 @@ def process_snippet(supabase_client, snippet, local_file, gemini_key, skip_revie | |||||||||||||||||||||||||||||||||||
| snippet_id=snippet["id"], | ||||||||||||||||||||||||||||||||||||
| gemini_response=analyzing_response["response"], | ||||||||||||||||||||||||||||||||||||
| grounding_metadata=analyzing_response["grounding_metadata"], | ||||||||||||||||||||||||||||||||||||
| thought_summaries=analyzing_response["thought_summaries"], | ||||||||||||||||||||||||||||||||||||
| analyzed_by=analyzing_response["analyzed_by"], | ||||||||||||||||||||||||||||||||||||
| status=status, | ||||||||||||||||||||||||||||||||||||
| error_message=None, | ||||||||||||||||||||||||||||||||||||
|
|
@@ -359,12 +362,15 @@ def run( | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||
| # Step 1: Analyze with Google Search | ||||||||||||||||||||||||||||||||||||
| analysis_text, grounding_metadata = cls.__analyze_with_search( | ||||||||||||||||||||||||||||||||||||
| analysis_result = cls.__analyze_with_search( | ||||||||||||||||||||||||||||||||||||
| client, | ||||||||||||||||||||||||||||||||||||
| model_name, | ||||||||||||||||||||||||||||||||||||
| user_prompt, | ||||||||||||||||||||||||||||||||||||
| uploaded_audio_file, | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
| analysis_text = analysis_result["text"] | ||||||||||||||||||||||||||||||||||||
| grounding_metadata = analysis_result["grounding_metadata"] | ||||||||||||||||||||||||||||||||||||
| thought_summaries = analysis_result["thought_summaries"] | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Try to validate with Pydantic model first | ||||||||||||||||||||||||||||||||||||
| validated_output = cls.__validate_with_pydantic(analysis_text) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -373,12 +379,14 @@ def run( | |||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||
| "response": validated_output, | ||||||||||||||||||||||||||||||||||||
| "grounding_metadata": grounding_metadata, | ||||||||||||||||||||||||||||||||||||
| "thought_summaries": thought_summaries, | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # Step 2: Structure with response_schema (if validation failed) | ||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||
| "response": cls.__structure_with_schema(client, analysis_text), | ||||||||||||||||||||||||||||||||||||
| "grounding_metadata": grounding_metadata, | ||||||||||||||||||||||||||||||||||||
| "thought_summaries": thought_summaries, | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| finally: | ||||||||||||||||||||||||||||||||||||
| client.files.delete(name=uploaded_audio_file.name) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -407,11 +415,16 @@ def __analyze_with_search( | |||||||||||||||||||||||||||||||||||
| system_instruction=cls.SYSTEM_INSTRUCTION, | ||||||||||||||||||||||||||||||||||||
| max_output_tokens=16384, | ||||||||||||||||||||||||||||||||||||
| tools=[Tool(google_search=GoogleSearch())], | ||||||||||||||||||||||||||||||||||||
| thinking_config=ThinkingConfig(thinking_budget=4096), | ||||||||||||||||||||||||||||||||||||
| thinking_config=ThinkingConfig(thinking_budget=4096, include_thoughts=True), | ||||||||||||||||||||||||||||||||||||
| safety_settings=get_safety_settings(), | ||||||||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| thoughts = "" | ||||||||||||||||||||||||||||||||||||
| for part in response.candidates[0].content.parts: | ||||||||||||||||||||||||||||||||||||
| if part.thought and part.text: | ||||||||||||||||||||||||||||||||||||
| thoughts += part.text | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+423
to
+426
Contributor
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 block has a critical bug. The code accesses Additionally, for consistency, I'm renaming the
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| grounding_metadata = ( | ||||||||||||||||||||||||||||||||||||
| response.candidates[0].grounding_metadata.model_dump_json(indent=2) if response.candidates else None | ||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+423
to
430
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. Add defensive check before accessing The thought extraction accesses thoughts = ""
- for part in response.candidates[0].content.parts:
- if part.thought and part.text:
- thoughts += part.text
+ if response.candidates and response.candidates[0].content:
+ for part in response.candidates[0].content.parts:
+ if part.thought and part.text:
+ thoughts += part.text
grounding_metadata = (📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
|
|
@@ -425,7 +438,11 @@ def __analyze_with_search( | |||||||||||||||||||||||||||||||||||
| print(f"Response finish reason: {finish_reason}") | ||||||||||||||||||||||||||||||||||||
| raise ValueError("No response from Gemini in step 1.") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return response.text, grounding_metadata | ||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||
| "text": response.text, | ||||||||||||||||||||||||||||||||||||
| "grounding_metadata": grounding_metadata, | ||||||||||||||||||||||||||||||||||||
| "thought_summaries": thoughts, | ||||||||||||||||||||||||||||||||||||
|
Contributor
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. |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||
| def __validate_with_pydantic(cls, response_text: str): | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
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.
While the current implementation of
__analyze_with_searchensures these keys exist, it's safer to use the.get()method for dictionary access. This makes the code more robust against future changes where a key might be missing, preventing potentialKeyErrorexceptions.