dawid-lorek commited on
Commit
d8f0a51
·
verified ·
1 Parent(s): b5349ae

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +42 -48
agent.py CHANGED
@@ -1,4 +1,4 @@
1
- # agent_v40.py — czysty agent, zero sugerowanych odpowiedzi, precyzyjne pozyskiwanie
2
  import os
3
  import re
4
  import io
@@ -24,22 +24,46 @@ class GaiaAgent:
24
  except:
25
  return None, None
26
 
27
- def search_context(self, question):
 
 
 
 
 
 
 
28
  try:
29
- return self.search_tool.run(question)[:2000]
 
 
 
 
 
 
30
  except:
31
- return ""
 
 
 
 
 
 
32
 
33
- def ask(self, context, question):
 
 
 
 
34
  try:
 
35
  response = self.client.chat.completions.create(
36
  model="gpt-4-turbo",
37
  messages=[
38
- {"role": "system", "content": "Answer precisely and factually based only on the provided context. Return only the final answer, in the correct format."},
39
- {"role": "user", "content": f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"}
40
  ],
41
  temperature=0,
42
- timeout=25
43
  )
44
  return response.choices[0].message.content.strip()
45
  except:
@@ -51,7 +75,7 @@ class GaiaAgent:
51
  if "image" in ctype:
52
  b64 = base64.b64encode(content).decode("utf-8")
53
  messages = [
54
- {"role": "system", "content": "You're a chess analyst. Return only the best move for Black that guarantees a win. Use algebraic notation, like Qd1 or Rxf2."},
55
  {"role": "user", "content": [
56
  {"type": "text", "text": question},
57
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
@@ -76,55 +100,20 @@ class GaiaAgent:
76
  return "$0.00"
77
  return content.decode("utf-8", errors="ignore")[:3000]
78
 
79
- def extract_commutativity_set(self, question):
80
- try:
81
- lines = question.splitlines()
82
- S, table = [], {}
83
- for line in lines:
84
- if line.startswith("|*"):
85
- S = line.strip().split("|")[2:]
86
- elif line.startswith("|") and len(line.strip().split("|")) > 2:
87
- parts = line.strip().split("|")[1:-1]
88
- row_key, values = parts[0], parts[1:]
89
- table[row_key] = values
90
- non_comm = set()
91
- for x in S:
92
- for y in S:
93
- if table[x][S.index(y)] != table[y][S.index(x)]:
94
- non_comm.update([x, y])
95
- return ", ".join(sorted(non_comm))
96
- except:
97
- return ""
98
-
99
- def validate_format(self, answer, question):
100
- q = question.lower()
101
- a = answer.strip()
102
- if "algebraic notation" in q:
103
- return bool(re.fullmatch(r"[KQBNR]?[a-h]?[1-8]?x?[a-h][1-8][+#]?", a))
104
- if "usd with two decimal places" in q:
105
- return bool(re.fullmatch(r"\$\d+\.\d{2}", a))
106
- if "ioc country code" in q:
107
- return bool(re.fullmatch(r"[A-Z]{3}", a.strip()))
108
- if "award number" in q:
109
- return bool(re.fullmatch(r"80NSSC[0-9A-Z]{6,7}", a))
110
- return True
111
-
112
  def format_answer(self, raw, question):
113
  raw = raw.strip().strip("\"'")
114
  q = question.lower()
115
- if "commutative" in q:
116
- return self.extract_commutativity_set(question)
117
  if "algebraic notation" in q:
118
  match = re.search(r"[KQBNR]?[a-h]?[1-8]?x?[a-h][1-8][+#]?", raw)
119
  return match.group(0) if match else raw
120
  if "award number" in q:
121
  match = re.search(r"80NSSC[0-9A-Z]+", raw)
122
  return match.group(0) if match else raw
123
- if "first name" in q:
124
- return raw.split()[0]
125
  if "usd" in q:
126
  m = re.search(r"\d+(\.\d{2})", raw)
127
  return f"${m.group()}" if m else "$0.00"
 
 
128
  try:
129
  return str(w2n.word_to_num(raw))
130
  except:
@@ -133,6 +122,11 @@ class GaiaAgent:
133
 
134
  def __call__(self, question, task_id=None):
135
  file, ctype = self.fetch_file(task_id) if task_id else (None, None)
136
- context = self.handle_file(file, ctype, question) if file else self.search_context(question)
137
- raw = self.ask(context, question)
 
 
 
 
 
138
  return self.format_answer(raw, question)
 
1
+ # agent_v41.py — Agent analizujący każde pytanie krok po kroku i szukający odpowiedzi zewnętrznie
2
  import os
3
  import re
4
  import io
 
24
  except:
25
  return None, None
26
 
27
+ def get_step_by_step_plan(self, question):
28
+ steps_prompt = f"""
29
+ You are an expert planner. Break down the question into a clear plan with 2–5 steps.
30
+
31
+ Question: {question}
32
+
33
+ Steps:
34
+ """
35
  try:
36
+ response = self.client.chat.completions.create(
37
+ model="gpt-4-turbo",
38
+ messages=[{"role": "user", "content": steps_prompt}],
39
+ temperature=0,
40
+ timeout=15
41
+ )
42
+ return response.choices[0].message.content.strip()
43
  except:
44
+ return "Step 1: Try to understand the question."
45
+
46
+ def search_with_steps(self, question, steps):
47
+ combined_prompt = f"""
48
+ You are a knowledgeable assistant. Given the following plan:
49
+
50
+ {steps}
51
 
52
+ Answer the original question using verified and precise information.
53
+ Return only the final answer, nothing else.
54
+
55
+ Question: {question}
56
+ """
57
  try:
58
+ web_context = self.search_tool.run(question)[:2000]
59
  response = self.client.chat.completions.create(
60
  model="gpt-4-turbo",
61
  messages=[
62
+ {"role": "system", "content": f"Use only this web data:\n{web_context}"},
63
+ {"role": "user", "content": combined_prompt}
64
  ],
65
  temperature=0,
66
+ timeout=30
67
  )
68
  return response.choices[0].message.content.strip()
69
  except:
 
75
  if "image" in ctype:
76
  b64 = base64.b64encode(content).decode("utf-8")
77
  messages = [
78
+ {"role": "system", "content": "You're a chess analyst. Return only the best move for Black that guarantees a win. Use algebraic notation."},
79
  {"role": "user", "content": [
80
  {"type": "text", "text": question},
81
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
 
100
  return "$0.00"
101
  return content.decode("utf-8", errors="ignore")[:3000]
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  def format_answer(self, raw, question):
104
  raw = raw.strip().strip("\"'")
105
  q = question.lower()
 
 
106
  if "algebraic notation" in q:
107
  match = re.search(r"[KQBNR]?[a-h]?[1-8]?x?[a-h][1-8][+#]?", raw)
108
  return match.group(0) if match else raw
109
  if "award number" in q:
110
  match = re.search(r"80NSSC[0-9A-Z]+", raw)
111
  return match.group(0) if match else raw
 
 
112
  if "usd" in q:
113
  m = re.search(r"\d+(\.\d{2})", raw)
114
  return f"${m.group()}" if m else "$0.00"
115
+ if "first name" in q:
116
+ return raw.split()[0]
117
  try:
118
  return str(w2n.word_to_num(raw))
119
  except:
 
122
 
123
  def __call__(self, question, task_id=None):
124
  file, ctype = self.fetch_file(task_id) if task_id else (None, None)
125
+
126
+ if file:
127
+ context = self.handle_file(file, ctype, question)
128
+ return self.format_answer(context, question)
129
+
130
+ steps = self.get_step_by_step_plan(question)
131
+ raw = self.search_with_steps(question, steps)
132
  return self.format_answer(raw, question)