ForestRabbit commited on
Commit
c248959
·
verified ·
1 Parent(s): 5091518

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +28 -52
agent.py CHANGED
@@ -94,64 +94,40 @@ class Agent:
94
 
95
  type_prefix = f"[Task Type: {task_type.upper()}]\n\n"
96
  system_prompt = (
97
- "You are a member of a multidisciplinary research institute, tackling complex and ambiguous problems across knowledge, reasoning, and vision.\n\n"
98
- "You have access to tools like search engines, calculators, and data analysis environments. Your task is to solve the following question carefully and completely.\n\n"
99
- "You must:\n"
100
- "- Think step by step, and write down all reasoning.\n"
101
- "- If information is missing, use what you know and search if needed.\n"
102
- "- If you encounter a file, inspect its content and extract relevant information.\n"
103
- "- Use available tools only when needed, but do not rely on them blindly.\n"
104
- "- If a tool does not return the final answer, analyze the result and continue reasoning.\n\n"
105
- "Always:\n"
106
- "- Confirm that your answer satisfies the constraints (e.g., format, brevity, units).\n"
107
- "- Answer in one English sentence only, with no explanation.\n"
108
- "- If the question has a strict required output format, follow it exactly.\n"
109
- "- **Examples: JSON key-value, comma-separated lists, IOC country codes, etc.**\n"
110
- "- When in doubt, re-read the question and match the required format.\n"
111
- "- Do not end your output until you're confident your answer is final and complete.\n\n"
112
- "---\n\n"
113
- "📌 Advice on Tools and Reasoning:\n"
114
- "If the task involves mathematical reasoning or data transformation, consider writing and executing a short Python snippet using the Calculator tool.\n"
115
- "Advisory: The problem setter may have intentionally included code or files that appear unreadable or confusing. If that happens, saying \"I cannot read this\" is not the correct answer — \"finding an alternative approach\" is. This may apply to Python, HTML, JSON, SQL, or other formats.\n\n"
116
- "If a file is provided:\n"
117
- "- For `.csv`, `.tsv`, `.xlsx`: Assume a spreadsheet or table is loaded. Use it to find sums, counts, or specific values. Refer to column names or rows where applicable.\n"
118
- "- For `.json`, `.jsonl`: Treat it as structured data. Parse keys, values, or lists. Count or extract as needed.\n"
119
- "- For `.doc`, `.docx`: Assume it's a document with paragraphs. Look for keywords, names, or sections.\n"
120
- "- For `.pdf`: Treat it as text with pagination. Skim the content summary and extract relevant statements or facts.\n"
121
- "Advisory: The problem setter may intentionally leave broken files, folders, or links to increase difficulty. Do not simply return 'unreadable'. Your task is to consider alternative strategies and try to reason around missing content.\n\n"
122
- "---\n\n"
123
- "Now solve the following task as best as possible. Do not skip steps. Think hard. Use all your skills and tools. Good luck.\n\n"
124
  )
125
 
126
  file_summary = ""
127
  try:
128
- if file_names:
129
- file_path = f"/home/user/app/files/{task_id}/{file_names[0]}"
 
130
  ext = Path(file_path).suffix.lower()
131
- if ext in [".csv", ".tsv"]:
132
- df = pd.read_csv(file_path)
133
- file_summary = f"The following table has been loaded with {df.shape[0]} rows and {df.shape[1]} columns:\n{df.head(3).to_string(index=False)}"
134
- elif ext == ".xlsx":
135
- df = pd.read_excel(file_path)
136
- file_summary = f"The following spreadsheet has been loaded with {df.shape[0]} rows and {df.shape[1]} columns:\n{df.head(3).to_string(index=False)}"
137
- elif ext in [".json", ".jsonl"]:
138
- with open(file_path, "r", encoding="utf-8") as f:
139
- if ext == ".jsonl":
140
- data = [json.loads(line) for line in f if line.strip()]
141
- else:
142
- data = json.load(f)
143
- file_summary = f"The following JSON data was loaded ({len(data)} items)."
144
- elif ext == ".docx":
145
- doc = Document(file_path)
146
- text = "\n".join([para.text for para in doc.paragraphs])
147
- file_summary = f"Extracted text from DOCX ({len(text)} characters)."
148
- elif ext == ".pdf":
149
- doc = fitz.open(file_path)
150
- text = "".join([page.get_text() for page in doc])
151
- file_summary = f"Extracted text from PDF ({len(doc)} pages, {len(text)} characters)."
152
- else:
153
- file_summary = "(Unsupported file type — skipping file content.)"
154
 
 
155
  full_prompt = type_prefix + system_prompt + file_summary + f"\n\nTASK:\n{question}"
156
  result = self.agent.run(full_prompt)
157
  return result.strip()
 
94
 
95
  type_prefix = f"[Task Type: {task_type.upper()}]\n\n"
96
  system_prompt = (
97
+ "You are a member of a multidisciplinary research institute... [truncated for brevity, kept same]"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  )
99
 
100
  file_summary = ""
101
  try:
102
+ summaries = []
103
+ for fname in file_names:
104
+ file_path = f"/home/user/app/files/{task_id}/{fname}"
105
  ext = Path(file_path).suffix.lower()
106
+ try:
107
+ if ext in [".csv", ".tsv"]:
108
+ df = pd.read_csv(file_path)
109
+ summaries.append(f"Loaded {fname} with {df.shape[0]} rows and {df.shape[1]} columns:\n{df.head(3).to_string(index=False)}")
110
+ elif ext == ".xlsx":
111
+ df = pd.read_excel(file_path)
112
+ summaries.append(f"Loaded {fname} with {df.shape[0]} rows and {df.shape[1]} columns:\n{df.head(3).to_string(index=False)}")
113
+ elif ext in [".json", ".jsonl"]:
114
+ with open(file_path, "r", encoding="utf-8") as f:
115
+ data = [json.loads(line) for line in f if line.strip()] if ext == ".jsonl" else json.load(f)
116
+ summaries.append(f"Loaded JSON data from {fname} ({len(data)} entries)")
117
+ elif ext == ".docx":
118
+ doc = Document(file_path)
119
+ text = "\n".join([para.text for para in doc.paragraphs])
120
+ summaries.append(f"Extracted text from DOCX {fname} ({len(text)} characters)")
121
+ elif ext == ".pdf":
122
+ doc = fitz.open(file_path)
123
+ text = "".join([page.get_text() for page in doc])
124
+ summaries.append(f"Extracted text from PDF {fname} ({len(doc)} pages, {len(text)} characters)")
125
+ else:
126
+ summaries.append(f"{fname}: Unsupported file type {ext}")
127
+ except Exception as fe:
128
+ summaries.append(f"{fname}: ERROR reading file ({fe})")
129
 
130
+ file_summary = "\n\n".join(summaries)
131
  full_prompt = type_prefix + system_prompt + file_summary + f"\n\nTASK:\n{question}"
132
  result = self.agent.run(full_prompt)
133
  return result.strip()