ameglei-external commited on
Commit
0031094
·
verified ·
1 Parent(s): 3aaeca6

Try the changes for system prompt + debug info

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -3,6 +3,7 @@ import tempfile
3
  from contextlib import suppress
4
  from io import BytesIO
5
  from pprint import pprint
 
6
  from typing import TypedDict, List, Dict, Any, Optional, Tuple
7
  from typing_extensions import Annotated
8
 
@@ -65,49 +66,41 @@ class BasicAgent:
65
  print(f"\nAgent received question: {question}")
66
  sys_msg = SystemMessage(
67
  content="""
68
- You are a ReAct (Reasoning and Acting) agent. I will ask you a question. Your job is to reason through it and decide whether to act (by using a tool) or answer directly.
 
 
 
 
 
69
 
70
- **Instructions:**
71
- 1. Think step-by-step through the question, prior reasoning, and any tool observations.
72
- 2. Decide the best next move: use a tool, or answer directly.
73
- 3. Only provide a final answer when confident.
 
74
 
75
- **Guidelines:**
76
- - Be thorough and logical.
77
- - Use tools when more information is needed.
78
- - Always ground your answer in tool results or reasoning.
79
- - If a tool fails or returns nothing, acknowledge it and try an alternative.
80
- - If you can't find a confident answer, return your best guess.
81
 
82
- **Important:**
83
- - When giving the final answer, reply with only the answer — nothing else.
84
- - Your final answer should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
85
- - If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
86
- - If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
87
- - If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
88
- - If there is no direct ask about the response format, think if it's possible to answer with a number, if yes - do it, if no - answer with as few words as possible, not the whole sentence.
89
-
90
- **Examples:**
91
-
92
- Q: What is 2 + 3?
93
- Thought: Simple arithmetic.
94
- Final Answer: 5
95
 
96
- Q: How many days are there in a leap year?
97
- Thought: Leap years have 366 days. It's possible to answer with a number only, so the answer will be "366".
98
- Final Answer: 366
99
-
100
- Q: What is the color opposite to "white"?
101
- Thought: "Black" color is the opposite color to "white".
102
- Final Answer: black
103
-
104
- Q: Please, sort words "acronym", "potato" and "curriculum" alphabetically in the descending order and return the comma separated string.
105
- Thought: Since we're using the descending order there will be "potato" then "curriculum" and then "acronym". Since I need to return comma separated string the answer will be "potato, curriculum, acronym".
106
- Final Answer: potato, curriculum, acronym
107
 
108
- Q: Who was the first person to walk on the Moon?
109
- Thought: Neil Armstrong was the first person to walk on the Moon. But I need to be as concise as possible, so it should be "Neil Armstrong".
110
- Final Answer: Neil Armstrong
111
  """
112
  )
113
 
@@ -160,6 +153,7 @@ class BasicAgent:
160
  )
161
  def read_text_file_tool(file_name: str) -> str:
162
  print(f"\nCalling read text file tool for", file_name)
 
163
  with open(file_name, 'r') as f:
164
  return f.read()
165
 
@@ -175,7 +169,10 @@ class BasicAgent:
175
  Returns:
176
  The LLM’s answer based on the image content.
177
  """
178
- # Load & save as bytes so the vision model can consume it
 
 
 
179
  img = Image.open(path)
180
  img_bytes = BytesIO()
181
  img.save(img_bytes, format=img.format)
@@ -200,7 +197,8 @@ class BasicAgent:
200
  """
201
  if not os.path.exists(path):
202
  return f"Error: file not found at {path}"
203
-
 
204
  audio = AudioSegment.from_file(path)
205
  tmp_path = os.path.join(tempfile.gettempdir(), "tmp_audio.wav")
206
  audio.export(tmp_path, format="wav")
@@ -233,7 +231,7 @@ class BasicAgent:
233
  Returns:
234
  The string form of the content.
