Update agent.py
Browse files
agent.py
CHANGED
@@ -26,13 +26,28 @@ class GaiaAgent:
|
|
26 |
res = self.client.chat.completions.create(
|
27 |
model=model,
|
28 |
messages=[
|
29 |
-
{"role": "system", "content": "You are a precise assistant. Think step by step and return only the
|
30 |
-
{"role": "user", "content": prompt + "\n\
|
31 |
],
|
32 |
temperature=0.0,
|
33 |
)
|
34 |
return self.clean(res.choices[0].message.content)
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
def q_excel_sales(self, file: bytes, question: str) -> str:
|
37 |
try:
|
38 |
df = pd.read_excel(io.BytesIO(file), engine="openpyxl")
|
@@ -54,27 +69,34 @@ class GaiaAgent:
|
|
54 |
prompt = f"Transcript: {content}\n\nQuestion: {question}"
|
55 |
return self.ask(prompt)
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
def __call__(self, question: str, task_id: str = None) -> str:
|
58 |
-
|
59 |
-
if task_id:
|
60 |
-
file, content_type = self.fetch_file(task_id)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
"1f975693-876d-457b-a649-393859e79bf3"
|
68 |
-
] and isinstance(file, bytes):
|
69 |
-
return self.q_audio_transcribe(file, question)
|
70 |
|
71 |
-
if isinstance(file, bytes) and content_type
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
return self.
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
return self.ask(
|
|
|
26 |
res = self.client.chat.completions.create(
|
27 |
model=model,
|
28 |
messages=[
|
29 |
+
{"role": "system", "content": "You are a precise assistant. Think step by step and return only the final answer in the correct format."},
|
30 |
+
{"role": "user", "content": prompt + "\n\nFinal Answer:"}
|
31 |
],
|
32 |
temperature=0.0,
|
33 |
)
|
34 |
return self.clean(res.choices[0].message.content)
|
35 |
|
36 |
+
def ask_image(self, image_bytes: bytes, question: str) -> str:
|
37 |
+
b64 = base64.b64encode(image_bytes).decode()
|
38 |
+
messages = [
|
39 |
+
{"role": "system", "content": "You are a visual assistant. Only return the final answer to the question."},
|
40 |
+
{
|
41 |
+
"role": "user",
|
42 |
+
"content": [
|
43 |
+
{"type": "text", "text": question},
|
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 self.clean(res.choices[0].message.content)
|
50 |
+
|
51 |
def q_excel_sales(self, file: bytes, question: str) -> str:
|
52 |
try:
|
53 |
df = pd.read_excel(io.BytesIO(file), engine="openpyxl")
|
|
|
69 |
prompt = f"Transcript: {content}\n\nQuestion: {question}"
|
70 |
return self.ask(prompt)
|
71 |
|
72 |
+
def extract_youtube_hint(self, question: str) -> str:
|
73 |
+
match = re.search(r"https://www\.youtube\.com/watch\?v=([\w-]+)", question)
|
74 |
+
if match:
|
75 |
+
return f"This task is about a YouTube video (ID: {match.group(1)}). Assume the video visually or audibly answers the question."
|
76 |
+
return ""
|
77 |
+
|
78 |
def __call__(self, question: str, task_id: str = None) -> str:
|
79 |
+
context = ""
|
|
|
|
|
80 |
|
81 |
+
if "youtube.com/watch" in question:
|
82 |
+
context += self.extract_youtube_hint(question) + "\n"
|
83 |
|
84 |
+
if task_id:
|
85 |
+
file, content_type = self.fetch_file(task_id)
|
|
|
|
|
|
|
86 |
|
87 |
+
if isinstance(file, bytes) and content_type:
|
88 |
+
if "image" in content_type:
|
89 |
+
return self.ask_image(file, question)
|
90 |
+
if "audio" in content_type or task_id.endswith(".mp3"):
|
91 |
+
return self.q_audio_transcribe(file, question)
|
92 |
+
if "spreadsheet" in content_type or content_type.endswith("excel") or content_type.endswith("xlsx"):
|
93 |
+
return self.q_excel_sales(file, question)
|
94 |
+
if "text" in content_type:
|
95 |
+
try:
|
96 |
+
text = file.decode("utf-8", errors="ignore")[:3000]
|
97 |
+
context += f"File Content:\n{text}\n"
|
98 |
+
except Exception:
|
99 |
+
pass
|
100 |
|
101 |
+
prompt = f"{context}\nQuestion: {question}"
|
102 |
+
return self.ask(prompt)
|