-
Notifications
You must be signed in to change notification settings - Fork 85
Fix Gemini finder coordinate detection bug caused by token limit #15
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| gemini: | ||
| api_key: !ENV GEMINI_API_KEY | ||
| model_name: gemini-1.5-flash | ||
| model_name: gemini-3-flash-preview | ||
| image_width: 768 | ||
| image_height: 768 | ||
| output_width: 1000 # max range of outputted values | ||
|
|
@@ -12,24 +12,29 @@ gemini: | |
| osx: | ||
| image_width: 768 | ||
| generation_config: | ||
| temperature: 0.7 | ||
| temperature: 1.0 | ||
| top_p: 0.95 | ||
| top_k: 40 | ||
| max_output_tokens: 200 | ||
| max_output_tokens: 65536 | ||
|
Comment on lines
+15
to
+17
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. The configuration for the planner has been updated to set |
||
| response_mime_type: text/plain | ||
| finder: | ||
| image_width: 768 | ||
| image_height: 768 | ||
| output_width: 1000 # max range of outputted values | ||
| output_height: 1000 | ||
| android: | ||
| image_width: 768 | ||
| image_height: 768 | ||
| output_width: 1000 # max range of outputted values | ||
| output_height: 1000 | ||
| osx: | ||
| image_width: 768 | ||
| output_width: 1000 | ||
| output_height: 1000 | ||
| generation_config: | ||
| temperature: 0.95 | ||
| top_p: 0.99 | ||
| top_k: 20 | ||
| max_output_tokens: 80 | ||
| max_output_tokens: 4096 | ||
| response_mime_type: application/json | ||
|
|
||
| openai: | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,11 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import google.generativeai as genai | ||||||||||||||||||||||||||||||||||||||||||
| from . import BaseFinder | ||||||||||||||||||||||||||||||||||||||||||
| from . import BaseFinder, FinderResponseLLM | ||||||||||||||||||||||||||||||||||||||||||
| from clickclickclick.config import BaseConfig | ||||||||||||||||||||||||||||||||||||||||||
| from clickclickclick.executor import Executor | ||||||||||||||||||||||||||||||||||||||||||
| from tempfile import NamedTemporaryFile | ||||||||||||||||||||||||||||||||||||||||||
| from PIL import Image | ||||||||||||||||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||||||||||||||||
| import re | ||||||||||||||||||||||||||||||||||||||||||
|
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. |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| class GeminiFinder(BaseFinder): | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -19,9 +21,12 @@ def __init__(self, c: BaseConfig, executor: Executor): | |||||||||||||||||||||||||||||||||||||||||
| self.OUTPUT_HEIGHT = finder_config.get("output_height") | ||||||||||||||||||||||||||||||||||||||||||
| api_key = finder_config.get("api_key") | ||||||||||||||||||||||||||||||||||||||||||
| model_name = finder_config.get("model_name") | ||||||||||||||||||||||||||||||||||||||||||
| generation_config = finder_config.get("generation_config") | ||||||||||||||||||||||||||||||||||||||||||
| generation_config = finder_config.get("generation_config", {}) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| super().__init__(api_key, model_name, generation_config, system_prompt, executor) | ||||||||||||||||||||||||||||||||||||||||||
| genai.configure(api_key=api_key) | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Generation config: {generation_config}") | ||||||||||||||||||||||||||||||||||||||||||
|
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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| self.model = genai.GenerativeModel( | ||||||||||||||||||||||||||||||||||||||||||
| model_name=model_name, | ||||||||||||||||||||||||||||||||||||||||||
| generation_config=generation_config, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -41,12 +46,37 @@ def process_segment(self, segment, model, prompt, retries=3): | |||||||||||||||||||||||||||||||||||||||||
| response = self.model.generate_content( | ||||||||||||||||||||||||||||||||||||||||||
| [segment_image, self.element_finder_prompt(prompt)] | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| response_text = response.text | ||||||||||||||||||||||||||||||||||||||||||
| print(response_text, " resp text") | ||||||||||||||||||||||||||||||||||||||||||
| response_text = response.text.strip() | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Gemini raw response: '{response_text}'") | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Response type: {type(response_text)}") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Try to extract JSON from response | ||||||||||||||||||||||||||||||||||||||||||
| # Sometimes Gemini wraps JSON in markdown code blocks | ||||||||||||||||||||||||||||||||||||||||||
| json_match = re.search(r'```json\s*(\{.*?\})\s*```', response_text, re.DOTALL) | ||||||||||||||||||||||||||||||||||||||||||
| if json_match: | ||||||||||||||||||||||||||||||||||||||||||
| response_text = json_match.group(1) | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Extracted JSON from markdown: '{response_text}'") | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Or it might just have extra text before/after | ||||||||||||||||||||||||||||||||||||||||||
| json_match = re.search(r'\{[^{}]*"ymin"[^{}]*\}', response_text) | ||||||||||||||||||||||||||||||||||||||||||
| if json_match: | ||||||||||||||||||||||||||||||||||||||||||
| response_text = json_match.group(0) | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Extracted JSON object: '{response_text}'") | ||||||||||||||||||||||||||||||||||||||||||
|
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. The current logic for extracting JSON is slightly inefficient as it attempts the second regex search even if the first one (for markdown blocks) was successful. You can make this more efficient and readable by using an
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Validate it's valid JSON | ||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||
| parsed = json.loads(response_text) | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Parsed JSON successfully: {parsed}") | ||||||||||||||||||||||||||||||||||||||||||
| except json.JSONDecodeError as je: | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - JSON decode error: {je}") | ||||||||||||||||||||||||||||||||||||||||||
| print(f"DEBUG - Failed to parse: '{response_text}'") | ||||||||||||||||||||||||||||||||||||||||||
|
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. |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return (response_text, coordinates) | ||||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||||
| # Log the exception or handle it as necessary | ||||||||||||||||||||||||||||||||||||||||||
| print(f"Attempt {attempt + 1} failed with exception: {e}") | ||||||||||||||||||||||||||||||||||||||||||
| import traceback | ||||||||||||||||||||||||||||||||||||||||||
|
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. |
||||||||||||||||||||||||||||||||||||||||||
| traceback.print_exc() | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| # Increment the attempt counter | ||||||||||||||||||||||||||||||||||||||||||
| attempt += 1 | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
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.
The model name
gemini-3-flash-previewdoes not appear to be a standard, publicly available Google Gemini model name. This might be a typo and could cause API requests to fail. Please verify this is the correct model identifier. For reference, a recent flash model is namedgemini-1.5-flash-latest.