Update agent.py
Browse files
agent.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
import re
|
3 |
import io
|
@@ -34,6 +35,13 @@ class GaiaAgent:
|
|
34 |
except:
|
35 |
return "[ERROR: ask failed]"
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def handle_file(self, content, ctype, question):
|
38 |
try:
|
39 |
if "image" in ctype:
|
@@ -41,7 +49,7 @@ class GaiaAgent:
|
|
41 |
result = self.client.chat.completions.create(
|
42 |
model="gpt-4o",
|
43 |
messages=[
|
44 |
-
{"role": "system", "content": "You're a chess assistant.
|
45 |
{"role": "user", "content": [
|
46 |
{"type": "text", "text": question},
|
47 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
@@ -57,8 +65,10 @@ class GaiaAgent:
|
|
57 |
if "excel" in ctype:
|
58 |
df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
|
59 |
df.columns = [c.lower().strip() for c in df.columns]
|
60 |
-
df = df[
|
61 |
df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
|
|
|
|
|
62 |
return f"${df['sales'].sum():.2f}"
|
63 |
return content.decode("utf-8", errors="ignore")[:3000]
|
64 |
except:
|
@@ -87,6 +97,9 @@ class GaiaAgent:
|
|
87 |
if "award number" in q:
|
88 |
m = re.search(r"80NSSC[0-9A-Z]+", raw)
|
89 |
return m.group(0) if m else raw
|
|
|
|
|
|
|
90 |
if "first name" in q:
|
91 |
return raw.split()[0]
|
92 |
try:
|
@@ -97,16 +110,25 @@ class GaiaAgent:
|
|
97 |
|
98 |
def __call__(self, question, task_id=None):
|
99 |
try:
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
104 |
{context}
|
105 |
|
106 |
Question:
|
107 |
{question}
|
108 |
Answer:"
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
except Exception as e:
|
112 |
return f"[AGENT ERROR: {e}]"
|
|
|
1 |
+
# Agent V45 — V26 + fallback + YouTube + Excel + dynamic formatting
|
2 |
import os
|
3 |
import re
|
4 |
import io
|
|
|
35 |
except:
|
36 |
return "[ERROR: ask failed]"
|
37 |
|
38 |
+
def search_context(self, query):
|
39 |
+
try:
|
40 |
+
result = self.search_tool.run(query)
|
41 |
+
return result[:2000] if result else "[NO RESULT]"
|
42 |
+
except:
|
43 |
+
return "[WEB ERROR]"
|
44 |
+
|
45 |
def handle_file(self, content, ctype, question):
|
46 |
try:
|
47 |
if "image" in ctype:
|
|
|
49 |
result = self.client.chat.completions.create(
|
50 |
model="gpt-4o",
|
51 |
messages=[
|
52 |
+
{"role": "system", "content": "You're a chess assistant. Give the best move in algebraic notation (e.g., Qd1#)."},
|
53 |
{"role": "user", "content": [
|
54 |
{"type": "text", "text": question},
|
55 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
|
|
65 |
if "excel" in ctype:
|
66 |
df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
|
67 |
df.columns = [c.lower().strip() for c in df.columns]
|
68 |
+
df = df.dropna(subset=['category', 'sales'], errors='ignore')
|
69 |
df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
|
70 |
+
if 'category' in df.columns:
|
71 |
+
df = df[df['category'].str.lower() == 'food']
|
72 |
return f"${df['sales'].sum():.2f}"
|
73 |
return content.decode("utf-8", errors="ignore")[:3000]
|
74 |
except:
|
|
|
97 |
if "award number" in q:
|
98 |
m = re.search(r"80NSSC[0-9A-Z]+", raw)
|
99 |
return m.group(0) if m else raw
|
100 |
+
if "ioc" in q:
|
101 |
+
m = re.search(r"\b[A-Z]{3}\b", raw)
|
102 |
+
return m.group(0) if m else raw
|
103 |
if "first name" in q:
|
104 |
return raw.split()[0]
|
105 |
try:
|
|
|
110 |
|
111 |
def __call__(self, question, task_id=None):
|
112 |
try:
|
113 |
+
file_content, ctype = self.fetch_file(task_id) if task_id else (None, None)
|
114 |
+
if file_content:
|
115 |
+
context = self.handle_file(file_content, ctype, question)
|
116 |
+
else:
|
117 |
+
context = self.search_context(question)
|
118 |
+
prompt = f"Use this context to answer the question:
|
119 |
{context}
|
120 |
|
121 |
Question:
|
122 |
{question}
|
123 |
Answer:"
|
124 |
+
answer = self.ask(prompt)
|
125 |
+
if not answer or "[ERROR" in answer:
|
126 |
+
fallback = self.search_context(question)
|
127 |
+
retry_prompt = f"Use this context to answer:
|
128 |
+
{fallback}
|
129 |
+
|
130 |
+
{question}"
|
131 |
+
answer = self.ask(retry_prompt)
|
132 |
+
return self.format_answer(answer, question)
|
133 |
except Exception as e:
|
134 |
return f"[AGENT ERROR: {e}]"
|