Skip to content

Commit f38ad4d

Browse files
authored
feat(attachments): 添加附件处理工具,包括列出、读取、获取信息和搜索功能,并更新依赖项 (#133)
* feat(attachments): 添加附件处理工具,包括列出、读取、获取信息和搜索功能,并更新依赖项 * feat(file_tools): 增强附件处理功能,添加详细日志记录和JSON格式化输出
1 parent 8e02549 commit f38ad4d

File tree

3 files changed

+517
-5
lines changed

3 files changed

+517
-5
lines changed

backend/ai_system/core_agents/main_agent.py

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,22 @@ def get_system_prompt(self) -> str:
6464
"你的职责:\n"
6565
"0. 请你生成论文为paper.md文档!!!\n"
6666
"1. 分析用户需求,制定论文生成计划\n"
67-
"2. 当需要代码执行、数据分析、图表生成时,调用CodeAgent工具\n"
68-
"3. 维护对话上下文,理解整个工作流程的连续性\n"
69-
"4. 最终使用tree工具检查生成的文件\n\n"
67+
"2. **主动检查和分析附件**:当用户上传附件时,使用list_attachments工具查看所有附件,然后使用read_attachment工具读取相关内容\n"
68+
"3. 当需要代码执行、数据分析、图表生成时,调用CodeAgent工具\n"
69+
"4. 维护对话上下文,理解整个工作流程的连续性\n"
70+
"5. 最终使用tree工具检查生成的文件\n\n"
71+
"**附件处理能力**:\n"
72+
"- 你可以读取和分析各种格式的附件文件(PDF、Word、Excel、CSV、文本文件、代码文件等)\n"
73+
"- 使用list_attachments查看所有可用附件\n"
74+
"- 使用read_attachment读取具体附件内容\n"
75+
"- 使用get_attachment_info获取附件详细信息\n"
76+
"- 使用search_attachments在附件中搜索关键词\n"
77+
"- 基于附件内容进行论文写作和数据分析\n\n"
7078
"重要原则:\n"
7179
"- 保持对话连贯性,不重复询问已明确的信息\n"
7280
"- 你是中枢大脑,负责规划和协调,不能直接编写、执行代码\n"
7381
"- CodeAgent负责具体执行,你负责规划和协调\n"
82+
"- **充分利用用户上传的附件内容,确保论文基于真实的资料和数据**\n"
7483
"- 所有生成的文件都要在最终论文中引用\n"
7584
"- 请自己执行迭代,直到任务完成\n"
7685
"- 生成的论文不要杜撰,确保科学性"
@@ -165,8 +174,89 @@ def _setup_tools(self):
165174
},
166175
}
167176

177+
# 附件读取工具
178+
list_attachments_tool = {
179+
"type": "function",
180+
"function": {
181+
"name": "list_attachments",
182+
"description": "列出工作空间中所有上传的附件文件",
183+
"parameters": {
184+
"type": "object",
185+
"properties": {},
186+
"required": [],
187+
},
188+
},
189+
}
190+
191+
read_attachment_tool = {
192+
"type": "function",
193+
"function": {
194+
"name": "read_attachment",
195+
"description": "读取指定附件文件的内容,支持txt、pdf、docx、csv、excel等格式",
196+
"parameters": {
197+
"type": "object",
198+
"properties": {
199+
"file_path": {
200+
"type": "string",
201+
"description": "附件文件路径(相对于attachment目录的路径)"
202+
}
203+
},
204+
"required": ["file_path"],
205+
},
206+
},
207+
}
208+
209+
get_attachment_info_tool = {
210+
"type": "function",
211+
"function": {
212+
"name": "get_attachment_info",
213+
"description": "获取附件文件的详细信息,包括文件大小、类型、创建时间等元数据",
214+
"parameters": {
215+
"type": "object",
216+
"properties": {
217+
"file_path": {
218+
"type": "string",
219+
"description": "附件文件路径(相对于attachment目录的路径)"
220+
}
221+
},
222+
"required": ["file_path"],
223+
},
224+
},
225+
}
226+
227+
search_attachments_tool = {
228+
"type": "function",
229+
"function": {
230+
"name": "search_attachments",
231+
"description": "在所有附件文件中搜索关键词,支持文件名和文件内容搜索",
232+
"parameters": {
233+
"type": "object",
234+
"properties": {
235+
"keyword": {
236+
"type": "string",
237+
"description": "要搜索的关键词"
238+
},
239+
"file_type": {
240+
"type": "string",
241+
"description": "可选的文件类型过滤(如 'pdf', 'docx', 'txt' 等)"
242+
}
243+
},
244+
"required": ["keyword"],
245+
},
246+
},
247+
}
248+
168249
# 模板操作工具(仅在有模板时添加)
169-
tools = [code_interpreter_tool, writemd_tool, update_template_tool, tree_tool]
250+
tools = [
251+
code_interpreter_tool,
252+
writemd_tool,
253+
update_template_tool,
254+
tree_tool,
255+
list_attachments_tool,
256+
read_attachment_tool,
257+
get_attachment_info_tool,
258+
search_attachments_tool
259+
]
170260

171261
if self.template_id:
172262
# analyze_template工具
@@ -278,6 +368,10 @@ def _register_tool_functions(self):
278368
"writemd": file_tool.writemd,
279369
"update_template": file_tool.update_template,
280370
"tree": file_tool.tree,
371+
"list_attachments": file_tool.list_attachments,
372+
"read_attachment": file_tool.read_attachment,
373+
"get_attachment_info": file_tool.get_attachment_info,
374+
"search_attachments": file_tool.search_attachments,
281375
"CodeAgent": self._execute_code_agent_wrapper
282376
}
283377

0 commit comments

Comments
 (0)