Update agent.py
Browse files
agent.py
CHANGED
|
@@ -79,35 +79,55 @@ class Agent:
|
|
| 79 |
file_names = input_data.get("file_names", [])
|
| 80 |
task_id = input_data.get("task_id", "")
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
try:
|
| 83 |
if file_names:
|
| 84 |
file_path = f"/home/user/app/files/{task_id}/{file_names[0]}"
|
| 85 |
ext = Path(file_path).suffix.lower()
|
| 86 |
if ext in [".csv", ".tsv"]:
|
| 87 |
df = pd.read_csv(file_path)
|
| 88 |
-
|
| 89 |
-
elif ext
|
| 90 |
df = pd.read_excel(file_path)
|
| 91 |
-
|
| 92 |
elif ext in [".json", ".jsonl"]:
|
| 93 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 94 |
if ext == ".jsonl":
|
| 95 |
data = [json.loads(line) for line in f if line.strip()]
|
| 96 |
else:
|
| 97 |
data = json.load(f)
|
| 98 |
-
|
| 99 |
elif ext == ".docx":
|
| 100 |
doc = Document(file_path)
|
| 101 |
text = "\n".join([para.text for para in doc.paragraphs])
|
| 102 |
-
|
| 103 |
elif ext == ".pdf":
|
| 104 |
doc = fitz.open(file_path)
|
| 105 |
text = "".join([page.get_text() for page in doc])
|
| 106 |
-
|
| 107 |
else:
|
| 108 |
-
|
| 109 |
|
| 110 |
-
|
|
|
|
| 111 |
return result.strip()
|
| 112 |
except Exception as e:
|
| 113 |
return f"AGENT ERROR: {str(e)}"
|
|
|
|
| 79 |
file_names = input_data.get("file_names", [])
|
| 80 |
task_id = input_data.get("task_id", "")
|
| 81 |
|
| 82 |
+
system_prompt = (
|
| 83 |
+
"You are a member of a multidisciplinary research institute, tackling complex and ambiguous problems across knowledge, reasoning, and vision.\n\n"
|
| 84 |
+
"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"
|
| 85 |
+
"You must:\n"
|
| 86 |
+
"- Think step by step, and write down all reasoning.\n"
|
| 87 |
+
"- If information is missing, use what you know and search if needed.\n"
|
| 88 |
+
"- If you encounter a file, inspect its content and extract relevant information.\n"
|
| 89 |
+
"- Use available tools only when needed, but do not rely on them blindly.\n"
|
| 90 |
+
"- If a tool does not return the final answer, analyze the result and continue reasoning.\n\n"
|
| 91 |
+
"Always:\n"
|
| 92 |
+
"- Confirm that your answer satisfies the constraints (e.g., format, brevity, units).\n"
|
| 93 |
+
"- Answer in one English sentence only, with no explanation.\n"
|
| 94 |
+
"- If the question has a strict required output format, follow it exactly.\n"
|
| 95 |
+
"- Do not end your output until you're confident your answer is final and complete.\n\n"
|
| 96 |
+
"---\n\n"
|
| 97 |
+
"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"
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
file_summary = ""
|
| 101 |
try:
|
| 102 |
if file_names:
|
| 103 |
file_path = f"/home/user/app/files/{task_id}/{file_names[0]}"
|
| 104 |
ext = Path(file_path).suffix.lower()
|
| 105 |
if ext in [".csv", ".tsv"]:
|
| 106 |
df = pd.read_csv(file_path)
|
| 107 |
+
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)}"
|
| 108 |
+
elif ext == ".xlsx":
|
| 109 |
df = pd.read_excel(file_path)
|
| 110 |
+
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)}"
|
| 111 |
elif ext in [".json", ".jsonl"]:
|
| 112 |
with open(file_path, "r", encoding="utf-8") as f:
|
| 113 |
if ext == ".jsonl":
|
| 114 |
data = [json.loads(line) for line in f if line.strip()]
|
| 115 |
else:
|
| 116 |
data = json.load(f)
|
| 117 |
+
file_summary = f"The following JSON data was loaded ({len(data)} items)."
|
| 118 |
elif ext == ".docx":
|
| 119 |
doc = Document(file_path)
|
| 120 |
text = "\n".join([para.text for para in doc.paragraphs])
|
| 121 |
+
file_summary = f"Extracted text from DOCX ({len(text)} characters)."
|
| 122 |
elif ext == ".pdf":
|
| 123 |
doc = fitz.open(file_path)
|
| 124 |
text = "".join([page.get_text() for page in doc])
|
| 125 |
+
file_summary = f"Extracted text from PDF ({len(doc)} pages, {len(text)} characters)."
|
| 126 |
else:
|
| 127 |
+
file_summary = "(Unsupported file type — skipping file content.)"
|
| 128 |
|
| 129 |
+
full_prompt = system_prompt + file_summary + f"\n\nTASK:\n{question}"
|
| 130 |
+
result = self.agent.run(full_prompt)
|
| 131 |
return result.strip()
|
| 132 |
except Exception as e:
|
| 133 |
return f"AGENT ERROR: {str(e)}"
|