Spaces:
Running
Running
Commit Β·
032f107
1
Parent(s): 0d90d84
fix: add Gemini provider block in reflection_agent.py (httpx REST, no SDK)
Browse files- agent/reflection_agent.py +35 -0
agent/reflection_agent.py
CHANGED
|
@@ -507,6 +507,41 @@ def _call_llm(
|
|
| 507 |
except httpx.ConnectError as e:
|
| 508 |
raise RuntimeError(f"Cannot reach Groq API β check network / GROQ_API_KEY: {e}") from e
|
| 509 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 510 |
# ββ OpenAI SDK fallback ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 511 |
if client is None:
|
| 512 |
try:
|
|
|
|
| 507 |
except httpx.ConnectError as e:
|
| 508 |
raise RuntimeError(f"Cannot reach Groq API β check network / GROQ_API_KEY: {e}") from e
|
| 509 |
|
| 510 |
+
# ββ Google Gemini via httpx REST (no SDK needed) βββββββββββββββββββββββ
|
| 511 |
+
if client is None and provider == "gemini":
|
| 512 |
+
import httpx as _httpx
|
| 513 |
+
|
| 514 |
+
api_key = (os.environ.get("GEMINI_API_KEY") or "").strip()
|
| 515 |
+
if not api_key:
|
| 516 |
+
raise ValueError("GEMINI_API_KEY is not set. Add it as an HF Space secret.")
|
| 517 |
+
|
| 518 |
+
model_name = effective_model or "gemini-2.0-flash"
|
| 519 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/{model_name}:generateContent?key={api_key}"
|
| 520 |
+
payload = {
|
| 521 |
+
"system_instruction": {"parts": [{"text": SYSTEM_PROMPT}]},
|
| 522 |
+
"contents": [{"parts": [{"text": user_prompt}]}],
|
| 523 |
+
"generationConfig": {
|
| 524 |
+
"maxOutputTokens": settings.llm_max_tokens,
|
| 525 |
+
"temperature": settings.llm_temperature,
|
| 526 |
+
},
|
| 527 |
+
}
|
| 528 |
+
logger.info("Calling Gemini API: model=%s", model_name)
|
| 529 |
+
try:
|
| 530 |
+
with _httpx.Client(timeout=120.0) as http:
|
| 531 |
+
resp = http.post(url, json=payload)
|
| 532 |
+
resp.raise_for_status()
|
| 533 |
+
data = resp.json()
|
| 534 |
+
|
| 535 |
+
patch_text = data["candidates"][0]["content"]["parts"][0]["text"] or ""
|
| 536 |
+
meta = data.get("usageMetadata", {})
|
| 537 |
+
return patch_text, {
|
| 538 |
+
"prompt_tokens": meta.get("promptTokenCount", 0),
|
| 539 |
+
"completion_tokens": meta.get("candidatesTokenCount", 0),
|
| 540 |
+
"total_tokens": meta.get("totalTokenCount", 0),
|
| 541 |
+
}
|
| 542 |
+
except _httpx.HTTPStatusError as e:
|
| 543 |
+
raise RuntimeError(f"Gemini API error {e.response.status_code}: {e.response.text[:300]}") from e
|
| 544 |
+
|
| 545 |
# ββ OpenAI SDK fallback ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 546 |
if client is None:
|
| 547 |
try:
|