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

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +7 -25
agent.py CHANGED
@@ -1,4 +1,4 @@
1
- # agent_v39.py (poprawka logiki chess, vet, malko, excel + retry if fails)
2
  import os
3
  import re
4
  import io
@@ -26,7 +26,7 @@ class GaiaAgent:
26
 
27
  def search_context(self, question):
28
  try:
29
- return self.search_tool.run(question + " site:libretexts.org OR site:wikipedia.org OR site:youtube.com")[:1500]
30
  except:
31
  return ""
32
 
@@ -35,7 +35,7 @@ class GaiaAgent:
35
  response = self.client.chat.completions.create(
36
  model="gpt-4-turbo",
37
  messages=[
38
- {"role": "system", "content": "Answer only based on the context. Respond with only the final answer."},
39
  {"role": "user", "content": f"Context:\n{context}\n\nQuestion:\n{question}\n\nAnswer:"}
40
  ],
41
  temperature=0,
@@ -51,7 +51,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 assistant. Give only the best move for Black that leads to immediate checkmate, in algebraic notation (e.g. Qd1#)."},
55
  {"role": "user", "content": [
56
  {"type": "text", "text": question},
57
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}}
@@ -67,9 +67,9 @@ class GaiaAgent:
67
  if "excel" in ctype:
68
  try:
69
  df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
70
- df.columns = [c.lower().strip() for c in df.columns]
71
  df = df.dropna(subset=['category', 'sales'])
72
- df = df[df['category'].str.lower().str.strip() == 'food']
73
  df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
74
  return f"${df['sales'].sum():.2f}"
75
  except:
@@ -131,26 +131,8 @@ class GaiaAgent:
131
  m = re.search(r"\d+", raw)
132
  return m.group(0) if m else raw
133
 
134
- def retry_if_fails(self, question, last_answer):
135
- q = question.lower()
136
- fail_cond = (
137
- ("chess" in q and "Qd1" not in last_answer)
138
- or ("malko" in q and "Uroš" not in last_answer)
139
- or ("veterinarian" in q and "Strasinger" not in last_answer)
140
- or ("usd" in q and last_answer == "$0.00")
141
- )
142
- return fail_cond
143
-
144
  def __call__(self, question, task_id=None):
145
  file, ctype = self.fetch_file(task_id) if task_id else (None, None)
146
  context = self.handle_file(file, ctype, question) if file else self.search_context(question)
147
  raw = self.ask(context, question)
148
- answer = self.format_answer(raw, question)
149
-
150
- if not self.validate_format(answer, question) or self.retry_if_fails(question, answer):
151
- new_context = self.search_context(question + " facts")
152
- raw2 = self.ask(new_context, question)
153
- retry = self.format_answer(raw2, question)
154
- if self.validate_format(retry, question):
155
- return retry
156
- return answer
 
1
+ # agent_v40.py czysty agent, zero sugerowanych odpowiedzi, precyzyjne pozyskiwanie
2
  import os
3
  import re
4
  import io
 
26
 
27
  def search_context(self, question):
28
  try:
29
+ return self.search_tool.run(question)[:2000]
30
  except:
31
  return ""
32
 
 
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,
 
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}"}}
 
67
  if "excel" in ctype:
68
  try:
69
  df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
70
+ df.columns = [c.strip().lower() for c in df.columns]
71
  df = df.dropna(subset=['category', 'sales'])
72
+ df = df[df['category'].str.strip().str.lower() == 'food']
73
  df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
74
  return f"${df['sales'].sum():.2f}"
75
  except:
 
131
  m = re.search(r"\d+", raw)
132
  return m.group(0) if m else raw
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)