dawid-lorek commited on
Commit
8dcca97
·
verified ·
1 Parent(s): b6dd3b0

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +40 -96
agent.py CHANGED
@@ -1,114 +1,58 @@
1
  import os
2
- import re
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 high-precision research assistant solving GAIA benchmark questions.\n"
10
- "Answer with exact values only. Use scratchpad reasoning internally, but only return the final answer."
 
11
  )
12
- self.task_templates = {
13
- "8e867cd7-cff9-4e6c-867a-ff5ddc2550be": self.q_mercedes_sosa_albums,
14
- "2d83110e-a098-4ebb-9987-066c06fa42d0": self.q_reversed_text,
15
- "3cef3a44-215e-4aed-8e3b-b1e3f08063b7": self.q_botanical_vegetables,
16
- "6f37996b-2ac7-44b0-8e68-6d28256631b4": self.q_commutative,
17
- "305ac316-eef6-4446-960a-92d80d542f82": self.q_ray_polish,
18
- "5a0c1adf-205e-4841-a666-7c3ef95def9d": self.q_malko_name
19
- }
20
-
21
- def __call__(self, question: str, task_id: str = None) -> str:
22
- if task_id in self.task_templates:
23
- result = self.task_templates[task_id](question)
24
- cleaned = self.clean_output(result)
25
- if self.validate(task_id, cleaned):
26
- return cleaned
 
27
  else:
28
- return "[SKIPPED: Invalid format]"
29
- return "[SKIPPED: Task not implemented]"
30
-
31
- def clean_output(self, text: str) -> str:
32
- return text.strip().replace("\n", "").replace(".", "").strip()
33
 
34
- def validate(self, task_id, text) -> bool:
35
- validators = {
36
- "8e867cd7-cff9-4e6c-867a-ff5ddc2550be": lambda t: t.isdigit(),
37
- "2d83110e-a098-4ebb-9987-066c06fa42d0": lambda t: t.lower() == "right",
38
- "3cef3a44-215e-4aed-8e3b-b1e3f08063b7": lambda t: all(x.isalpha() for x in t.replace(",", "").split()),
39
- "6f37996b-2ac7-44b0-8e68-6d28256631b4": lambda t: re.match(r"^[a-e](,[a-e])*$", t),
40
- "305ac316-eef6-4446-960a-92d80d542f82": lambda t: t.isalpha(),
41
- "5a0c1adf-205e-4841-a666-7c3ef95def9d": lambda t: t.isalpha()
42
- }
43
- return validators.get(task_id, lambda _: False)(text)
 
 
 
 
 
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()