Update agent.py
Browse files
agent.py
CHANGED
@@ -1,80 +1,45 @@
|
|
1 |
import os
|
2 |
-
import io
|
3 |
-
import base64
|
4 |
-
import requests
|
5 |
-
import pandas as pd
|
6 |
from openai import OpenAI
|
7 |
|
8 |
-
TEXT_ONLY_TASKS = {
|
9 |
-
"2d83110e-a098-4ebb-9987-066c06fa42d0", # reversed question
|
10 |
-
"4fc2f1ae-8625-45b5-ab34-ad4433bc21f8", # wikipedia FA
|
11 |
-
"6f37996b-2ac7-44b0-8e68-6d28256631b4", # commutative check
|
12 |
-
"3cef3a44-215e-4aed-8e3b-b1e3f08063b7", # grocery list - vegetables
|
13 |
-
"305ac316-eef6-4446-960a-92d80d542f82", # actor - Magda M
|
14 |
-
"cf106601-ab4f-4af9-b045-5295fe67b37d", # least athletes
|
15 |
-
"5a0c1adf-205e-4841-a666-7c3ef95def9d" # Malko Competition
|
16 |
-
}
|
17 |
-
|
18 |
-
CSV_TASKS = {
|
19 |
-
"7bd855d8-463d-4ed5-93ca-5fe35145f733" # Excel - food sales
|
20 |
-
}
|
21 |
-
|
22 |
class GaiaAgent:
|
23 |
def __init__(self):
|
24 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
25 |
-
self.api_url = "https://agents-course-unit4-scoring.hf.space"
|
26 |
self.instructions = (
|
27 |
-
"You are a
|
28 |
-
"
|
29 |
)
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
url = f"{self.api_url}/files/{task_id}"
|
34 |
-
r = requests.get(url, timeout=10)
|
35 |
-
r.raise_for_status()
|
36 |
-
return r.content, r.headers.get("Content-Type", "")
|
37 |
-
except Exception as e:
|
38 |
-
return None, f"[FILE ERROR: {e}]"
|
39 |
-
|
40 |
-
def handle_csv_sales(self, csv_bytes):
|
41 |
-
try:
|
42 |
-
df = pd.read_excel(io.BytesIO(csv_bytes)) if csv_bytes[:4] == b"PK\x03\x04" else pd.read_csv(io.StringIO(csv_bytes.decode()))
|
43 |
-
if 'category' not in df.columns or 'sales' not in df.columns:
|
44 |
-
return "[MISSING COLUMN]"
|
45 |
-
food_df = df[df['category'].str.lower() == 'food']
|
46 |
-
if food_df.empty:
|
47 |
-
return "[NO FOOD ITEMS FOUND]"
|
48 |
-
total = food_df['sales'].sum()
|
49 |
-
return f"${total:.2f}"
|
50 |
-
except Exception as e:
|
51 |
-
return f"[CSV ERROR: {e}]"
|
52 |
|
53 |
def __call__(self, question: str, task_id: str = None) -> str:
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
# 3. Text questions with high confidence
|
69 |
try:
|
70 |
response = self.client.chat.completions.create(
|
71 |
model="gpt-4-turbo",
|
72 |
messages=[
|
73 |
{"role": "system", "content": self.instructions},
|
74 |
-
{"role": "user", "content":
|
75 |
],
|
76 |
temperature=0.0
|
77 |
)
|
78 |
return response.choices[0].message.content.strip()
|
79 |
except Exception as e:
|
80 |
-
return f"[
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
2 |
from openai import OpenAI
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
class GaiaAgent:
|
5 |
def __init__(self):
|
6 |
self.client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
7 |
self.instructions = (
|
8 |
+
"You are a research assistant solving GAIA benchmark questions using 2022 English Wikipedia knowledge.\n"
|
9 |
+
"For each question, reason step-by-step and only return the final answer in exact format."
|
10 |
)
|
11 |
+
self.task_templates = {
|
12 |
+
"8e867cd7-cff9-4e6c-867a-ff5ddc2550be": self.q_mercedes_sosa_albums
|
13 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
def __call__(self, question: str, task_id: str = None) -> str:
|
16 |
+
if task_id in self.task_templates:
|
17 |
+
return self.task_templates[task_id](question)
|
18 |
+
else:
|
19 |
+
return "[SKIPPED: Task not yet implemented in Agent V9]"
|
20 |
+
|
21 |
+
def q_mercedes_sosa_albums(self, question: str) -> str:
|
22 |
+
prompt = (
|
23 |
+
"You are a research assistant using 2022 English Wikipedia knowledge.\n"
|
24 |
+
"\nQUESTION:\n"
|
25 |
+
"How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)?\n"
|
26 |
+
"\nFollow this step-by-step reasoning chain:\n"
|
27 |
+
"Step 1: Identify all studio albums by Mercedes Sosa.\n"
|
28 |
+
"Step 2: Filter the albums published between 2000 and 2009 inclusive.\n"
|
29 |
+
"Step 3: Count them.\n"
|
30 |
+
"Step 4: Return only the count as a number (no text, no explanation).\n"
|
31 |
+
"\nANSWER:"
|
32 |
+
)
|
33 |
|
|
|
34 |
try:
|
35 |
response = self.client.chat.completions.create(
|
36 |
model="gpt-4-turbo",
|
37 |
messages=[
|
38 |
{"role": "system", "content": self.instructions},
|
39 |
+
{"role": "user", "content": prompt}
|
40 |
],
|
41 |
temperature=0.0
|
42 |
)
|
43 |
return response.choices[0].message.content.strip()
|
44 |
except Exception as e:
|
45 |
+
return f"[AGENT ERROR: {e}]"
|