Update agent.py
Browse files
agent.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
#
|
2 |
import os
|
3 |
import re
|
4 |
import io
|
@@ -18,50 +18,39 @@ class GaiaAgent:
|
|
18 |
def fetch_file(self, task_id):
|
19 |
try:
|
20 |
url = f"{self.api_url}/files/{task_id}"
|
21 |
-
|
22 |
-
|
23 |
-
return
|
24 |
except:
|
25 |
return None, None
|
26 |
|
27 |
-
def
|
28 |
try:
|
29 |
-
|
30 |
model="gpt-4-turbo",
|
31 |
-
messages=[{"role": "user", "content":
|
32 |
temperature=0,
|
33 |
-
timeout=
|
34 |
)
|
35 |
-
return
|
36 |
except:
|
37 |
-
return "
|
38 |
|
39 |
-
def
|
40 |
try:
|
41 |
-
|
42 |
-
|
43 |
-
model="gpt-4-turbo",
|
44 |
-
messages=[
|
45 |
-
{"role": "system", "content": f"Use only this data:\n{web_context}"},
|
46 |
-
{"role": "user", "content": f"Follow this plan:\n{steps}\n\nAnswer this question concisely:\n{question}"}
|
47 |
-
],
|
48 |
-
temperature=0,
|
49 |
-
timeout=30
|
50 |
-
)
|
51 |
-
return response.choices[0].message.content.strip()
|
52 |
except:
|
53 |
-
return "[ERROR
|
54 |
|
55 |
def handle_file(self, content, ctype, question):
|
56 |
-
if not content:
|
57 |
-
return "[NO FILE DATA]"
|
58 |
try:
|
59 |
if "image" in ctype:
|
60 |
b64 = base64.b64encode(content).decode("utf-8")
|
61 |
result = self.client.chat.completions.create(
|
62 |
model="gpt-4o",
|
63 |
messages=[
|
64 |
-
{"role": "system", "content": "You're a chess assistant.
|
65 |
{"role": "user", "content": [
|
66 |
{"type": "text", "text": question},
|
67 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
@@ -73,13 +62,13 @@ class GaiaAgent:
|
|
73 |
with open("/tmp/audio.mp3", "wb") as f:
|
74 |
f.write(content)
|
75 |
result = self.client.audio.transcriptions.create(model="whisper-1", file=open("/tmp/audio.mp3", "rb"))
|
76 |
-
return result.text
|
77 |
if "excel" in ctype:
|
78 |
df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
|
79 |
-
df.columns = [c.
|
80 |
if 'category' in df.columns and 'sales' in df.columns:
|
81 |
df = df.dropna(subset=['category', 'sales'])
|
82 |
-
df = df[df['category'].str.lower()
|
83 |
df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
|
84 |
return f"${df['sales'].sum():.2f}"
|
85 |
return "[NO FOOD SALES DATA]"
|
@@ -87,48 +76,79 @@ class GaiaAgent:
|
|
87 |
except Exception as e:
|
88 |
return f"[FILE ERROR: {e}]"
|
89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
def extract_ingredients(self, text):
|
91 |
try:
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
return ", ".join(unique[:15])
|
97 |
except:
|
98 |
-
return text[:
|
99 |
|
100 |
-
def format_answer(self,
|
101 |
-
if not raw:
|
102 |
-
return "[NO ANSWER]"
|
103 |
-
raw = raw.strip().strip("\"'")
|
104 |
q = question.lower()
|
105 |
-
|
|
|
|
|
|
|
106 |
return self.extract_ingredients(raw)
|
107 |
if "algebraic notation" in q:
|
108 |
-
|
109 |
-
return
|
110 |
-
if "award number" in q:
|
111 |
-
match = re.search(r"80NSSC[0-9A-Z]+", raw)
|
112 |
-
return match.group(0) if match else raw
|
113 |
if "usd" in q:
|
114 |
m = re.search(r"\$?\d+(\.\d{2})", raw)
|
115 |
return f"${m.group()}" if m else "$0.00"
|
|
|
|
|
|
|
116 |
if "first name" in q:
|
117 |
-
return raw.split()[0]
|
118 |
try:
|
119 |
return str(w2n.word_to_num(raw))
|
120 |
except:
|
121 |
m = re.search(r"\d+", raw)
|
122 |
return m.group(0) if m else raw
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
def __call__(self, question, task_id=None):
|
125 |
try:
|
126 |
-
|
127 |
-
if
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
|
|
|
|
133 |
except Exception as e:
|
134 |
return f"[AGENT ERROR: {e}]"
|
|
|
1 |
+
# agent_v43.py — Najlepsze cechy z V18–V34: precyzja, retry fallback, stabilność
|
2 |
import os
|
3 |
import re
|
4 |
import io
|
|
|
18 |
def fetch_file(self, task_id):
|
19 |
try:
|
20 |
url = f"{self.api_url}/files/{task_id}"
|
21 |
+
r = requests.get(url, timeout=10)
|
22 |
+
r.raise_for_status()
|
23 |
+
return r.content, r.headers.get("Content-Type", "")
|
24 |
except:
|
25 |
return None, None
|
26 |
|
27 |
+
def ask(self, prompt):
|
28 |
try:
|
29 |
+
r = self.client.chat.completions.create(
|
30 |
model="gpt-4-turbo",
|
31 |
+
messages=[{"role": "user", "content": prompt}],
|
32 |
temperature=0,
|
33 |
+
timeout=30
|
34 |
)
|
35 |
+
return r.choices[0].message.content.strip()
|
36 |
except:
|
37 |
+
return "[ERROR: ask failed]"
|
38 |
|
39 |
+
def search_context(self, query):
|
40 |
try:
|
41 |
+
result = self.search_tool.run(query)
|
42 |
+
return result[:2000] if result else "[NO WEB RESULT]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
except:
|
44 |
+
return "[WEB ERROR]"
|
45 |
|
46 |
def handle_file(self, content, ctype, question):
|
|
|
|
|
47 |
try:
|
48 |
if "image" in ctype:
|
49 |
b64 = base64.b64encode(content).decode("utf-8")
|
50 |
result = self.client.chat.completions.create(
|
51 |
model="gpt-4o",
|
52 |
messages=[
|
53 |
+
{"role": "system", "content": "You're a chess assistant. Reply only with the best move in algebraic notation (e.g., Qd1#)."},
|
54 |
{"role": "user", "content": [
|
55 |
{"type": "text", "text": question},
|
56 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
|
|
62 |
with open("/tmp/audio.mp3", "wb") as f:
|
63 |
f.write(content)
|
64 |
result = self.client.audio.transcriptions.create(model="whisper-1", file=open("/tmp/audio.mp3", "rb"))
|
65 |
+
return result.text
|
66 |
if "excel" in ctype:
|
67 |
df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
|
68 |
+
df.columns = [c.lower().strip() for c in df.columns]
|
69 |
if 'category' in df.columns and 'sales' in df.columns:
|
70 |
df = df.dropna(subset=['category', 'sales'])
|
71 |
+
df = df[df['category'].str.lower() == 'food']
|
72 |
df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
|
73 |
return f"${df['sales'].sum():.2f}"
|
74 |
return "[NO FOOD SALES DATA]"
|
|
|
76 |
except Exception as e:
|
77 |
return f"[FILE ERROR: {e}]"
|
78 |
|
79 |
+
def extract_commutativity_set(self, table_txt):
|
80 |
+
try:
|
81 |
+
lines = table_txt.splitlines()
|
82 |
+
S, table = [], {}
|
83 |
+
for l in lines:
|
84 |
+
if l.startswith("|*"):
|
85 |
+
S = l.strip().split("|")[2:]
|
86 |
+
elif l.startswith("|"):
|
87 |
+
parts = l.strip().split("|")[1:-1]
|
88 |
+
table[parts[0]] = parts[1:]
|
89 |
+
fail = set()
|
90 |
+
for x in S:
|
91 |
+
for y in S:
|
92 |
+
if table[x][S.index(y)] != table[y][S.index(x)]:
|
93 |
+
fail |= {x, y}
|
94 |
+
return ", ".join(sorted(fail))
|
95 |
+
except:
|
96 |
+
return "[COMMUTATIVE ERROR]"
|
97 |
+
|
98 |
def extract_ingredients(self, text):
|
99 |
try:
|
100 |
+
candidates = re.findall(r"[a-zA-Z]+(?:\s[a-zA-Z]+)?", text)
|
101 |
+
blocked = {"add", "combine", "cook", "stir", "remove", "cool", "mixture", "saucepan", "until", "heat", "dash"}
|
102 |
+
clean = [c.lower() for c in candidates if c.lower() not in blocked and len(c.split()) <= 3]
|
103 |
+
return ", ".join(sorted(set(clean)))
|
|
|
104 |
except:
|
105 |
+
return text[:100]
|
106 |
|
107 |
+
def format_answer(self, answer, question):
|
|
|
|
|
|
|
108 |
q = question.lower()
|
109 |
+
raw = answer.strip().strip("\"'")
|
110 |
+
if "commutative" in q:
|
111 |
+
return self.extract_commutativity_set(question)
|
112 |
+
if "ingredient" in q:
|
113 |
return self.extract_ingredients(raw)
|
114 |
if "algebraic notation" in q:
|
115 |
+
m = re.search(r"[KQBNR]?[a-h]?[1-8]?x?[a-h][1-8][+#]?", raw)
|
116 |
+
return m.group(0) if m else raw
|
|
|
|
|
|
|
117 |
if "usd" in q:
|
118 |
m = re.search(r"\$?\d+(\.\d{2})", raw)
|
119 |
return f"${m.group()}" if m else "$0.00"
|
120 |
+
if "award number" in q:
|
121 |
+
m = re.search(r"80NSSC[0-9A-Z]+", raw)
|
122 |
+
return m.group(0) if m else raw
|
123 |
if "first name" in q:
|
124 |
+
return raw.split()[0]
|
125 |
try:
|
126 |
return str(w2n.word_to_num(raw))
|
127 |
except:
|
128 |
m = re.search(r"\d+", raw)
|
129 |
return m.group(0) if m else raw
|
130 |
|
131 |
+
def retry_fallback(self, question):
|
132 |
+
try:
|
133 |
+
prompt = f"Answer concisely and factually:
|
134 |
+
Question: {question}"
|
135 |
+
return self.ask(prompt)
|
136 |
+
except:
|
137 |
+
return "[RETRY FAILED]"
|
138 |
+
|
139 |
def __call__(self, question, task_id=None):
|
140 |
try:
|
141 |
+
content, ctype = self.fetch_file(task_id) if task_id else (None, None)
|
142 |
+
context = self.handle_file(content, ctype, question) if content else self.search_context(question)
|
143 |
+
raw = self.ask(f"Use this context to answer:
|
144 |
+
{context}
|
145 |
+
|
146 |
+
Question:
|
147 |
+
{question}
|
148 |
+
Answer:")
|
149 |
+
if not raw or "[ERROR" in raw or "step execution failed" in raw:
|
150 |
+
retry = self.retry_fallback(question)
|
151 |
+
return self.format_answer(retry, question)
|
152 |
+
return self.format_answer(raw, question)
|
153 |
except Exception as e:
|
154 |
return f"[AGENT ERROR: {e}]"
|