Commit ·
80eaceb
1
Parent(s): 5d282cd
Add aggressive minimal answer enforcement
Browse files- Completely revamped system prompt with much stricter minimal answer instructions
- Added post-processing to extract minimal answers for different question types
- Implemented regex-based extraction for numbers and specific list formats
- Added final reminder in the user prompt to reinforce minimal answers
- Added detailed answer processing logging to show original vs. minimal answers
- direct_agent_fixed.py +128 -16
direct_agent_fixed.py
CHANGED
|
@@ -455,14 +455,23 @@ When presented with a question:
|
|
| 455 |
4. Always present your final answer directly without adding 'FINAL ANSWER:' or similar phrases
|
| 456 |
5. If you use research or external information, incorporate it naturally into your response
|
| 457 |
|
| 458 |
-
CRITICAL INSTRUCTION: Your final answer
|
| 459 |
-
-
|
| 460 |
-
-
|
| 461 |
-
-
|
| 462 |
-
-
|
| 463 |
-
-
|
| 464 |
-
-
|
| 465 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 466 |
|
| 467 |
For specialized types of questions:
|
| 468 |
- For YouTube video questions, use the analyze_youtube tool to get information about the video
|
|
@@ -470,7 +479,7 @@ For specialized types of questions:
|
|
| 470 |
- For Excel/spreadsheet file questions, use the analyze_excel tool with proper parameters
|
| 471 |
- When calculations are needed, use the calculate tool to ensure accurate results
|
| 472 |
|
| 473 |
-
|
| 474 |
|
| 475 |
def _handle_tool_call(self, tool_calls):
|
| 476 |
results = []
|
|
@@ -623,6 +632,84 @@ Be extremely precise and minimal in your final answer. The answer should never i
|
|
| 623 |
# If we've exhausted retries, raise an exception
|
| 624 |
raise Exception(f"Failed after {max_retries} retries due to rate limits")
|
| 625 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
def __call__(self, question, context=None):
|
| 627 |
"""Run the agent on a question and return the answer."""
|
| 628 |
# Add context about any files if provided
|
|
@@ -641,9 +728,21 @@ Be extremely precise and minimal in your final answer. The answer should never i
|
|
| 641 |
question += f"\n\nAttached file information: {file_name}"
|
| 642 |
question += f"\nYou can access this file using the read_file tool with the path: {file_path}"
|
| 643 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 644 |
messages = [
|
| 645 |
{"role": "system", "content": self.system_message},
|
| 646 |
-
{"role": "user", "content":
|
| 647 |
]
|
| 648 |
|
| 649 |
max_steps = 10 # Increased the number of iterations for more complex questions
|
|
@@ -695,7 +794,16 @@ Be extremely precise and minimal in your final answer. The answer should never i
|
|
| 695 |
print("Error: Message has no content attribute")
|
| 696 |
return "Error: Invalid response from OpenRouter API - message has no content"
|
| 697 |
|
| 698 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 699 |
|
| 700 |
except Exception as e:
|
| 701 |
print(f"Error in step {step+1}: {str(e)}")
|
|
@@ -704,19 +812,23 @@ Be extremely precise and minimal in your final answer. The answer should never i
|
|
| 704 |
if messages and len(messages) > 2:
|
| 705 |
last_message = messages[-1]
|
| 706 |
if isinstance(last_message, dict) and 'content' in last_message:
|
| 707 |
-
|
|
|
|
| 708 |
elif hasattr(last_message, 'content') and last_message.content:
|
| 709 |
-
|
|
|
|
| 710 |
|
| 711 |
-
return f"Error
|
| 712 |
|
| 713 |
# If we've reached the maximum number of steps, return the last message
|
| 714 |
if messages and len(messages) > 1:
|
| 715 |
last_message = messages[-1]
|
| 716 |
if isinstance(last_message, dict) and 'content' in last_message:
|
| 717 |
-
|
|
|
|
| 718 |
elif hasattr(last_message, 'content') and last_message.content:
|
| 719 |
-
|
|
|
|
| 720 |
|
| 721 |
return "Error: Reached maximum steps without getting a final answer"
|
| 722 |
|
|
|
|
| 455 |
4. Always present your final answer directly without adding 'FINAL ANSWER:' or similar phrases
|
| 456 |
5. If you use research or external information, incorporate it naturally into your response
|
| 457 |
|
| 458 |
+
CRITICAL INSTRUCTION: Your final answer MUST be EXTREMELY minimal. The evaluation system expects the shortest possible answer:
|
| 459 |
+
- For "How many..." questions: JUST THE NUMBER with no words (e.g., "2" not "2 albums")
|
| 460 |
+
- For name questions: JUST THE NAME with no context (e.g., "John Smith" not "The person is John Smith")
|
| 461 |
+
- For date questions: JUST THE DATE with no explanation (e.g., "1995" not "It happened in 1995")
|
| 462 |
+
- For "Who..." questions: JUST THE NAME(S) (e.g., "Marie Curie" not "The scientist was Marie Curie")
|
| 463 |
+
- For "Where..." questions: JUST THE LOCATION (e.g., "Paris" not "It's located in Paris")
|
| 464 |
+
- For sets/lists: JUST THE ITEMS with minimal formatting (e.g., "a,b,c" or "a, b, c")
|
| 465 |
+
- For yes/no questions: JUST "Yes" or "No" (not "Yes, because...")
|
| 466 |
+
|
| 467 |
+
DO NOT INCLUDE:
|
| 468 |
+
- Explanations or reasoning
|
| 469 |
+
- Your thought process
|
| 470 |
+
- Citations or references
|
| 471 |
+
- Introductory phrases like "The answer is..."
|
| 472 |
+
- Confidence statements like "I believe..."
|
| 473 |
+
- Context or background information
|
| 474 |
+
- Multiple possible answers (choose the most likely one)
|
| 475 |
|
| 476 |
For specialized types of questions:
|
| 477 |
- For YouTube video questions, use the analyze_youtube tool to get information about the video
|
|
|
|
| 479 |
- For Excel/spreadsheet file questions, use the analyze_excel tool with proper parameters
|
| 480 |
- When calculations are needed, use the calculate tool to ensure accurate results
|
| 481 |
|
| 482 |
+
THE FINAL ANSWER MUST BE EXTREMELY MINIMAL - think of it as filling in a blank or answering a multiple-choice test. Your reasoning is valuable during your investigation, but your final response should ONLY contain the direct answer with nothing else."""
|
| 483 |
|
| 484 |
def _handle_tool_call(self, tool_calls):
|
| 485 |
results = []
|
|
|
|
| 632 |
# If we've exhausted retries, raise an exception
|
| 633 |
raise Exception(f"Failed after {max_retries} retries due to rate limits")
|
| 634 |
|
| 635 |
+
def _enforce_minimal_answer(self, answer, question):
|
| 636 |
+
"""Enforce minimal answer requirements by post-processing the LLM's response."""
|
| 637 |
+
# Check if the answer already looks minimal (very short)
|
| 638 |
+
if len(answer.strip()) <= 20:
|
| 639 |
+
# Already seems minimal, but clean up any extra text
|
| 640 |
+
minimal = answer.strip()
|
| 641 |
+
# Remove phrases like "the answer is"
|
| 642 |
+
for phrase in ["the answer is", "i believe", "i think", "i found", "the result is"]:
|
| 643 |
+
if minimal.lower().startswith(phrase):
|
| 644 |
+
minimal = minimal[len(phrase):].strip()
|
| 645 |
+
# Remove any leading colon or punctuation
|
| 646 |
+
minimal = minimal.lstrip(":,.;").strip()
|
| 647 |
+
return minimal
|
| 648 |
+
|
| 649 |
+
# Try to extract just the answer based on question type
|
| 650 |
+
question_lower = question.lower()
|
| 651 |
+
|
| 652 |
+
# For "how many" questions - extract just the number
|
| 653 |
+
if "how many" in question_lower:
|
| 654 |
+
import re
|
| 655 |
+
# Look for numbers in the answer
|
| 656 |
+
numbers = re.findall(r'\b\d+\b', answer)
|
| 657 |
+
if numbers:
|
| 658 |
+
return numbers[0] # Return the first number found
|
| 659 |
+
|
| 660 |
+
# For "who" questions - try to extract just the name
|
| 661 |
+
if question_lower.startswith("who"):
|
| 662 |
+
# Look for capitalized words which might be names
|
| 663 |
+
lines = answer.strip().split('\n')
|
| 664 |
+
if len(lines) > 0:
|
| 665 |
+
# Return first line, or if it's too long, first sentence
|
| 666 |
+
first_line = lines[0]
|
| 667 |
+
if len(first_line) > 100:
|
| 668 |
+
sentences = first_line.split('.')
|
| 669 |
+
return sentences[0].strip()
|
| 670 |
+
return first_line
|
| 671 |
+
|
| 672 |
+
# For lists or sets
|
| 673 |
+
if 'subset' in question_lower or 'list' in question_lower or 'set' in question_lower:
|
| 674 |
+
lines = answer.strip().split('\n')
|
| 675 |
+
for line in lines:
|
| 676 |
+
line = line.strip()
|
| 677 |
+
if ',' in line and len(line) < 50:
|
| 678 |
+
return line
|
| 679 |
+
|
| 680 |
+
# If we can't find a simple comma-separated list,
|
| 681 |
+
# look for the last line that might contain the answer
|
| 682 |
+
for line in reversed(lines):
|
| 683 |
+
line = line.strip()
|
| 684 |
+
if line and len(line) < 100:
|
| 685 |
+
return line
|
| 686 |
+
|
| 687 |
+
# For yes/no questions
|
| 688 |
+
if question_lower.startswith("is ") or question_lower.startswith("are ") or question_lower.startswith("does ") or question_lower.startswith("can "):
|
| 689 |
+
if "yes" in answer.lower()[:20]:
|
| 690 |
+
return "Yes"
|
| 691 |
+
if "no" in answer.lower()[:20]:
|
| 692 |
+
return "No"
|
| 693 |
+
|
| 694 |
+
# For questions with obvious lists at the end
|
| 695 |
+
lines = answer.strip().split('\n')
|
| 696 |
+
if len(lines) > 2:
|
| 697 |
+
last_line = lines[-1].strip()
|
| 698 |
+
if last_line and len(last_line) < 100:
|
| 699 |
+
return last_line
|
| 700 |
+
|
| 701 |
+
# If all else fails, just return the first line/sentence
|
| 702 |
+
lines = answer.strip().split('\n')
|
| 703 |
+
if lines:
|
| 704 |
+
first_line = lines[0].strip()
|
| 705 |
+
if len(first_line) > 100:
|
| 706 |
+
sentences = first_line.split('.')
|
| 707 |
+
return sentences[0].strip()
|
| 708 |
+
return first_line
|
| 709 |
+
|
| 710 |
+
# Fallback: just return the answer as is
|
| 711 |
+
return answer.strip()
|
| 712 |
+
|
| 713 |
def __call__(self, question, context=None):
|
| 714 |
"""Run the agent on a question and return the answer."""
|
| 715 |
# Add context about any files if provided
|
|
|
|
| 728 |
question += f"\n\nAttached file information: {file_name}"
|
| 729 |
question += f"\nYou can access this file using the read_file tool with the path: {file_path}"
|
| 730 |
|
| 731 |
+
# Add a special instruction to enforce minimal answers
|
| 732 |
+
extra_instruction = """
|
| 733 |
+
FINAL REMINDER: Your answer must be MINIMAL. Return ONLY the specific information requested without any explanation.
|
| 734 |
+
- For "How many" questions: ONLY return the number
|
| 735 |
+
- For "Who/what/where" questions: ONLY return the name/item/location
|
| 736 |
+
- For lists: ONLY return the items in a simple comma-separated format (e.g., "A, B, C")
|
| 737 |
+
- NEVER include your reasoning, context, or explanations
|
| 738 |
+
"""
|
| 739 |
+
|
| 740 |
+
# Add the question and special reminder
|
| 741 |
+
user_prompt = f"{question}\n\n{extra_instruction}"
|
| 742 |
+
|
| 743 |
messages = [
|
| 744 |
{"role": "system", "content": self.system_message},
|
| 745 |
+
{"role": "user", "content": user_prompt}
|
| 746 |
]
|
| 747 |
|
| 748 |
max_steps = 10 # Increased the number of iterations for more complex questions
|
|
|
|
| 794 |
print("Error: Message has no content attribute")
|
| 795 |
return "Error: Invalid response from OpenRouter API - message has no content"
|
| 796 |
|
| 797 |
+
# Apply post-processing to extract a minimal answer
|
| 798 |
+
original_answer = message.content
|
| 799 |
+
minimal_answer = self._enforce_minimal_answer(original_answer, question)
|
| 800 |
+
|
| 801 |
+
print("\n=== ANSWER PROCESSING ===")
|
| 802 |
+
print(f"ORIGINAL: {original_answer}")
|
| 803 |
+
print(f"MINIMAL: {minimal_answer}")
|
| 804 |
+
print("=========================\n")
|
| 805 |
+
|
| 806 |
+
return minimal_answer
|
| 807 |
|
| 808 |
except Exception as e:
|
| 809 |
print(f"Error in step {step+1}: {str(e)}")
|
|
|
|
| 812 |
if messages and len(messages) > 2:
|
| 813 |
last_message = messages[-1]
|
| 814 |
if isinstance(last_message, dict) and 'content' in last_message:
|
| 815 |
+
content = last_message['content']
|
| 816 |
+
return self._enforce_minimal_answer(content, question)
|
| 817 |
elif hasattr(last_message, 'content') and last_message.content:
|
| 818 |
+
content = last_message.content
|
| 819 |
+
return self._enforce_minimal_answer(content, question)
|
| 820 |
|
| 821 |
+
return f"Error: {str(e)}"
|
| 822 |
|
| 823 |
# If we've reached the maximum number of steps, return the last message
|
| 824 |
if messages and len(messages) > 1:
|
| 825 |
last_message = messages[-1]
|
| 826 |
if isinstance(last_message, dict) and 'content' in last_message:
|
| 827 |
+
content = last_message['content']
|
| 828 |
+
return self._enforce_minimal_answer(content, question)
|
| 829 |
elif hasattr(last_message, 'content') and last_message.content:
|
| 830 |
+
content = last_message.content
|
| 831 |
+
return self._enforce_minimal_answer(content, question)
|
| 832 |
|
| 833 |
return "Error: Reached maximum steps without getting a final answer"
|
| 834 |
|