Spaces:
Running
Running
Update backend/agents/hint_agent.py
Browse files- backend/agents/hint_agent.py +32 -19
backend/agents/hint_agent.py
CHANGED
|
@@ -2,28 +2,41 @@ import google.generativeai as genai
|
|
| 2 |
import os
|
| 3 |
from typing import Dict, Any, Optional
|
| 4 |
|
| 5 |
-
def _get_best_flash_model() -> str:
|
| 6 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
try:
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
if 'flash' in m.name.lower():
|
| 12 |
-
available_models.append(m.name)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
print(f"
|
| 26 |
-
|
| 27 |
return "gemini-2.0-flash"
|
| 28 |
|
| 29 |
|
|
@@ -41,7 +54,7 @@ class HintAgent:
|
|
| 41 |
pass
|
| 42 |
else:
|
| 43 |
genai.configure(api_key=self.api_key)
|
| 44 |
-
best_model = _get_best_flash_model()
|
| 45 |
print(f"[HintAgent] Selected Model: {best_model}")
|
| 46 |
self.model = genai.GenerativeModel(best_model)
|
| 47 |
|
|
|
|
| 2 |
import os
|
| 3 |
from typing import Dict, Any, Optional
|
| 4 |
|
| 5 |
+
def _get_best_flash_model(api_key: Optional[str] = None) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Auto-detects the best available Gemini model.
|
| 8 |
+
Dynamically parses the list of supported models and selects the highest-version Flash model.
|
| 9 |
+
Falls back to a default if query fails or no flash models are found.
|
| 10 |
+
"""
|
| 11 |
+
key = api_key or os.getenv("GEMINI_API_KEY")
|
| 12 |
+
if not key:
|
| 13 |
+
return "gemini-2.0-flash"
|
| 14 |
try:
|
| 15 |
+
import google.generativeai as genai
|
| 16 |
+
import re
|
| 17 |
+
genai.configure(api_key=key)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Get all available models for this key
|
| 20 |
+
models = [m.name for m in genai.list_models()]
|
| 21 |
+
|
| 22 |
+
# Filter for flash models
|
| 23 |
+
flash_candidates = [m for m in models if "flash" in m.lower()]
|
| 24 |
+
|
| 25 |
+
if flash_candidates:
|
| 26 |
+
# Sort by version number extracted from the name
|
| 27 |
+
def _extract_version(name: str) -> float:
|
| 28 |
+
match = re.search(r'(\d+\.\d+)', name)
|
| 29 |
+
return float(match.group(1)) if match else 0.0
|
| 30 |
|
| 31 |
+
flash_candidates.sort(key=_extract_version, reverse=True)
|
| 32 |
+
return flash_candidates[0].replace("models/", "")
|
| 33 |
+
|
| 34 |
+
# If no flash models found, fall back to standard candidates list
|
| 35 |
+
for candidate in ["models/gemini-2.0-flash", "models/gemini-1.5-flash", "models/gemini-1.0-pro"]:
|
| 36 |
+
if candidate in models:
|
| 37 |
+
return candidate.replace("models/", "")
|
| 38 |
except Exception as e:
|
| 39 |
+
print(f"[HintAgent] Error listing models: {e}. Defaulting to gemini-2.0-flash.")
|
|
|
|
| 40 |
return "gemini-2.0-flash"
|
| 41 |
|
| 42 |
|
|
|
|
| 54 |
pass
|
| 55 |
else:
|
| 56 |
genai.configure(api_key=self.api_key)
|
| 57 |
+
best_model = _get_best_flash_model(self.api_key)
|
| 58 |
print(f"[HintAgent] Selected Model: {best_model}")
|
| 59 |
self.model = genai.GenerativeModel(best_model)
|
| 60 |
|