Try the changes for system prompt + debug info
Browse files
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
**
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
**
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
- If you can't find a confident answer, return your best guess.
|
| 81 |
|
| 82 |
-
**
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 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 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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}"})
|