Spaces:
Sleeping
Sleeping
Update src/agent.py
Browse files- src/agent.py +25 -10
src/agent.py
CHANGED
|
@@ -40,35 +40,50 @@ class BasicAgent():
|
|
| 40 |
|
| 41 |
while True:
|
| 42 |
try:
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if isinstance(step, str):
|
| 45 |
final_answer = step
|
| 46 |
-
print(f"
|
| 47 |
continue
|
|
|
|
|
|
|
| 48 |
step_name = step.__class__.__name__
|
| 49 |
output = getattr(step, "output", None)
|
|
|
|
| 50 |
if output:
|
| 51 |
-
print(f"Step: {step_name} Output: {
|
|
|
|
| 52 |
self.token_rate_limiter.maybe_wait(self.expected_tokens_per_step)
|
| 53 |
tokens_used = getattr(step, "token_usage", None)
|
| 54 |
if tokens_used:
|
| 55 |
-
|
| 56 |
|
|
|
|
| 57 |
if step_name == "FinalAnswerStep":
|
| 58 |
-
final_answer =
|
| 59 |
-
print(f"Final Answer
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
except Exception as e:
|
|
|
|
| 62 |
if "overload" in str(e).lower() or "rate limit" in str(e).lower():
|
| 63 |
print("Rate limit exceeded. Retrying...")
|
| 64 |
if retry_count >= self.max_retries:
|
| 65 |
print("Max retries reached. Exiting...")
|
| 66 |
break
|
| 67 |
delay = self.base_delay * (2 ** retry_count) + random.random()
|
| 68 |
-
print(f"
|
| 69 |
time.sleep(delay)
|
| 70 |
-
retry_count += 1
|
| 71 |
-
|
| 72 |
else:
|
| 73 |
print(f"Error: {e}")
|
| 74 |
break
|
|
|
|
| 40 |
|
| 41 |
while True:
|
| 42 |
try:
|
| 43 |
+
# Run the agent
|
| 44 |
+
steps = self.agent.run(input_text)
|
| 45 |
+
|
| 46 |
+
# If steps is a string, convert it to a single-item list
|
| 47 |
+
if isinstance(steps, str):
|
| 48 |
+
steps = [steps]
|
| 49 |
+
|
| 50 |
+
for step in steps:
|
| 51 |
+
# Handle string steps
|
| 52 |
if isinstance(step, str):
|
| 53 |
final_answer = step
|
| 54 |
+
print(f"Step: String Output: {final_answer}")
|
| 55 |
continue
|
| 56 |
+
|
| 57 |
+
# Handle object steps
|
| 58 |
step_name = step.__class__.__name__
|
| 59 |
output = getattr(step, "output", None)
|
| 60 |
+
|
| 61 |
if output:
|
| 62 |
+
print(f"Step: {step_name} Output: {output}")
|
| 63 |
+
|
| 64 |
self.token_rate_limiter.maybe_wait(self.expected_tokens_per_step)
|
| 65 |
tokens_used = getattr(step, "token_usage", None)
|
| 66 |
if tokens_used:
|
| 67 |
+
self.token_rate_limiter.add_tokens(tokens_used.input_tokens)
|
| 68 |
|
| 69 |
+
# Capture the final answer from the final answer step
|
| 70 |
if step_name == "FinalAnswerStep":
|
| 71 |
+
final_answer = output
|
| 72 |
+
print(f"Captured Final Answer from step: {final_answer}")
|
| 73 |
+
|
| 74 |
+
break # Exit retry loop if successful
|
| 75 |
+
|
| 76 |
except Exception as e:
|
| 77 |
+
# Handle API overload/rate limits
|
| 78 |
if "overload" in str(e).lower() or "rate limit" in str(e).lower():
|
| 79 |
print("Rate limit exceeded. Retrying...")
|
| 80 |
if retry_count >= self.max_retries:
|
| 81 |
print("Max retries reached. Exiting...")
|
| 82 |
break
|
| 83 |
delay = self.base_delay * (2 ** retry_count) + random.random()
|
| 84 |
+
print(f"Retrying in {delay:.1f}s ... ({e})")
|
| 85 |
time.sleep(delay)
|
| 86 |
+
retry_count += 1
|
|
|
|
| 87 |
else:
|
| 88 |
print(f"Error: {e}")
|
| 89 |
break
|