Spaces:
Sleeping
Sleeping
Joachim M. Vis commited on
Update BasicAgent with proper reasoning logic and HF evaluation format
Browse files
app.py
CHANGED
|
@@ -13,11 +13,145 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
|
|
|
|
|
|
|
|
|
| 16 |
def __call__(self, question: str) -> str:
|
| 17 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 23 |
"""
|
|
|
|
| 13 |
class BasicAgent:
|
| 14 |
def __init__(self):
|
| 15 |
print("BasicAgent initialized.")
|
| 16 |
+
# System prompt as specified by HF evaluation requirements
|
| 17 |
+
self.system_prompt = """You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string."""
|
| 18 |
+
|
| 19 |
def __call__(self, question: str) -> str:
|
| 20 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Here you can implement your agent logic
|
| 24 |
+
# For now, I'll implement a basic reasoning approach
|
| 25 |
+
answer = self._process_question(question)
|
| 26 |
+
print(f"Agent returning answer: {answer}")
|
| 27 |
+
return answer
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error processing question: {e}")
|
| 30 |
+
return "FINAL ANSWER: unknown"
|
| 31 |
+
|
| 32 |
+
def _process_question(self, question: str) -> str:
|
| 33 |
+
"""
|
| 34 |
+
Process the question and return an answer following the required format.
|
| 35 |
+
This is where you can implement your agent's reasoning logic.
|
| 36 |
+
"""
|
| 37 |
+
# Convert question to lowercase for easier processing
|
| 38 |
+
q_lower = question.lower()
|
| 39 |
+
|
| 40 |
+
# Basic reasoning patterns - you can expand this significantly
|
| 41 |
+
if "what is" in q_lower and "plus" in q_lower:
|
| 42 |
+
# Simple math detection
|
| 43 |
+
return self._handle_math(question)
|
| 44 |
+
elif "capital" in q_lower:
|
| 45 |
+
return self._handle_capital(question)
|
| 46 |
+
elif "largest" in q_lower or "biggest" in q_lower:
|
| 47 |
+
return self._handle_largest(question)
|
| 48 |
+
elif "when" in q_lower and ("born" in q_lower or "birth" in q_lower):
|
| 49 |
+
return self._handle_birth_year(question)
|
| 50 |
+
elif "how many" in q_lower:
|
| 51 |
+
return self._handle_count(question)
|
| 52 |
+
else:
|
| 53 |
+
# General question handling
|
| 54 |
+
return self._handle_general(question)
|
| 55 |
+
|
| 56 |
+
def _handle_math(self, question: str) -> str:
|
| 57 |
+
"""Handle basic math questions"""
|
| 58 |
+
try:
|
| 59 |
+
# Extract numbers from the question
|
| 60 |
+
import re
|
| 61 |
+
numbers = re.findall(r'\b\d+\b', question)
|
| 62 |
+
if len(numbers) >= 2:
|
| 63 |
+
result = int(numbers[0]) + int(numbers[1])
|
| 64 |
+
return f"FINAL ANSWER: {result}"
|
| 65 |
+
except:
|
| 66 |
+
pass
|
| 67 |
+
return "FINAL ANSWER: unknown"
|
| 68 |
+
|
| 69 |
+
def _handle_capital(self, question: str) -> str:
|
| 70 |
+
"""Handle capital city questions"""
|
| 71 |
+
# Basic capital knowledge
|
| 72 |
+
capitals = {
|
| 73 |
+
"france": "Paris",
|
| 74 |
+
"germany": "Berlin",
|
| 75 |
+
"italy": "Rome",
|
| 76 |
+
"spain": "Madrid",
|
| 77 |
+
"uk": "London",
|
| 78 |
+
"united kingdom": "London",
|
| 79 |
+
"usa": "Washington DC",
|
| 80 |
+
"united states": "Washington DC",
|
| 81 |
+
"japan": "Tokyo",
|
| 82 |
+
"china": "Beijing",
|
| 83 |
+
"russia": "Moscow",
|
| 84 |
+
"canada": "Ottawa",
|
| 85 |
+
"australia": "Canberra",
|
| 86 |
+
"brazil": "Brasilia",
|
| 87 |
+
"india": "New Delhi"
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
q_lower = question.lower()
|
| 91 |
+
for country, capital in capitals.items():
|
| 92 |
+
if country in q_lower:
|
| 93 |
+
return f"FINAL ANSWER: {capital}"
|
| 94 |
+
|
| 95 |
+
return "FINAL ANSWER: unknown"
|
| 96 |
+
|
| 97 |
+
def _handle_largest(self, question: str) -> str:
|
| 98 |
+
"""Handle largest/biggest questions"""
|
| 99 |
+
q_lower = question.lower()
|
| 100 |
+
|
| 101 |
+
if "country" in q_lower:
|
| 102 |
+
return "FINAL ANSWER: Russia"
|
| 103 |
+
elif "ocean" in q_lower:
|
| 104 |
+
return "FINAL ANSWER: Pacific Ocean"
|
| 105 |
+
elif "continent" in q_lower:
|
| 106 |
+
return "FINAL ANSWER: Asia"
|
| 107 |
+
elif "planet" in q_lower:
|
| 108 |
+
return "FINAL ANSWER: Jupiter"
|
| 109 |
+
|
| 110 |
+
return "FINAL ANSWER: unknown"
|
| 111 |
+
|
| 112 |
+
def _handle_birth_year(self, question: str) -> str:
|
| 113 |
+
"""Handle birth year questions"""
|
| 114 |
+
# Basic historical figure birth years
|
| 115 |
+
birth_years = {
|
| 116 |
+
"einstein": "1879",
|
| 117 |
+
"newton": "1643",
|
| 118 |
+
"shakespeare": "1564",
|
| 119 |
+
"napoleon": "1769",
|
| 120 |
+
"lincoln": "1809",
|
| 121 |
+
"washington": "1732"
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
q_lower = question.lower()
|
| 125 |
+
for person, year in birth_years.items():
|
| 126 |
+
if person in q_lower:
|
| 127 |
+
return f"FINAL ANSWER: {year}"
|
| 128 |
+
|
| 129 |
+
return "FINAL ANSWER: unknown"
|
| 130 |
+
|
| 131 |
+
def _handle_count(self, question: str) -> str:
|
| 132 |
+
"""Handle counting questions"""
|
| 133 |
+
q_lower = question.lower()
|
| 134 |
+
|
| 135 |
+
if "continents" in q_lower:
|
| 136 |
+
return "FINAL ANSWER: 7"
|
| 137 |
+
elif "planets" in q_lower:
|
| 138 |
+
return "FINAL ANSWER: 8"
|
| 139 |
+
elif "states" in q_lower and "usa" in q_lower:
|
| 140 |
+
return "FINAL ANSWER: 50"
|
| 141 |
+
|
| 142 |
+
return "FINAL ANSWER: unknown"
|
| 143 |
+
|
| 144 |
+
def _handle_general(self, question: str) -> str:
|
| 145 |
+
"""Handle general questions with basic knowledge"""
|
| 146 |
+
q_lower = question.lower()
|
| 147 |
+
|
| 148 |
+
# Add more general knowledge patterns here
|
| 149 |
+
if "earth" in q_lower and "round" in q_lower:
|
| 150 |
+
return "FINAL ANSWER: yes"
|
| 151 |
+
elif "water" in q_lower and "freezing" in q_lower:
|
| 152 |
+
return "FINAL ANSWER: 0"
|
| 153 |
+
|
| 154 |
+
return "FINAL ANSWER: unknown"
|
| 155 |
|
| 156 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 157 |
"""
|