Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,8 @@ import pandas as pd
|
|
6 |
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
7 |
from smolagents.tools import Tool
|
8 |
import time
|
|
|
|
|
9 |
|
10 |
# (Keep Constants as is)
|
11 |
# --- Constants ---
|
@@ -13,6 +15,26 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
13 |
|
14 |
# --- Basic Agent Definition ---
|
15 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
class BasicAgent:
|
17 |
def __init__(self):
|
18 |
self.agent = CodeAgent(
|
@@ -24,13 +46,25 @@ class BasicAgent:
|
|
24 |
add_base_tools=True,
|
25 |
)
|
26 |
print("BasicAgent initialized.")
|
|
|
27 |
def __call__(self, question: str) -> str:
|
28 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
31 |
return fixed_answer
|
32 |
|
33 |
|
|
|
34 |
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
35 |
"""
|
36 |
Helper function to safely call the agent with retry on rate limit errors (HTTP 429).
|
|
|
6 |
from smolagents import OpenAIServerModel, DuckDuckGoSearchTool, CodeAgent, WikipediaSearchTool
|
7 |
from smolagents.tools import Tool
|
8 |
import time
|
9 |
+
import openai
|
10 |
+
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
|
11 |
|
12 |
# (Keep Constants as is)
|
13 |
# --- Constants ---
|
|
|
15 |
|
16 |
# --- Basic Agent Definition ---
|
17 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
18 |
+
|
19 |
+
class RateLimitException(Exception):
|
20 |
+
pass
|
21 |
+
|
22 |
+
@retry(
|
23 |
+
reraise=True,
|
24 |
+
stop=stop_after_attempt(10),
|
25 |
+
wait=wait_exponential(multiplier=20, min=20, max=120),
|
26 |
+
retry=retry_if_exception_type(RateLimitException),
|
27 |
+
)
|
28 |
+
def call_openai_with_retry(**kwargs):
|
29 |
+
try:
|
30 |
+
return openai.ChatCompletion.create(**kwargs)
|
31 |
+
except openai.error.RateLimitError as e:
|
32 |
+
print("Rate limit error detected. Will retry...")
|
33 |
+
raise RateLimitException from e
|
34 |
+
except Exception as e:
|
35 |
+
print(f"Non-rate-limit error occurred: {e}")
|
36 |
+
raise
|
37 |
+
|
38 |
class BasicAgent:
|
39 |
def __init__(self):
|
40 |
self.agent = CodeAgent(
|
|
|
46 |
add_base_tools=True,
|
47 |
)
|
48 |
print("BasicAgent initialized.")
|
49 |
+
|
50 |
def __call__(self, question: str) -> str:
|
51 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
52 |
+
try:
|
53 |
+
fixed_answer = call_openai_with_retry(
|
54 |
+
model="gpt-4o",
|
55 |
+
messages=[
|
56 |
+
{"role": "user", "content": question}
|
57 |
+
],
|
58 |
+
temperature=0.2,
|
59 |
+
)['choices'][0]['message']['content']
|
60 |
+
except Exception as e:
|
61 |
+
print(f"Agent error: {e}")
|
62 |
+
raise
|
63 |
print(f"Agent returning fixed answer: {fixed_answer}")
|
64 |
return fixed_answer
|
65 |
|
66 |
|
67 |
+
|
68 |
def safe_agent_call(agent, question, retries=5, wait_time=20):
|
69 |
"""
|
70 |
Helper function to safely call the agent with retry on rate limit errors (HTTP 429).
|