Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -119,7 +119,25 @@ class BasicAgent:
|
|
| 119 |
raise ValueError("ANTHROPIC_API_KEY not found in environment variables.")
|
| 120 |
|
| 121 |
self.model_name = "claude-3-haiku-20240307"
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
self.rate_limiter = RateLimiter()
|
| 124 |
|
| 125 |
# Initialize tools
|
|
@@ -143,8 +161,13 @@ class BasicAgent:
|
|
| 143 |
# Create a comprehensive prompt for Claude
|
| 144 |
prompt = self._create_prompt(question)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
agent_answer = response.content
|
| 149 |
|
| 150 |
print(f"Agent returning answer: {agent_answer[:100]}...")
|
|
@@ -152,20 +175,39 @@ class BasicAgent:
|
|
| 152 |
|
| 153 |
except Exception as e:
|
| 154 |
error_msg = str(e)
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
-
# Check
|
| 158 |
-
if "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
if attempt < max_retries - 1:
|
| 160 |
-
wait_time = (attempt + 1) * 30 #
|
| 161 |
print(f"Rate limit hit. Waiting {wait_time} seconds before retry...")
|
| 162 |
time.sleep(wait_time)
|
| 163 |
continue
|
| 164 |
else:
|
| 165 |
return f"RATE_LIMIT_ERROR: {error_msg}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
else:
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
return "MAX_RETRIES_EXCEEDED"
|
| 171 |
|
|
@@ -304,9 +346,15 @@ def run_and_submit_all(profile: gr.OAuthProfile | None, progress=gr.Progress()):
|
|
| 304 |
submitted_answer = agent(question_text)
|
| 305 |
|
| 306 |
# Check if the answer indicates an error
|
| 307 |
-
if submitted_answer.startswith(("RATE_LIMIT_ERROR", "AGENT_ERROR", "MAX_RETRIES_EXCEEDED")):
|
| 308 |
print(f"Error processing task {task_id}: {submitted_answer}")
|
| 309 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
# Don't add to answers_payload for submission if it's an error
|
| 311 |
continue
|
| 312 |
|
|
|
|
| 119 |
raise ValueError("ANTHROPIC_API_KEY not found in environment variables.")
|
| 120 |
|
| 121 |
self.model_name = "claude-3-haiku-20240307"
|
| 122 |
+
|
| 123 |
+
# Test API key and connection
|
| 124 |
+
print("Testing Anthropic API connection...")
|
| 125 |
+
try:
|
| 126 |
+
self.chat_model = ChatAnthropic(
|
| 127 |
+
model=self.model_name,
|
| 128 |
+
anthropic_api_key=API_KEY,
|
| 129 |
+
timeout=30,
|
| 130 |
+
max_retries=2
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Test with a simple query
|
| 134 |
+
test_response = self.chat_model.invoke("Hello, are you working?")
|
| 135 |
+
print(f"✅ API connection successful! Test response: {test_response.content[:50]}...")
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
print(f"❌ API connection failed: {e}")
|
| 139 |
+
raise ValueError(f"Failed to initialize Claude model: {e}")
|
| 140 |
+
|
| 141 |
self.rate_limiter = RateLimiter()
|
| 142 |
|
| 143 |
# Initialize tools
|
|
|
|
| 161 |
# Create a comprehensive prompt for Claude
|
| 162 |
prompt = self._create_prompt(question)
|
| 163 |
|
| 164 |
+
print(f"Attempt {attempt + 1}: Sending request to Claude...")
|
| 165 |
+
|
| 166 |
+
# Get response from Claude with timeout
|
| 167 |
+
response = self.chat_model.invoke(
|
| 168 |
+
prompt,
|
| 169 |
+
config={"timeout": 30} # 30 second timeout
|
| 170 |
+
)
|
| 171 |
agent_answer = response.content
|
| 172 |
|
| 173 |
print(f"Agent returning answer: {agent_answer[:100]}...")
|
|
|
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
error_msg = str(e)
|
| 178 |
+
error_type = type(e).__name__
|
| 179 |
+
print(f"Attempt {attempt + 1} failed with {error_type}: {error_msg}")
|
| 180 |
|
| 181 |
+
# Check specific error types
|
| 182 |
+
if "connection" in error_msg.lower() or "timeout" in error_msg.lower():
|
| 183 |
+
if attempt < max_retries - 1:
|
| 184 |
+
wait_time = (attempt + 1) * 10 # Progressive backoff for connection issues
|
| 185 |
+
print(f"Connection issue detected. Waiting {wait_time} seconds before retry...")
|
| 186 |
+
time.sleep(wait_time)
|
| 187 |
+
continue
|
| 188 |
+
else:
|
| 189 |
+
return f"CONNECTION_ERROR: {error_msg}"
|
| 190 |
+
|
| 191 |
+
elif "rate limit" in error_msg.lower() or "429" in error_msg:
|
| 192 |
if attempt < max_retries - 1:
|
| 193 |
+
wait_time = (attempt + 1) * 30 # Longer wait for rate limits
|
| 194 |
print(f"Rate limit hit. Waiting {wait_time} seconds before retry...")
|
| 195 |
time.sleep(wait_time)
|
| 196 |
continue
|
| 197 |
else:
|
| 198 |
return f"RATE_LIMIT_ERROR: {error_msg}"
|
| 199 |
+
|
| 200 |
+
elif "authentication" in error_msg.lower() or "api key" in error_msg.lower():
|
| 201 |
+
return f"AUTH_ERROR: {error_msg}"
|
| 202 |
+
|
| 203 |
else:
|
| 204 |
+
if attempt < max_retries - 1:
|
| 205 |
+
wait_time = (attempt + 1) * 5 # Short wait for other errors
|
| 206 |
+
print(f"Unknown error. Waiting {wait_time} seconds before retry...")
|
| 207 |
+
time.sleep(wait_time)
|
| 208 |
+
continue
|
| 209 |
+
else:
|
| 210 |
+
return f"AGENT_ERROR: {error_msg}"
|
| 211 |
|
| 212 |
return "MAX_RETRIES_EXCEEDED"
|
| 213 |
|
|
|
|
| 346 |
submitted_answer = agent(question_text)
|
| 347 |
|
| 348 |
# Check if the answer indicates an error
|
| 349 |
+
if submitted_answer.startswith(("RATE_LIMIT_ERROR", "AGENT_ERROR", "MAX_RETRIES_EXCEEDED", "CONNECTION_ERROR", "AUTH_ERROR")):
|
| 350 |
print(f"Error processing task {task_id}: {submitted_answer}")
|
| 351 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 352 |
+
|
| 353 |
+
# For authentication errors, stop processing
|
| 354 |
+
if submitted_answer.startswith("AUTH_ERROR"):
|
| 355 |
+
print("Authentication error detected. Stopping processing.")
|
| 356 |
+
break
|
| 357 |
+
|
| 358 |
# Don't add to answers_payload for submission if it's an error
|
| 359 |
continue
|
| 360 |
|