Spaces:
Running
Running
Commit Β·
25e2686
1
Parent(s): e63f982
fix: use httpx directly for Groq API (bypass SDK connection issues in HF Spaces)
Browse files- agent/reflection_agent.py +47 -14
agent/reflection_agent.py
CHANGED
|
@@ -456,32 +456,65 @@ def _call_llm(
|
|
| 456 |
model: str = "gpt-4o",
|
| 457 |
) -> tuple[str, dict]:
|
| 458 |
"""
|
| 459 |
-
Call the configured LLM provider (Groq
|
| 460 |
-
|
| 461 |
Returns (patch_text, usage_dict).
|
| 462 |
"""
|
|
|
|
| 463 |
from configs.settings import settings
|
| 464 |
|
| 465 |
-
provider = settings.llm_provider.lower()
|
| 466 |
-
effective_model =
|
| 467 |
|
| 468 |
-
# ββ Groq (
|
| 469 |
if client is None and provider == "groq":
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
except ImportError as e:
|
| 475 |
-
raise ImportError("Install groq: pip install groq") from e
|
| 476 |
|
| 477 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
if client is None:
|
| 479 |
try:
|
| 480 |
from openai import OpenAI
|
| 481 |
-
client = OpenAI(api_key=settings.openai_api_key or None)
|
| 482 |
except ImportError as e:
|
| 483 |
raise ImportError(
|
| 484 |
-
"No LLM client available. Set LLM_PROVIDER=groq
|
| 485 |
"or install openai: pip install openai"
|
| 486 |
) from e
|
| 487 |
|
|
|
|
| 456 |
model: str = "gpt-4o",
|
| 457 |
) -> tuple[str, dict]:
|
| 458 |
"""
|
| 459 |
+
Call the configured LLM provider via httpx (Groq) or OpenAI SDK.
|
| 460 |
+
Uses httpx directly for Groq to avoid SDK connection issues in HF Spaces.
|
| 461 |
Returns (patch_text, usage_dict).
|
| 462 |
"""
|
| 463 |
+
import os
|
| 464 |
from configs.settings import settings
|
| 465 |
|
| 466 |
+
provider = (os.environ.get("LLM_PROVIDER") or settings.llm_provider).lower()
|
| 467 |
+
effective_model = os.environ.get("LLM_MODEL") or settings.llm_model
|
| 468 |
|
| 469 |
+
# ββ Groq via httpx directly (most reliable in containerised envs) ββββββ
|
| 470 |
if client is None and provider == "groq":
|
| 471 |
+
import httpx
|
| 472 |
+
api_key = os.environ.get("GROQ_API_KEY") or settings.groq_api_key
|
| 473 |
+
if not api_key:
|
| 474 |
+
raise ValueError("GROQ_API_KEY is not set. Add it as an env var or HF Space secret.")
|
|
|
|
|
|
|
| 475 |
|
| 476 |
+
logger.info("Calling Groq API: model=%s", effective_model)
|
| 477 |
+
try:
|
| 478 |
+
with httpx.Client(timeout=120.0) as http:
|
| 479 |
+
resp = http.post(
|
| 480 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
| 481 |
+
headers={
|
| 482 |
+
"Authorization": f"Bearer {api_key}",
|
| 483 |
+
"Content-Type": "application/json",
|
| 484 |
+
},
|
| 485 |
+
json={
|
| 486 |
+
"model": effective_model,
|
| 487 |
+
"messages": [
|
| 488 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 489 |
+
{"role": "user", "content": user_prompt},
|
| 490 |
+
],
|
| 491 |
+
"max_tokens": settings.llm_max_tokens,
|
| 492 |
+
"temperature": settings.llm_temperature,
|
| 493 |
+
},
|
| 494 |
+
)
|
| 495 |
+
resp.raise_for_status()
|
| 496 |
+
data = resp.json()
|
| 497 |
+
|
| 498 |
+
patch_text = data["choices"][0]["message"]["content"] or ""
|
| 499 |
+
usage_raw = data.get("usage", {})
|
| 500 |
+
return patch_text, {
|
| 501 |
+
"prompt_tokens": usage_raw.get("prompt_tokens", 0),
|
| 502 |
+
"completion_tokens": usage_raw.get("completion_tokens", 0),
|
| 503 |
+
"total_tokens": usage_raw.get("total_tokens", 0),
|
| 504 |
+
}
|
| 505 |
+
except httpx.HTTPStatusError as e:
|
| 506 |
+
raise RuntimeError(f"Groq API error {e.response.status_code}: {e.response.text[:300]}") from e
|
| 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:
|
| 513 |
from openai import OpenAI
|
| 514 |
+
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY") or settings.openai_api_key or None)
|
| 515 |
except ImportError as e:
|
| 516 |
raise ImportError(
|
| 517 |
+
"No LLM client available. Set LLM_PROVIDER=groq + GROQ_API_KEY, "
|
| 518 |
"or install openai: pip install openai"
|
| 519 |
) from e
|
| 520 |
|