235
  """
236
- df = pd.read_excel(path)
237
  return str(df.to_csv(index=False))
238
 
239
 
@@ -311,6 +309,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
311
  submitted_answer, logs = agent(question_text)
312
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
313
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
 
 
314
  except Exception as e:
315
  print(f"Error running agent on task {task_id}: {e}")
316
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
 
3
  from contextlib import suppress
4
  from io import BytesIO
5
  from pprint import pprint
6
+ from time import sleep
7
  from typing import TypedDict, List, Dict, Any, Optional, Tuple
8
  from typing_extensions import Annotated
9
 
 
66
  print(f"\nAgent received question: {question}")
67
  sys_msg = SystemMessage(
68
  content="""
69
+ You are a ReAct (Reasoning and Acting) agent with self-reflection. For each question:
70
+
71
+ 1. **Thought:** Briefly outline your reasoning step.
72
+ 2. **Reflect:** Check “Did I use all observations? Did my tool call succeed?”
73
+ 3. **Action:** Either call a tool (with arguments) or prepare your final answer.
74
+ 4. **Final Answer:** Provide only the bare result (no labels, no extra text).
75
 
76
+ **Answer Format Rules**
77
+ - If the answer is a number, output digits only (no commas, no units, no strings like “one”, “twenty three”).
78
+ - If it’s a word or phrase, don't use articles, neither abbreviations (e.g. for cities).
79
+ - If it’s a comma separated list, output a comma-separated list following the above rules for each element.
80
+ - **Always** output exactly one line as an answer and nothing else.
81
 
82
+ **Example 1**
83
+ Q: What is 7 × 6?
84
+ Thought: Multiply 7 by 6.
85
+ Reflect: Simple arithmetic, no tool needed.
86
+ Final Answer: 42
 
87
 
88
+ **Example 2**
89
+ Q: How many prime numbers are there under 20?
90
+ Thought: Primes under 20 are 2, 3, 5, 7, 11, 13, 17, 19 (8 total).
91
+ Reflect: Count is correct.
92
+ Final Answer: 8
 
 
 
 
 
 
 
 
93
 
94
+ **Example 3**
95
+ Q: Sort “banana”, “apple”, “cherry” alphabetically descending.
96
+ Thought: Alphabetical descending: cherry, banana, apple.
97
+ Reflect: Order and formatting confirmed.
98
+ Final Answer: cherry, banana, apple
99
+
100
+ ---
101
+
102
+ Now answer the next question following this chain-of-thought + reflection pattern, and output **only** the `Final Answer` in the required format.
 
 
103
 
 
 
 
104
  """
105
  )
106
 
 
153
  )
154
  def read_text_file_tool(file_name: str) -> str:
155
  print(f"\nCalling read text file tool for", file_name)
156
+ print("File metadata:", os.stat(file_name))
157
  with open(file_name, 'r') as f:
158
  return f.read()
159
 
 
169
  Returns:
170
  The LLM’s answer based on the image content.
171
  """
172
+ if not os.path.exists(path):
173
+ return f"Error: file not found at {path}"
174
+
175
+ print("File metadata:", os.stat(path))
176
  img = Image.open(path)
177
  img_bytes = BytesIO()
178
  img.save(img_bytes, format=img.format)
 
197
  """
198
  if not os.path.exists(path):
199
  return f"Error: file not found at {path}"
200
+
201
+ print("File metadata:", os.stat(path))
202
  audio = AudioSegment.from_file(path)
203
  tmp_path = os.path.join(tempfile.gettempdir(), "tmp_audio.wav")
204
  audio.export(tmp_path, format="wav")
 
231
  Returns:
232
  The string form of the content.
233
  """
234
+ df = pd.read_excel(path, engine='openpyxl')
235
  return str(df.to_csv(index=False))
236
 
237
 
 
309
  submitted_answer, logs = agent(question_text)
310
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
311
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
312
+ print(f"\n\n\n==============Finishing task id: {task_id}, question_text: {question_text}==============\n\n\n")
313
+ sleep(2)
314
  except Exception as e:
315
  print(f"Error running agent on task {task_id}: {e}")
316
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})