Update agent.py
Browse files
agent.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
-
# agent_v44.py — Poprawiona obsługa: YouTube, commutativity, web fallback, Excel
|
2 |
import os
|
3 |
import re
|
4 |
import io
|
5 |
import base64
|
6 |
import requests
|
7 |
import pandas as pd
|
8 |
-
from word2number import w2n
|
9 |
from openai import OpenAI
|
|
|
10 |
from langchain_community.tools import DuckDuckGoSearchRun
|
11 |
|
12 |
class GaiaAgent:
|
@@ -21,7 +20,7 @@ class GaiaAgent:
|
|
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):
|
@@ -29,20 +28,12 @@ class GaiaAgent:
|
|
29 |
r = self.client.chat.completions.create(
|
30 |
model="gpt-4-turbo",
|
31 |
messages=[{"role": "user", "content": prompt}],
|
32 |
-
temperature=0
|
33 |
-
timeout=40
|
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 Exception:
|
44 |
-
return "[WEB ERROR]"
|
45 |
-
|
46 |
def handle_file(self, content, ctype, question):
|
47 |
try:
|
48 |
if "image" in ctype:
|
@@ -50,7 +41,7 @@ class GaiaAgent:
|
|
50 |
result = self.client.chat.completions.create(
|
51 |
model="gpt-4o",
|
52 |
messages=[
|
53 |
-
{"role": "system", "content": "You're a chess assistant.
|
54 |
{"role": "user", "content": [
|
55 |
{"type": "text", "text": question},
|
56 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
@@ -66,44 +57,25 @@ class GaiaAgent:
|
|
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 |
-
|
70 |
-
|
71 |
-
return f"${
|
72 |
return content.decode("utf-8", errors="ignore")[:3000]
|
73 |
-
except
|
74 |
-
return
|
75 |
-
|
76 |
-
def extract_commutativity_set(self, table_txt):
|
77 |
-
try:
|
78 |
-
rows = [r for r in table_txt.splitlines() if r.strip().startswith("|")]
|
79 |
-
head = rows[0].split("|")[2:-1]
|
80 |
-
table = {}
|
81 |
-
for row in rows[1:]:
|
82 |
-
parts = row.split("|")[1:-1]
|
83 |
-
table[parts[0]] = parts[1:]
|
84 |
-
s = set()
|
85 |
-
for a in head:
|
86 |
-
for b in head:
|
87 |
-
if table[a][head.index(b)] != table[b][head.index(a)]:
|
88 |
-
s |= {a, b}
|
89 |
-
return ", ".join(sorted(s))
|
90 |
-
except Exception:
|
91 |
-
return "[COMMUTATIVE ERROR]"
|
92 |
|
93 |
def extract_ingredients(self, text):
|
94 |
try:
|
95 |
tokens = re.findall(r"[a-zA-Z]+(?:\s[a-zA-Z]+)?", text)
|
96 |
-
|
97 |
-
filtered = [t.lower() for t in tokens if t.lower() not in
|
98 |
return ", ".join(sorted(set(filtered)))
|
99 |
-
except
|
100 |
-
return text[:
|
101 |
|
102 |
def format_answer(self, answer, question):
|
103 |
q = question.lower()
|
104 |
raw = answer.strip().strip("\"'")
|
105 |
-
if "commutative" in q:
|
106 |
-
return self.extract_commutativity_set(question)
|
107 |
if "ingredient" in q:
|
108 |
return self.extract_ingredients(raw)
|
109 |
if "algebraic notation" in q:
|
@@ -115,9 +87,6 @@ class GaiaAgent:
|
|
115 |
if "award number" in q:
|
116 |
m = re.search(r"80NSSC[0-9A-Z]+", raw)
|
117 |
return m.group(0) if m else raw
|
118 |
-
if "ioc" in q:
|
119 |
-
m = re.search(r"\b[A-Z]{3}\b", raw)
|
120 |
-
return m.group(0) if m else raw
|
121 |
if "first name" in q:
|
122 |
return raw.split()[0]
|
123 |
try:
|
@@ -126,27 +95,18 @@ class GaiaAgent:
|
|
126 |
m = re.search(r"\d+", raw)
|
127 |
return m.group(0) if m else raw
|
128 |
|
129 |
-
def retry_fallback(self, question):
|
130 |
-
context = self.search_context(question)
|
131 |
-
prompt = f"""Answer concisely and factually:
|
132 |
-
Context: {context}
|
133 |
-
Question: {question}"""
|
134 |
-
return self.ask(prompt)
|
135 |
-
|
136 |
def __call__(self, question, task_id=None):
|
137 |
try:
|
138 |
content, ctype = self.fetch_file(task_id) if task_id else (None, None)
|
139 |
-
context = self.handle_file(content, ctype, question) if content else
|
140 |
-
prompt = f"
|
|
|
141 |
{context}
|
142 |
|
143 |
Question:
|
144 |
{question}
|
145 |
-
Answer:"
|
146 |
raw = self.ask(prompt)
|
147 |
-
if not raw or "[ERROR" in raw or "step execution failed" in raw:
|
148 |
-
retry = self.retry_fallback(question)
|
149 |
-
return self.format_answer(retry, question)
|
150 |
return self.format_answer(raw, question)
|
151 |
except Exception as e:
|
152 |
return f"[AGENT ERROR: {e}]"
|
|
|
|
|
1 |
import os
|
2 |
import re
|
3 |
import io
|
4 |
import base64
|
5 |
import requests
|
6 |
import pandas as pd
|
|
|
7 |
from openai import OpenAI
|
8 |
+
from word2number import w2n
|
9 |
from langchain_community.tools import DuckDuckGoSearchRun
|
10 |
|
11 |
class GaiaAgent:
|
|
|
20 |
r = requests.get(url, timeout=10)
|
21 |
r.raise_for_status()
|
22 |
return r.content, r.headers.get("Content-Type", "")
|
23 |
+
except:
|
24 |
return None, None
|
25 |
|
26 |
def ask(self, prompt):
|
|
|
28 |
r = self.client.chat.completions.create(
|
29 |
model="gpt-4-turbo",
|
30 |
messages=[{"role": "user", "content": prompt}],
|
31 |
+
temperature=0
|
|
|
32 |
)
|
33 |
return r.choices[0].message.content.strip()
|
34 |
+
except:
|
35 |
return "[ERROR: ask failed]"
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def handle_file(self, content, ctype, question):
|
38 |
try:
|
39 |
if "image" in ctype:
|
|
|
41 |
result = self.client.chat.completions.create(
|
42 |
model="gpt-4o",
|
43 |
messages=[
|
44 |
+
{"role": "system", "content": "You're a chess assistant. Answer only with the best move in algebraic notation (e.g., Qd1#)."},
|
45 |
{"role": "user", "content": [
|
46 |
{"type": "text", "text": question},
|
47 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
|
|
|
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[df['category'].str.lower() == 'food']
|
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:
|
65 |
+
return "[FILE ERROR]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
def extract_ingredients(self, text):
|
68 |
try:
|
69 |
tokens = re.findall(r"[a-zA-Z]+(?:\s[a-zA-Z]+)?", text)
|
70 |
+
blocked = {"add", "combine", "cook", "stir", "remove", "cool", "mixture", "saucepan", "until", "heat", "dash"}
|
71 |
+
filtered = [t.lower() for t in tokens if t.lower() not in blocked and len(t.split()) <= 3]
|
72 |
return ", ".join(sorted(set(filtered)))
|
73 |
+
except:
|
74 |
+
return text[:100]
|
75 |
|
76 |
def format_answer(self, answer, question):
|
77 |
q = question.lower()
|
78 |
raw = answer.strip().strip("\"'")
|
|
|
|
|
79 |
if "ingredient" in q:
|
80 |
return self.extract_ingredients(raw)
|
81 |
if "algebraic notation" in q:
|
|
|
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:
|
|
|
95 |
m = re.search(r"\d+", raw)
|
96 |
return m.group(0) if m else raw
|
97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
def __call__(self, question, task_id=None):
|
99 |
try:
|
100 |
content, ctype = self.fetch_file(task_id) if task_id else (None, None)
|
101 |
+
context = self.handle_file(content, ctype, question) if content else ""
|
102 |
+
prompt = f"Use this context to answer the question below.
|
103 |
+
Context:
|
104 |
{context}
|
105 |
|
106 |
Question:
|
107 |
{question}
|
108 |
+
Answer:"
|
109 |
raw = self.ask(prompt)
|
|
|
|
|
|
|
110 |
return self.format_answer(raw, question)
|
111 |
except Exception as e:
|
112 |
return f"[AGENT ERROR: {e}]"
|