Spaces:
Sleeping
Sleeping
adityaverma977 commited on
Commit ·
97270d8
1
Parent(s): 5c81985
Add HF fallback for Groq rate-limit errors; remove rate-limited Groq models
Browse files- app/groq_client.py +45 -0
app/groq_client.py
CHANGED
|
@@ -2,13 +2,16 @@ import json
|
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
import math
|
|
|
|
| 5 |
from groq import AsyncGroq
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
_GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
|
|
|
| 11 |
_client = AsyncGroq(api_key=_GROQ_API_KEY) if _GROQ_API_KEY else None
|
|
|
|
| 12 |
|
| 13 |
DEFAULT_DECISION_MODEL = "mixtral-8x7b-32768"
|
| 14 |
MAX_AGENT_SPEED = 80
|
|
@@ -144,7 +147,49 @@ What do you do?"""
|
|
| 144 |
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 145 |
}
|
| 146 |
except Exception as e:
|
|
|
|
| 147 |
print(f"Error calling groq for {agent.model_name}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
return _fallback_escape(agent, fire)
|
| 149 |
|
| 150 |
|
|
|
|
| 2 |
import os
|
| 3 |
import random
|
| 4 |
import math
|
| 5 |
+
import httpx
|
| 6 |
from groq import AsyncGroq
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
_GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 12 |
+
_HF_API_TOKEN = os.environ.get("HF_API_TOKEN") or os.environ.get("HUGGINGFACE_API_TOKEN")
|
| 13 |
_client = AsyncGroq(api_key=_GROQ_API_KEY) if _GROQ_API_KEY else None
|
| 14 |
+
_HF_API_BASE = "https://api-inference.huggingface.co/models"
|
| 15 |
|
| 16 |
DEFAULT_DECISION_MODEL = "mixtral-8x7b-32768"
|
| 17 |
MAX_AGENT_SPEED = 80
|
|
|
|
| 147 |
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 148 |
}
|
| 149 |
except Exception as e:
|
| 150 |
+
# If Groq fails (rate limits, network), try a HF fallback when possible
|
| 151 |
print(f"Error calling groq for {agent.model_name}: {e}")
|
| 152 |
+
err = str(e).lower()
|
| 153 |
+
if _HF_API_TOKEN and ("rate limit" in err or "rate_limit" in err or "429" in err):
|
| 154 |
+
fallback_hf = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 155 |
+
try:
|
| 156 |
+
async with httpx.AsyncClient(timeout=8.0) as client:
|
| 157 |
+
resp = await client.post(
|
| 158 |
+
f"{_HF_API_BASE}/{fallback_hf}",
|
| 159 |
+
headers={"Authorization": f"Bearer {_HF_API_TOKEN}"},
|
| 160 |
+
json={"inputs": system_prompt, "parameters": {"max_new_tokens": 150, "temperature": 0.7}},
|
| 161 |
+
)
|
| 162 |
+
resp.raise_for_status()
|
| 163 |
+
data = resp.json()
|
| 164 |
+
if isinstance(data, list) and len(data) > 0:
|
| 165 |
+
text = data[0].get("generated_text", "")
|
| 166 |
+
else:
|
| 167 |
+
text = data.get("generated_text", "")
|
| 168 |
+
text = text[len(system_prompt):].strip() if text.startswith(system_prompt) else text
|
| 169 |
+
try:
|
| 170 |
+
js = text[text.find('{'):text.rfind('}')+1]
|
| 171 |
+
decision = json.loads(js)
|
| 172 |
+
except Exception:
|
| 173 |
+
decision = {}
|
| 174 |
+
|
| 175 |
+
action = decision.get("action", "escape")
|
| 176 |
+
if action not in ["search_water", "collect_water", "extinguish_fire", "escape", "vote_for_leader"]:
|
| 177 |
+
action = "escape"
|
| 178 |
+
|
| 179 |
+
if dist_to_water is not None and dist_to_water <= 60 and not agent.water_collected:
|
| 180 |
+
action = "collect_water"
|
| 181 |
+
elif agent.water_collected and dist_to_fire <= 350:
|
| 182 |
+
action = "extinguish_fire"
|
| 183 |
+
|
| 184 |
+
return {
|
| 185 |
+
"action": action,
|
| 186 |
+
"vote_for": decision.get("vote_for"),
|
| 187 |
+
"message": decision.get("message", "Moving strategically."),
|
| 188 |
+
"reasoning": decision.get("reasoning", "Survival and teamwork.")
|
| 189 |
+
}
|
| 190 |
+
except Exception as e2:
|
| 191 |
+
print(f"HF fallback failed: {e2}")
|
| 192 |
+
return _fallback_escape(agent, fire)
|
| 193 |
return _fallback_escape(agent, fire)
|
| 194 |
|
| 195 |
|