Imnotchis commited on
Commit
2d6d256
·
verified ·
1 Parent(s): 924e943

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -1
app.py CHANGED
@@ -25,12 +25,50 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
25
  # BASIC AGENT
26
  # =========================================================
27
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT -----
 
 
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  class BasicAgent:
30
  def __init__(self):
31
 
32
  # Groq model
33
- self.model = OpenAIModel(
34
  model_id="openai/gpt-oss-120b",
35
  api_base="https://api.groq.com/openai/v1",
36
  api_key=os.environ["GROQ_API_KEY"],
 
25
  # BASIC AGENT
26
  # =========================================================
27
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT -----
28
+ class RetryOpenAIModel(OpenAIModel):
29
+ def generate(self, *args, **kwargs):
30
+ max_attempts = 4
31
 
32
+ for attempt in range(max_attempts):
33
+ try:
34
+ return super().generate(*args, **kwargs)
35
+
36
+ except RateLimitError as e:
37
+ if attempt == max_attempts - 1:
38
+ print("Groq rate limit: Maximum retries reached.")
39
+ raise
40
+
41
+ retry_after = None
42
+
43
+ if e.response is not None:
44
+ retry_after = e.response.headers.get("retry-after")
45
+
46
+ try:
47
+ wait_time = float(retry_after)
48
+ except (TypeError, ValueError):
49
+ wait_time = 10 * (2 ** attempt)
50
+
51
+ # Avoid waiting indefinitely for daily quota resets
52
+ if wait_time > 120:
53
+ print(
54
+ f"Rate limit requires waiting {wait_time} seconds. "
55
+ "Stopping retries."
56
+ )
57
+ raise
58
+
59
+ wait_time = max(wait_time, 1)
60
+
61
+ print(
62
+ f"[GROQ 429] Attempt {attempt + 1}/{max_attempts}. "
63
+ f"Waiting {wait_time} seconds before retry..."
64
+ )
65
+
66
+ time.sleep(wait_time)
67
  class BasicAgent:
68
  def __init__(self):
69
 
70
  # Groq model
71
+ self.model = RetryOpenAIModel(
72
  model_id="openai/gpt-oss-120b",
73
  api_base="https://api.groq.com/openai/v1",
74
  api_key=os.environ["GROQ_API_KEY"],