Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -348,6 +348,8 @@ class SlpMultiAgent:
|
|
| 348 |
|
| 349 |
async def __call__(self, question: str) -> str:
|
| 350 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
|
|
|
| 351 |
|
| 352 |
# Truncate question to avoid exceeding model context length
|
| 353 |
MAX_QUESTION_LENGTH = 1000
|
|
@@ -371,10 +373,10 @@ class SlpMultiAgent:
|
|
| 371 |
|
| 372 |
# Simplified research agent
|
| 373 |
research_agent = CodeAgent(
|
| 374 |
-
tools=[GoogleSearchTool(), KnowledgeBaseTool()
|
| 375 |
model=model if not isinstance(model, GeminiModel) else OpenAIServerModel(model_id="gpt-3.5-turbo", temperature=0.0, max_tokens=400),
|
| 376 |
additional_authorized_imports=["re", "json"],
|
| 377 |
-
max_steps=
|
| 378 |
name="ResearchAgent",
|
| 379 |
verbosity_level=0,
|
| 380 |
description="Simple research with Google Search."
|
|
@@ -384,23 +386,23 @@ class SlpMultiAgent:
|
|
| 384 |
tools=[MathTool(), GoogleSearchTool()],
|
| 385 |
model=model if not isinstance(model, GeminiModel) else OpenAIServerModel(model_id="gpt-3.5-turbo", temperature=0.0, max_tokens=400),
|
| 386 |
additional_authorized_imports=["math", "re"],
|
| 387 |
-
max_steps=
|
| 388 |
name="SolverAgent",
|
| 389 |
verbosity_level=0,
|
| 390 |
description="Simple problem solving with math and search."
|
| 391 |
)
|
| 392 |
|
| 393 |
manager_agent = CodeAgent(
|
| 394 |
-
model=model if not isinstance(model, GeminiModel) else OpenAIServerModel(model_id="gpt-3.5-turbo", temperature=0.0, max_tokens=400),
|
| 395 |
-
tools=[GoogleSearchTool(), MathTool(), FileProcessorTool(), KnowledgeBaseTool()
|
| 396 |
managed_agents=[research_agent, solver_agent],
|
| 397 |
name="ManagerAgent",
|
| 398 |
description="Manager with comprehensive tool access and agent coordination.",
|
| 399 |
additional_authorized_imports=["re", "math", "json", "pandas", "numpy", "requests", "time", "os", "tempfile", "uuid"],
|
| 400 |
planning_interval=1,
|
| 401 |
verbosity_level=0,
|
| 402 |
-
max_steps=
|
| 403 |
-
final_answer_checks=[
|
| 404 |
)
|
| 405 |
|
| 406 |
# Create a task for the agent run with retry mechanism for rate limits
|
|
@@ -413,57 +415,18 @@ class SlpMultiAgent:
|
|
| 413 |
result = await loop.run_in_executor(
|
| 414 |
None,
|
| 415 |
lambda: manager_agent.run(f"""
|
| 416 |
-
|
| 417 |
-
If you do, you will be penalized and your answer will be rejected.
|
| 418 |
-
|
| 419 |
-
INSTRUCTIONS:
|
| 420 |
-
- For every question, you MUST call the most appropriate tool to get the answer.
|
| 421 |
-
- You MUST NOT output any code blocks, markdown, or print statements.
|
| 422 |
-
- You MUST NOT use triple backticks or <code> tags.
|
| 423 |
-
- Only call the tool and then call final_answer('...').
|
| 424 |
-
- If you cannot answer, call final_answer('I don't know').
|
| 425 |
-
- STRICTLY FOLLOW THIS FORMAT. DO NOT OUTPUT ANYTHING ELSE.
|
| 426 |
-
- If you reach max steps, call final_answer('I don't know').
|
| 427 |
-
|
| 428 |
-
BAD EXAMPLES (DO NOT DO THIS):
|
| 429 |
-
```python\nfinal_answer('foo')\n```
|
| 430 |
-
<code>final_answer('foo')</code>
|
| 431 |
-
Thoughts: ...
|
| 432 |
-
Plan: ...
|
| 433 |
-
|
| 434 |
-
GOOD EXAMPLES (DO THIS):
|
| 435 |
-
final_answer('Paris')
|
| 436 |
-
final_answer('4')
|
| 437 |
-
final_answer('I don't know')
|
| 438 |
|
| 439 |
-
|
| 440 |
-
- google_search(query): Search the web
|
| 441 |
-
- math_calculator(expression): Calculate math
|
| 442 |
-
- file_processor(action, data): Handle files
|
| 443 |
-
- knowledge_base(topic): Get knowledge
|
| 444 |
-
- wikipedia_search(query): Search Wikipedia
|
| 445 |
-
- code_executor(code): Execute Python code
|
| 446 |
-
- web_scraper(url): Scrape web pages
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
A: final_answer('4')
|
| 453 |
-
Q: Who won the 2020 Olympics 100m?
|
| 454 |
-
A: final_answer('Lamont Marcell Jacobs')
|
| 455 |
-
Q: What is the square root of 81?
|
| 456 |
-
A: final_answer('9')
|
| 457 |
-
Q: What is the population of Japan?
|
| 458 |
-
A: final_answer('125.7 million')
|
| 459 |
-
Q: What is the output of print(2**3)?
|
| 460 |
-
A: final_answer('8')
|
| 461 |
-
Q: What is the main ingredient in guacamole?
|
| 462 |
-
A: final_answer('avocado')
|
| 463 |
|
| 464 |
-
|
| 465 |
|
| 466 |
-
|
| 467 |
""")
|
| 468 |
)
|
| 469 |
break # Success, exit retry loop
|
|
@@ -484,51 +447,33 @@ class SlpMultiAgent:
|
|
| 484 |
if result is None:
|
| 485 |
return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
|
| 486 |
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
def strip_code_blocks(text):
|
| 490 |
-
# Remove triple backtick code blocks
|
| 491 |
-
text = re.sub(r"```[\s\S]*?```", "", text)
|
| 492 |
-
# Remove <code>...</code> blocks
|
| 493 |
-
text = re.sub(r"<code>[\s\S]*?</code>", "", text)
|
| 494 |
-
# Remove markdown headers and print statements
|
| 495 |
-
text = re.sub(r"^#+.*$", "", text, flags=re.MULTILINE)
|
| 496 |
-
text = re.sub(r"print\(.*?\)", "", text)
|
| 497 |
-
return text.strip()
|
| 498 |
-
def extract_final_answer(text):
|
| 499 |
-
# Try to find final_answer('...') or final_answer("...")
|
| 500 |
-
match = re.search(r"final_answer\(['\"](.*?)['\"]\)", text, re.DOTALL)
|
| 501 |
-
if match:
|
| 502 |
-
return match.group(1).strip()
|
| 503 |
-
# Try to find final_answer(...) with any content
|
| 504 |
-
match = re.search(r"final_answer\((.*?)\)", text, re.DOTALL)
|
| 505 |
-
if match:
|
| 506 |
-
return match.group(1).strip().strip("'\"")
|
| 507 |
-
# As a last resort, return the first non-empty line
|
| 508 |
-
for line in text.splitlines():
|
| 509 |
-
if line.strip():
|
| 510 |
-
return line.strip()
|
| 511 |
-
return text.strip()
|
| 512 |
-
# Post-process the result to remove code blocks and extract the answer
|
| 513 |
if result and isinstance(result, str):
|
| 514 |
-
#
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
# Return the result from the agent
|
| 531 |
-
return "
|
| 532 |
|
| 533 |
def check_reasoning(final_answer, agent_memory):
|
| 534 |
# Skip expensive validation to save costs
|
|
|
|
| 348 |
|
| 349 |
async def __call__(self, question: str) -> str:
|
| 350 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 351 |
+
fixed_answer = "This is a default answer."
|
| 352 |
+
print(f"Agent returning fixed answer: {fixed_answer}")
|
| 353 |
|
| 354 |
# Truncate question to avoid exceeding model context length
|
| 355 |
MAX_QUESTION_LENGTH = 1000
|
|
|
|
| 373 |
|
| 374 |
# Simplified research agent
|
| 375 |
research_agent = CodeAgent(
|
| 376 |
+
tools=[GoogleSearchTool(), KnowledgeBaseTool()],
|
| 377 |
model=model if not isinstance(model, GeminiModel) else OpenAIServerModel(model_id="gpt-3.5-turbo", temperature=0.0, max_tokens=400),
|
| 378 |
additional_authorized_imports=["re", "json"],
|
| 379 |
+
max_steps=5,
|
| 380 |
name="ResearchAgent",
|
| 381 |
verbosity_level=0,
|
| 382 |
description="Simple research with Google Search."
|
|
|
|
| 386 |
tools=[MathTool(), GoogleSearchTool()],
|
| 387 |
model=model if not isinstance(model, GeminiModel) else OpenAIServerModel(model_id="gpt-3.5-turbo", temperature=0.0, max_tokens=400),
|
| 388 |
additional_authorized_imports=["math", "re"],
|
| 389 |
+
max_steps=5,
|
| 390 |
name="SolverAgent",
|
| 391 |
verbosity_level=0,
|
| 392 |
description="Simple problem solving with math and search."
|
| 393 |
)
|
| 394 |
|
| 395 |
manager_agent = CodeAgent(
|
| 396 |
+
model=model if not isinstance(model, GeminiModel) else OpenAIServerModel(model_id="gpt-3.5-turbo", temperature=0.0, max_tokens=400),
|
| 397 |
+
tools=[GoogleSearchTool(), MathTool(), FileProcessorTool(), KnowledgeBaseTool()],
|
| 398 |
managed_agents=[research_agent, solver_agent],
|
| 399 |
name="ManagerAgent",
|
| 400 |
description="Manager with comprehensive tool access and agent coordination.",
|
| 401 |
additional_authorized_imports=["re", "math", "json", "pandas", "numpy", "requests", "time", "os", "tempfile", "uuid"],
|
| 402 |
planning_interval=1,
|
| 403 |
verbosity_level=0,
|
| 404 |
+
max_steps=8,
|
| 405 |
+
final_answer_checks=[]
|
| 406 |
)
|
| 407 |
|
| 408 |
# Create a task for the agent run with retry mechanism for rate limits
|
|
|
|
| 415 |
result = await loop.run_in_executor(
|
| 416 |
None,
|
| 417 |
lambda: manager_agent.run(f"""
|
| 418 |
+
Question: {short_question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 419 |
|
| 420 |
+
Answer this question step by step:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
+
1. If it's a factual question, use google_search() to find current information
|
| 423 |
+
2. If it's a math problem, use math_calculator() for calculations
|
| 424 |
+
3. Think through the problem logically
|
| 425 |
+
4. Provide a clear, specific answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
+
IMPORTANT: Always end with final_answer("your specific answer")
|
| 428 |
|
| 429 |
+
Be precise and factual in your response.
|
| 430 |
""")
|
| 431 |
)
|
| 432 |
break # Success, exit retry loop
|
|
|
|
| 447 |
if result is None:
|
| 448 |
return "I apologize, but I'm currently experiencing technical difficulties. Please try again later."
|
| 449 |
|
| 450 |
+
|
| 451 |
+
# Extract clean answer from result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
if result and isinstance(result, str):
|
| 453 |
+
# Look for final_answer pattern - more flexible regex
|
| 454 |
+
import re
|
| 455 |
+
final_answer_patterns = [
|
| 456 |
+
r'final_answer\(["\']([^"\']*)["\'\)]',
|
| 457 |
+
r'final_answer\("([^"]*)",?\)',
|
| 458 |
+
r'final_answer\(\'([^\']*)\',?\)',
|
| 459 |
+
r'final_answer\(([^)]+)\)'
|
| 460 |
+
]
|
| 461 |
+
|
| 462 |
+
for pattern in final_answer_patterns:
|
| 463 |
+
match = re.search(pattern, result, re.IGNORECASE)
|
| 464 |
+
if match:
|
| 465 |
+
clean_answer = match.group(1).strip('"\'')
|
| 466 |
+
return clean_answer
|
| 467 |
+
|
| 468 |
+
# If no final_answer found, try to extract the last meaningful line
|
| 469 |
+
lines = result.strip().split('\n')
|
| 470 |
+
for line in reversed(lines):
|
| 471 |
+
line = line.strip()
|
| 472 |
+
if line and not line.startswith('#') and not line.startswith('###') and len(line) < 200:
|
| 473 |
+
return line
|
| 474 |
+
|
| 475 |
# Return the result from the agent
|
| 476 |
+
return result if result else "Unable to determine answer."
|
| 477 |
|
| 478 |
def check_reasoning(final_answer, agent_memory):
|
| 479 |
# Skip expensive validation to save costs
|