Update agent.py
Browse files
agent.py
CHANGED
@@ -1,114 +1,58 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
from openai import OpenAI
|
4 |
|
5 |
class GaiaAgent:
|
6 |
def __init__(self):
|
7 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
8 |
self.instructions = (
|
9 |
-
"You are a
|
10 |
-
"
|
|
|
11 |
)
|
12 |
-
self.
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
"
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
else:
|
28 |
-
return "[
|
29 |
-
|
30 |
-
|
31 |
-
def clean_output(self, text: str) -> str:
|
32 |
-
return text.strip().replace("\n", "").replace(".", "").strip()
|
33 |
|
34 |
-
def
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
def query(self, prompt: str) -> str:
|
46 |
response = self.client.chat.completions.create(
|
47 |
model="gpt-4-turbo",
|
48 |
messages=[
|
49 |
{"role": "system", "content": self.instructions},
|
50 |
{"role": "user", "content": prompt}
|
51 |
],
|
52 |
-
temperature=0.0
|
53 |
)
|
54 |
-
return response.choices[0].message.content
|
55 |
-
|
56 |
-
def q_mercedes_sosa_albums(self, _: str) -> str:
|
57 |
-
return self.query("""
|
58 |
-
QUESTION: How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?
|
59 |
-
Scratchpad:
|
60 |
-
Step 1: Identify studio albums.
|
61 |
-
Step 2: Filter 2000–2009.
|
62 |
-
Step 3: Count them.
|
63 |
-
Answer as number only.
|
64 |
-
""")
|
65 |
-
|
66 |
-
def q_reversed_text(self, _: str) -> str:
|
67 |
-
return self.query("""
|
68 |
-
QUESTION: .rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI
|
69 |
-
Scratchpad:
|
70 |
-
Step 1: Reverse the sentence.
|
71 |
-
Step 2: Understand it.
|
72 |
-
Step 3: The opposite of "left" is "right".
|
73 |
-
Final Answer:
|
74 |
-
""")
|
75 |
-
|
76 |
-
def q_botanical_vegetables(self, _: str) -> str:
|
77 |
-
return self.query("""
|
78 |
-
QUESTION: Classify these items botanically and return only vegetables: milk, eggs, flour, whole bean coffee, Oreos, sweet potatoes, fresh basil, plums, green beans, rice, corn, bell pepper, whole allspice, acorns, broccoli, celery, zucchini, lettuce, peanuts.
|
79 |
-
Scratchpad:
|
80 |
-
Step 1: Identify botanical vegetables.
|
81 |
-
Step 2: Exclude fruits/seeds.
|
82 |
-
Step 3: Alphabetize.
|
83 |
-
Final Answer: comma-separated list.
|
84 |
-
""")
|
85 |
-
|
86 |
-
def q_commutative(self, _: str) -> str:
|
87 |
-
return self.query("""
|
88 |
-
QUESTION: Given the operation table over S = {a,b,c,d,e}, return elements involved in counterexamples that prove * is not commutative. Alphabetical order, comma-separated.
|
89 |
-
Scratchpad:
|
90 |
-
Step 1: Test all a*b vs b*a.
|
91 |
-
Step 2: Record unequal pairs.
|
92 |
-
Step 3: Collect elements.
|
93 |
-
Final Answer:
|
94 |
-
""")
|
95 |
-
|
96 |
-
def q_ray_polish(self, _: str) -> str:
|
97 |
-
return self.query("""
|
98 |
-
QUESTION: Who did the actor who played Ray in the Polish version of Everybody Loves Raymond play in Magda M.? Give only the first name.
|
99 |
-
Scratchpad:
|
100 |
-
Step 1: Find actor.
|
101 |
-
Step 2: Cross-reference roles.
|
102 |
-
Step 3: Return first name only.
|
103 |
-
Final Answer:
|
104 |
-
""")
|
105 |
-
|
106 |
-
def q_malko_name(self, _: str) -> str:
|
107 |
-
return self.query("""
|
108 |
-
QUESTION: First name of the only Malko Competition recipient from the 20th century (after 1977) whose nationality was from a country that no longer exists?
|
109 |
-
Scratchpad:
|
110 |
-
Step 1: Get winners list.
|
111 |
-
Step 2: Check nationality.
|
112 |
-
Step 3: Return first name.
|
113 |
-
Final Answer:
|
114 |
-
""")
|
|
|
1 |
import os
|
2 |
+
import requests
|
3 |
from openai import OpenAI
|
4 |
|
5 |
class GaiaAgent:
|
6 |
def __init__(self):
|
7 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
8 |
self.instructions = (
|
9 |
+
"You are a highly skilled and concise research assistant solving GAIA benchmark questions.\n"
|
10 |
+
"Analyze attached files, extract relevant information, reason step-by-step internally,\n"
|
11 |
+
"and return only the final factual answer in the correct format. Avoid explanations."
|
12 |
)
|
13 |
+
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
14 |
+
|
15 |
+
def fetch_file_content(self, task_id: str) -> str:
|
16 |
+
try:
|
17 |
+
url = f"{self.api_url}/files/{task_id}"
|
18 |
+
response = requests.get(url, timeout=10)
|
19 |
+
response.raise_for_status()
|
20 |
+
content_type = response.headers.get("Content-Type", "")
|
21 |
+
if "text" in content_type or "csv" in content_type or "json" in content_type:
|
22 |
+
return response.text[:3000] # Truncate to 3000 chars
|
23 |
+
elif "application/pdf" in content_type:
|
24 |
+
return "[PDF detected. Summarize manually if needed.]"
|
25 |
+
elif "image" in content_type:
|
26 |
+
return "[Image detected. Describe the image if needed.]"
|
27 |
+
elif "audio" in content_type:
|
28 |
+
return "[Audio detected. Provide transcription if needed.]"
|
29 |
else:
|
30 |
+
return f"[Unsupported file type: {content_type}]"
|
31 |
+
except Exception as e:
|
32 |
+
return f"[Error fetching file: {e}]"
|
|
|
|
|
33 |
|
34 |
+
def __call__(self, question: str, task_id: str = None) -> str:
|
35 |
+
file_context = ""
|
36 |
+
if task_id:
|
37 |
+
file_context = self.fetch_file_content(task_id)
|
38 |
+
if file_context:
|
39 |
+
file_context = f"Attached File Context:\n{file_context}\n"
|
40 |
+
|
41 |
+
# Add scratchpad-like structure
|
42 |
+
prompt = (
|
43 |
+
f"{self.instructions}\n\n"
|
44 |
+
f"{file_context}"
|
45 |
+
f"Question: {question}\n"
|
46 |
+
f"Think step-by-step to extract relevant facts and solve the task.\n"
|
47 |
+
f"Final Answer (no explanation, just the answer):"
|
48 |
+
)
|
49 |
|
|
|
50 |
response = self.client.chat.completions.create(
|
51 |
model="gpt-4-turbo",
|
52 |
messages=[
|
53 |
{"role": "system", "content": self.instructions},
|
54 |
{"role": "user", "content": prompt}
|
55 |
],
|
56 |
+
temperature=0.0,
|
57 |
)
|
58 |
+
return response.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|