Update app.py
Browse files
app.py
CHANGED
|
@@ -18,22 +18,24 @@ from openai import OpenAI
|
|
| 18 |
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
-
print("GAIA
|
| 22 |
self.client = OpenAI()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def clean_answer(self, text):
|
| 25 |
-
"""
|
| 26 |
-
GAIA scoring requires exact answers.
|
| 27 |
-
This removes explanations and keeps only the final value.
|
| 28 |
-
"""
|
| 29 |
|
| 30 |
text = text.strip()
|
| 31 |
|
| 32 |
-
# remove phrases
|
| 33 |
text = re.sub(r"(?i)final answer[:\- ]*", "", text)
|
| 34 |
text = re.sub(r"(?i)answer[:\- ]*", "", text)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
text = text.split("\n")[-1]
|
| 38 |
|
| 39 |
return text.strip()
|
|
@@ -41,14 +43,13 @@ class BasicAgent:
|
|
| 41 |
def ask_llm(self, question):
|
| 42 |
|
| 43 |
prompt = f"""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
Think step by step internally but output ONLY the final answer.
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
- No explanation
|
| 50 |
-
- No sentences
|
| 51 |
-
- Only the final value
|
| 52 |
|
| 53 |
Question:
|
| 54 |
{question}
|
|
@@ -56,24 +57,46 @@ Question:
|
|
| 56 |
|
| 57 |
response = self.client.chat.completions.create(
|
| 58 |
model="gpt-4o-mini",
|
|
|
|
| 59 |
messages=[
|
| 60 |
-
{"role": "system", "content": "You are a precise
|
| 61 |
{"role": "user", "content": prompt}
|
| 62 |
-
]
|
| 63 |
-
temperature=0
|
| 64 |
)
|
| 65 |
|
| 66 |
return response.choices[0].message.content.strip()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def __call__(self, question: str) -> str:
|
| 69 |
|
| 70 |
print("Question:", question)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
raw_answer = self.ask_llm(question)
|
| 73 |
|
| 74 |
final_answer = self.clean_answer(raw_answer)
|
| 75 |
|
| 76 |
-
print("Final
|
| 77 |
|
| 78 |
return final_answer
|
| 79 |
|
|
|
|
| 18 |
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
+
print("Advanced GAIA Agent initialized")
|
| 22 |
self.client = OpenAI()
|
| 23 |
|
| 24 |
+
# calculator tool
|
| 25 |
+
def calculate(self, expression):
|
| 26 |
+
try:
|
| 27 |
+
return str(eval(expression))
|
| 28 |
+
except:
|
| 29 |
+
return None
|
| 30 |
+
|
| 31 |
def clean_answer(self, text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
text = text.strip()
|
| 34 |
|
|
|
|
| 35 |
text = re.sub(r"(?i)final answer[:\- ]*", "", text)
|
| 36 |
text = re.sub(r"(?i)answer[:\- ]*", "", text)
|
| 37 |
|
| 38 |
+
# take last line
|
| 39 |
text = text.split("\n")[-1]
|
| 40 |
|
| 41 |
return text.strip()
|
|
|
|
| 43 |
def ask_llm(self, question):
|
| 44 |
|
| 45 |
prompt = f"""
|
| 46 |
+
You are solving a GAIA benchmark problem.
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
Instructions:
|
| 49 |
+
- Think step by step
|
| 50 |
+
- Use math reasoning when necessary
|
| 51 |
+
- Return ONLY the final answer
|
| 52 |
- No explanation
|
|
|
|
|
|
|
| 53 |
|
| 54 |
Question:
|
| 55 |
{question}
|
|
|
|
| 57 |
|
| 58 |
response = self.client.chat.completions.create(
|
| 59 |
model="gpt-4o-mini",
|
| 60 |
+
temperature=0,
|
| 61 |
messages=[
|
| 62 |
+
{"role": "system", "content": "You are a precise reasoning agent."},
|
| 63 |
{"role": "user", "content": prompt}
|
| 64 |
+
]
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
return response.choices[0].message.content.strip()
|
| 68 |
|
| 69 |
+
def try_math(self, question):
|
| 70 |
+
|
| 71 |
+
# detect simple math expressions
|
| 72 |
+
matches = re.findall(r"[\d\.\+\-\*\/\(\) ]+", question)
|
| 73 |
+
|
| 74 |
+
for m in matches:
|
| 75 |
+
m = m.strip()
|
| 76 |
+
if len(m) > 3:
|
| 77 |
+
result = self.calculate(m)
|
| 78 |
+
if result:
|
| 79 |
+
return result
|
| 80 |
+
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
def __call__(self, question: str) -> str:
|
| 84 |
|
| 85 |
print("Question:", question)
|
| 86 |
|
| 87 |
+
# try math tool first
|
| 88 |
+
math_result = self.try_math(question)
|
| 89 |
+
|
| 90 |
+
if math_result:
|
| 91 |
+
print("Calculator used:", math_result)
|
| 92 |
+
return math_result
|
| 93 |
+
|
| 94 |
+
# otherwise ask LLM
|
| 95 |
raw_answer = self.ask_llm(question)
|
| 96 |
|
| 97 |
final_answer = self.clean_answer(raw_answer)
|
| 98 |
|
| 99 |
+
print("Final answer:", final_answer)
|
| 100 |
|
| 101 |
return final_answer
|
| 102 |
|