Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -150,7 +150,9 @@ class GaiaAgent:
|
|
| 150 |
|
| 151 |
# Rate limiting
|
| 152 |
self.last_call_time = 0
|
| 153 |
-
self.min_delay =
|
|
|
|
|
|
|
| 154 |
|
| 155 |
# Initialize agent with primary model
|
| 156 |
self._reinitialize_agent()
|
|
@@ -196,7 +198,7 @@ class GaiaAgent:
|
|
| 196 |
print(f"🔹 Task ID: {task_id}")
|
| 197 |
print(f"🔹 Question: {question[:100]}...")
|
| 198 |
|
| 199 |
-
# Try to get answer with retry logic
|
| 200 |
answer = None
|
| 201 |
for attempt in range(self.max_retries + 1):
|
| 202 |
try:
|
|
@@ -204,13 +206,33 @@ class GaiaAgent:
|
|
| 204 |
if answer:
|
| 205 |
break
|
| 206 |
except Exception as e:
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
else:
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
if not answer:
|
| 216 |
answer = "⚠️ Sorry, I could not generate a valid response."
|
|
|
|
| 150 |
|
| 151 |
# Rate limiting
|
| 152 |
self.last_call_time = 0
|
| 153 |
+
self.min_delay = 10 # 10 seconds between tasks to avoid rate limits
|
| 154 |
+
self.tokens_used_in_window = 0
|
| 155 |
+
self.window_start_time = time.time()
|
| 156 |
|
| 157 |
# Initialize agent with primary model
|
| 158 |
self._reinitialize_agent()
|
|
|
|
| 198 |
print(f"🔹 Task ID: {task_id}")
|
| 199 |
print(f"🔹 Question: {question[:100]}...")
|
| 200 |
|
| 201 |
+
# Try to get answer with retry logic and exponential backoff
|
| 202 |
answer = None
|
| 203 |
for attempt in range(self.max_retries + 1):
|
| 204 |
try:
|
|
|
|
| 206 |
if answer:
|
| 207 |
break
|
| 208 |
except Exception as e:
|
| 209 |
+
error_str = str(e)
|
| 210 |
+
print(f"⚠️ Attempt {attempt + 1} failed: {error_str[:150]}")
|
| 211 |
+
|
| 212 |
+
# Check if it's a rate limit error
|
| 213 |
+
if "rate_limit" in error_str.lower() or "Rate limit" in error_str:
|
| 214 |
+
# Extract wait time if available
|
| 215 |
+
import re
|
| 216 |
+
wait_match = re.search(r'(\d+\.?\d*)\s*s', error_str)
|
| 217 |
+
if wait_match:
|
| 218 |
+
wait_time = float(wait_match.group(1)) + 2 # Add 2s buffer
|
| 219 |
+
else:
|
| 220 |
+
wait_time = 15 * (attempt + 1) # Exponential backoff: 15s, 30s, 45s
|
| 221 |
+
|
| 222 |
+
print(f"⏳ Rate limit hit. Waiting {wait_time:.1f}s before retry...")
|
| 223 |
+
time.sleep(wait_time)
|
| 224 |
+
|
| 225 |
+
if attempt < self.max_retries:
|
| 226 |
+
print(f"🔄 Retrying (attempt {attempt + 2}/{self.max_retries + 1})...")
|
| 227 |
+
continue
|
| 228 |
else:
|
| 229 |
+
# Non-rate-limit error
|
| 230 |
+
if attempt < self.max_retries:
|
| 231 |
+
print(f"🔄 Retrying with fresh agent...")
|
| 232 |
+
self._reinitialize_agent()
|
| 233 |
+
time.sleep(2)
|
| 234 |
+
else:
|
| 235 |
+
answer = f"⚠️ Agent failed after {self.max_retries + 1} attempts: {e}"
|
| 236 |
|
| 237 |
if not answer:
|
| 238 |
answer = "⚠️ Sorry, I could not generate a valid response."
|