Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -430,7 +430,7 @@ class SlpMultiAgent:
|
|
| 430 |
description="Answer questions using Google search, Wikipedia, and math.",
|
| 431 |
additional_authorized_imports=["re", "math", "requests"],
|
| 432 |
verbosity_level=0,
|
| 433 |
-
max_steps=
|
| 434 |
)
|
| 435 |
|
| 436 |
# Create a task for the agent run with retry mechanism for rate limits
|
|
@@ -445,20 +445,22 @@ class SlpMultiAgent:
|
|
| 445 |
lambda: manager_agent.run(f"""
|
| 446 |
Question: {short_question}
|
| 447 |
|
| 448 |
-
|
| 449 |
-
1.
|
| 450 |
-
2.
|
| 451 |
-
3.
|
| 452 |
-
4.
|
|
|
|
| 453 |
|
| 454 |
-
|
| 455 |
-
Thoughts:
|
| 456 |
<code>
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
final_answer("specific answer only")
|
| 461 |
</code>
|
|
|
|
|
|
|
| 462 |
""")
|
| 463 |
)
|
| 464 |
break # Success, exit retry loop
|
|
@@ -484,6 +486,8 @@ class SlpMultiAgent:
|
|
| 484 |
if result and isinstance(result, str):
|
| 485 |
# Look for final_answer pattern - more flexible regex
|
| 486 |
import re
|
|
|
|
|
|
|
| 487 |
final_answer_patterns = [
|
| 488 |
r'final_answer\(["\']([^"\']*)["\'\)]',
|
| 489 |
r'final_answer\("([^"]*)",?\)',
|
|
@@ -495,12 +499,27 @@ class SlpMultiAgent:
|
|
| 495 |
match = re.search(pattern, result, re.IGNORECASE | re.DOTALL)
|
| 496 |
if match:
|
| 497 |
clean_answer = match.group(1).strip('"\'')
|
| 498 |
-
# Clean up common artifacts
|
| 499 |
clean_answer = re.sub(r'^[\s\n]*', '', clean_answer)
|
| 500 |
clean_answer = re.sub(r'[\s\n]*$', '', clean_answer)
|
| 501 |
-
if clean_answer and not clean_answer.startswith('If you have'):
|
| 502 |
return clean_answer
|
| 503 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
# If no final_answer found, try to extract the last meaningful line
|
| 505 |
lines = result.strip().split('\n')
|
| 506 |
for line in reversed(lines):
|
|
|
|
| 430 |
description="Answer questions using Google search, Wikipedia, and math.",
|
| 431 |
additional_authorized_imports=["re", "math", "requests"],
|
| 432 |
verbosity_level=0,
|
| 433 |
+
max_steps=6
|
| 434 |
)
|
| 435 |
|
| 436 |
# Create a task for the agent run with retry mechanism for rate limits
|
|
|
|
| 445 |
lambda: manager_agent.run(f"""
|
| 446 |
Question: {short_question}
|
| 447 |
|
| 448 |
+
Answer this question by:
|
| 449 |
+
1. Using wikipedia_search() for Wikipedia questions
|
| 450 |
+
2. Using google_search() for other factual questions
|
| 451 |
+
3. Using math_calculator() for calculations
|
| 452 |
+
4. Extracting the specific answer from search results
|
| 453 |
+
5. Calling final_answer() with ONLY the answer
|
| 454 |
|
| 455 |
+
Example:
|
| 456 |
+
Thoughts: Need to search for information about X
|
| 457 |
<code>
|
| 458 |
+
search_result = wikipedia_search("search term")
|
| 459 |
+
# Extract specific answer from search_result
|
| 460 |
+
final_answer("just the answer")
|
|
|
|
| 461 |
</code>
|
| 462 |
+
|
| 463 |
+
IMPORTANT: final_answer() should contain ONLY the direct answer, not explanations.
|
| 464 |
""")
|
| 465 |
)
|
| 466 |
break # Success, exit retry loop
|
|
|
|
| 486 |
if result and isinstance(result, str):
|
| 487 |
# Look for final_answer pattern - more flexible regex
|
| 488 |
import re
|
| 489 |
+
|
| 490 |
+
# First try to find final_answer calls
|
| 491 |
final_answer_patterns = [
|
| 492 |
r'final_answer\(["\']([^"\']*)["\'\)]',
|
| 493 |
r'final_answer\("([^"]*)",?\)',
|
|
|
|
| 499 |
match = re.search(pattern, result, re.IGNORECASE | re.DOTALL)
|
| 500 |
if match:
|
| 501 |
clean_answer = match.group(1).strip('"\'')
|
|
|
|
| 502 |
clean_answer = re.sub(r'^[\s\n]*', '', clean_answer)
|
| 503 |
clean_answer = re.sub(r'[\s\n]*$', '', clean_answer)
|
| 504 |
+
if clean_answer and len(clean_answer) > 0 and not clean_answer.startswith('If you have'):
|
| 505 |
return clean_answer
|
| 506 |
|
| 507 |
+
# If no final_answer found, try to extract from the end of the result
|
| 508 |
+
if result:
|
| 509 |
+
# Remove code blocks and common artifacts
|
| 510 |
+
cleaned = re.sub(r'</code>.*$', '', result, flags=re.DOTALL)
|
| 511 |
+
cleaned = re.sub(r'<code>.*?</code>', '', cleaned, flags=re.DOTALL)
|
| 512 |
+
cleaned = re.sub(r'```.*?```', '', cleaned, flags=re.DOTALL)
|
| 513 |
+
|
| 514 |
+
# Get the last meaningful line
|
| 515 |
+
lines = cleaned.strip().split('\n')
|
| 516 |
+
for line in reversed(lines):
|
| 517 |
+
line = line.strip()
|
| 518 |
+
if (line and not line.startswith('#') and not line.startswith('Thoughts:')
|
| 519 |
+
and not line.startswith('•') and len(line) < 200
|
| 520 |
+
and not line.startswith('If you have')):
|
| 521 |
+
return line
|
| 522 |
+
|
| 523 |
# If no final_answer found, try to extract the last meaningful line
|
| 524 |
lines = result.strip().split('\n')
|
| 525 |
for line in reversed(lines):
|