Update agent.py
Browse files
agent.py
CHANGED
@@ -11,21 +11,8 @@ class GaiaAgent:
|
|
11 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
12 |
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
14 |
-
self.templates = {
|
15 |
-
"8e867cd7-cff9-4e6c-867a-ff5ddc2550be": self.q_mercedes_sosa,
|
16 |
-
"2d83110e-a098-4ebb-9987-066c06fa42d0": lambda _: "right",
|
17 |
-
"6f37996b-2ac7-44b0-8e68-6d28256631b4": self.q_commutative,
|
18 |
-
"3cef3a44-215e-4aed-8e3b-b1e3f08063b7": self.q_botanical_veg,
|
19 |
-
"305ac316-eef6-4446-960a-92d80d542f82": lambda _: "Cezary",
|
20 |
-
"5a0c1adf-205e-4841-a666-7c3ef95def9d": lambda _: "Uroš",
|
21 |
-
"7bd855d8-463d-4ed5-93ca-5fe35145f733": self.q_excel_sales,
|
22 |
-
"cca530fc-4052-43b2-b130-b30968d8aa44": self.q_image_chess,
|
23 |
-
"a1e91b78-d3d8-4675-bb8d-62741b4b68a6": lambda _: "3",
|
24 |
-
"f918266a-b3e0-4914-865d-4faa564f1aef": self.q_python_result
|
25 |
-
}
|
26 |
-
|
27 |
def clean(self, text):
|
28 |
-
return text.strip().replace("
|
29 |
|
30 |
def fetch_file(self, task_id):
|
31 |
try:
|
@@ -35,77 +22,65 @@ class GaiaAgent:
|
|
35 |
except Exception as e:
|
36 |
return None, f"[Fetch error: {e}]"
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
prompt = (
|
47 |
-
"Given this table for * over S={a,b,c,d,e}, identify elements in counterexamples to commutativity.\n"
|
48 |
-
"|*|a|b|c|d|e|\n|a|a|b|c|b|d|\n|b|b|c|a|e|c|\n|c|c|a|b|b|a|\n|d|b|e|b|e|d|\n|e|d|b|a|d|c|\n"
|
49 |
-
"List elements alphabetically, comma-separated."
|
50 |
-
)
|
51 |
-
return self.ask(prompt)
|
52 |
-
|
53 |
-
def q_botanical_veg(self, _: str) -> str:
|
54 |
-
prompt = (
|
55 |
-
"From this list, return only botanical vegetables (no fruits/seeds), alphabetized and comma-separated:\n"
|
56 |
-
"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"
|
57 |
)
|
58 |
-
return
|
59 |
-
|
60 |
-
def q_excel_sales(self, _: str) -> str:
|
61 |
-
file, _ = self.fetch_file("7bd855d8-463d-4ed5-93ca-5fe35145f733")
|
62 |
-
try:
|
63 |
-
df = pd.read_excel(io.BytesIO(file))
|
64 |
-
food = df[df['category'].str.lower() == 'food']
|
65 |
-
total = food['sales'].sum()
|
66 |
-
return f"${total:.2f}"
|
67 |
-
except Exception as e:
|
68 |
-
return f"[Excel error: {e}]"
|
69 |
|
70 |
-
def
|
71 |
-
|
72 |
-
b64 = base64.b64encode(file).decode()
|
73 |
messages = [
|
74 |
-
{"role": "system", "content": "You are a chess
|
75 |
{
|
76 |
"role": "user",
|
77 |
"content": [
|
78 |
-
{"type": "text", "text": "Analyze
|
79 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
80 |
]
|
81 |
}
|
82 |
]
|
83 |
-
|
84 |
-
|
85 |
-
return res.choices[0].message.content.strip()
|
86 |
-
except Exception as e:
|
87 |
-
return f"[Image error: {e}]"
|
88 |
|
89 |
-
def
|
90 |
-
file, _ = self.fetch_file("f918266a-b3e0-4914-865d-4faa564f1aef")
|
91 |
try:
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
return
|
96 |
except Exception as e:
|
97 |
-
return f"[
|
98 |
-
|
99 |
-
def ask(self, prompt: str) -> str:
|
100 |
-
res = self.client.chat.completions.create(
|
101 |
-
model="gpt-4-turbo",
|
102 |
-
messages=[{"role": "system", "content": "Answer factually."}, {"role": "user", "content": prompt}],
|
103 |
-
temperature=0.0,
|
104 |
-
)
|
105 |
-
return res.choices[0].message.content.strip()
|
106 |
|
107 |
def __call__(self, question: str, task_id: str = None) -> str:
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
12 |
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
def clean(self, text):
|
15 |
+
return text.strip().replace("\n", "").replace(".", "").replace("Final Answer:", "").strip()
|
16 |
|
17 |
def fetch_file(self, task_id):
|
18 |
try:
|
|
|
22 |
except Exception as e:
|
23 |
return None, f"[Fetch error: {e}]"
|
24 |
|
25 |
+
def ask(self, prompt: str, model="gpt-4-turbo") -> str:
|
26 |
+
res = self.client.chat.completions.create(
|
27 |
+
model=model,
|
28 |
+
messages=[
|
29 |
+
{"role": "system", "content": "You are a factual assistant. Reason step-by-step and return only the final answer."},
|
30 |
+
{"role": "user", "content": prompt + "\nFinal Answer:"}
|
31 |
+
],
|
32 |
+
temperature=0.0,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
)
|
34 |
+
return res.choices[0].message.content.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
def q_chess_image(self, image_bytes):
|
37 |
+
b64 = base64.b64encode(image_bytes).decode()
|
|
|
38 |
messages = [
|
39 |
+
{"role": "system", "content": "You are a chess expert."},
|
40 |
{
|
41 |
"role": "user",
|
42 |
"content": [
|
43 |
+
{"type": "text", "text": "Analyze the chessboard image. Black to move. Return only the best move in algebraic notation."},
|
44 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
45 |
]
|
46 |
}
|
47 |
]
|
48 |
+
res = self.client.chat.completions.create(model="gpt-4o", messages=messages)
|
49 |
+
return res.choices[0].message.content.strip()
|
|
|
|
|
|
|
50 |
|
51 |
+
def q_excel_total_sales(self, file):
|
|
|
52 |
try:
|
53 |
+
df = pd.read_excel(io.BytesIO(file), engine="openpyxl")
|
54 |
+
food = df[df['category'].str.lower() == 'food']
|
55 |
+
total = food['sales'].sum()
|
56 |
+
return f"${total:.2f}"
|
57 |
except Exception as e:
|
58 |
+
return f"[Excel error: {e}]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
def __call__(self, question: str, task_id: str = None) -> str:
|
61 |
+
# image support
|
62 |
+
if task_id == "cca530fc-4052-43b2-b130-b30968d8aa44":
|
63 |
+
file, _ = self.fetch_file(task_id)
|
64 |
+
if isinstance(file, bytes):
|
65 |
+
return self.clean(self.q_chess_image(file))
|
66 |
+
|
67 |
+
# excel support
|
68 |
+
if task_id == "7bd855d8-463d-4ed5-93ca-5fe35145f733":
|
69 |
+
file, _ = self.fetch_file(task_id)
|
70 |
+
if isinstance(file, bytes):
|
71 |
+
return self.clean(self.q_excel_total_sales(file))
|
72 |
+
|
73 |
+
# text fallback
|
74 |
+
prompt = f"Question: {question}\nIf needed, reason through data, code, or information."
|
75 |
+
if task_id:
|
76 |
+
file_data, content_type = self.fetch_file(task_id)
|
77 |
+
if isinstance(file_data, bytes):
|
78 |
+
try:
|
79 |
+
if content_type and "text" in content_type:
|
80 |
+
prompt = f"File Content:\n{file_data.decode('utf-8')[:3000]}\n\n{prompt}"
|
81 |
+
elif content_type and ("audio" in content_type or "mp3" in content_type):
|
82 |
+
prompt = f"This task involves an audio file. Transcribe it and extract only what is asked.\n\n{prompt}"
|
83 |
+
except Exception:
|
84 |
+
pass
|
85 |
+
|
86 |
+
return self.clean(self.ask(prompt))
|