Update agent.py
Browse files
agent.py
CHANGED
@@ -1,3 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# agent_v43.py — Najlepsze cechy z V18–V34: precyzja, retry fallback, stabilność
|
2 |
import os
|
3 |
import re
|
@@ -74,81 +151,4 @@ class GaiaAgent:
|
|
74 |
return "[NO FOOD SALES DATA]"
|
75 |
return content.decode("utf-8", errors="ignore")[:3000]
|
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}]"
|
|
|
1 |
+
Agent V31
|
2 |
+
1
|
3 |
+
2
|
4 |
+
3
|
5 |
+
4
|
6 |
+
5
|
7 |
+
6
|
8 |
+
7
|
9 |
+
8
|
10 |
+
9
|
11 |
+
10
|
12 |
+
11
|
13 |
+
12
|
14 |
+
13
|
15 |
+
14
|
16 |
+
15
|
17 |
+
16
|
18 |
+
17
|
19 |
+
18
|
20 |
+
19
|
21 |
+
20
|
22 |
+
21
|
23 |
+
22
|
24 |
+
23
|
25 |
+
24
|
26 |
+
25
|
27 |
+
26
|
28 |
+
27
|
29 |
+
28
|
30 |
+
29
|
31 |
+
30
|
32 |
+
31
|
33 |
+
32
|
34 |
+
33
|
35 |
+
34
|
36 |
+
35
|
37 |
+
36
|
38 |
+
37
|
39 |
+
38
|
40 |
+
39
|
41 |
+
40
|
42 |
+
41
|
43 |
+
42
|
44 |
+
43
|
45 |
+
44
|
46 |
+
45
|
47 |
+
46
|
48 |
+
47
|
49 |
+
48
|
50 |
+
49
|
51 |
+
50
|
52 |
+
51
|
53 |
+
52
|
54 |
+
53
|
55 |
+
54
|
56 |
+
55
|
57 |
+
56
|
58 |
+
57
|
59 |
+
58
|
60 |
+
59
|
61 |
+
60
|
62 |
+
61
|
63 |
+
62
|
64 |
+
63
|
65 |
+
64
|
66 |
+
65
|
67 |
+
66
|
68 |
+
67
|
69 |
+
68
|
70 |
+
69
|
71 |
+
70
|
72 |
+
71
|
73 |
+
72
|
74 |
+
73
|
75 |
+
74
|
76 |
+
75
|
77 |
+
76
|
78 |
# agent_v43.py — Najlepsze cechy z V18–V34: precyzja, retry fallback, stabilność
|
79 |
import os
|
80 |
import re
|
|
|
151 |
return "[NO FOOD SALES DATA]"
|
152 |
return content.decode("utf-8", errors="ignore")[:3000]
|
153 |
except Exception as e:
|
|
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|