Miguel Figueiredo Marques commited on
Commit
92272e4
·
1 Parent(s): 87033aa
Files changed (1) hide show
  1. app.py +20 -3
app.py CHANGED
@@ -22,7 +22,8 @@ load_dotenv()
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
  # Main reasoning + tool-calling model (Groq free tier). Swap if a model is retired.
25
- MODEL_NAME = os.getenv("GROQ_MODEL", "llama-3.3-70b-versatile")
 
26
  # Vision model for image questions (best-effort; update id if Groq retires it).
27
  VISION_MODEL = os.getenv("GROQ_VISION_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct")
28
  # Seconds to pause between questions to stay under Groq free-tier rate limits.
@@ -253,12 +254,28 @@ class BasicAgent:
253
  print(f"Agent returning: {answer[:120]}")
254
  return answer
255
  except Exception as e:
256
- msg = str(e).lower()
257
- if ("rate" in msg or "429" in msg) and attempt < 2:
258
  wait = 25 * (attempt + 1)
259
  print(f"Rate limited; waiting {wait}s and retrying...")
260
  time.sleep(wait)
261
  continue
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  raise
263
 
264
 
 
22
  # --- Constants ---
23
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
24
  # Main reasoning + tool-calling model (Groq free tier). Swap if a model is retired.
25
+ # llama-3.1-70b-versatile has more reliable JSON tool-call formatting than 3.3 on Groq.
26
+ MODEL_NAME = os.getenv("GROQ_MODEL", "llama-3.1-70b-versatile")
27
  # Vision model for image questions (best-effort; update id if Groq retires it).
28
  VISION_MODEL = os.getenv("GROQ_VISION_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct")
29
  # Seconds to pause between questions to stay under Groq free-tier rate limits.
 
254
  print(f"Agent returning: {answer[:120]}")
255
  return answer
256
  except Exception as e:
257
+ msg = str(e)
258
+ if ("rate" in msg.lower() or "429" in msg) and attempt < 2:
259
  wait = 25 * (attempt + 1)
260
  print(f"Rate limited; waiting {wait}s and retrying...")
261
  time.sleep(wait)
262
  continue
263
+ if ("tool_use_failed" in msg or "tool call validation" in msg) and attempt < 1:
264
+ # Model emitted malformed tool call syntax; retry once
265
+ print(f"Malformed tool call on attempt {attempt + 1}; retrying...")
266
+ time.sleep(5)
267
+ continue
268
+ if "tool_use_failed" in msg or "tool call validation" in msg:
269
+ # Retries exhausted — fall back to a direct no-tools LLM call
270
+ print("Tool calls keep failing; falling back to direct LLM (no tools).")
271
+ try:
272
+ llm = ChatGroq(model=MODEL_NAME, temperature=0)
273
+ direct = llm.invoke(messages)
274
+ answer = extract_final_answer(direct.content)
275
+ print(f"Fallback returning: {answer[:120]}")
276
+ return answer
277
+ except Exception as fe:
278
+ raise fe
279
  raise
280
 
281