Skip to content

Commit a1a6b01

Browse files
committed
modify
modify
1 parent 5b92879 commit a1a6b01

File tree

13 files changed

+147
-2343
lines changed

13 files changed

+147
-2343
lines changed

backend/app/config/md_template.toml

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
firstPage = """
22
# 标题: 基于全文取一个标题
33
例子:基于哈里斯鹰算法的农业种植优化模型研究
4-
54
摘要
65
第一段:背景
7-
例子:随着农业现代化的推进,农业种植问题逐渐成为乡村发展中的关键课题之一
86
大约100字
97
10-
第二段:针对问题一,根据问题一回答,使用到什么模型,如何去构建该模型,做了什么工作,得到什么结果,一些具体的数值
11-
大约170字
12-
13-
每个问题都需要单独撰写一段,与上面相同要求
8+
每个问题都需要按照下面类似的单独撰写成一段'\n',不要分点叙述。
9+
针对问题几,根据问题几回答,使用到什么模型,如何去构建该模型,做了什么工作,得到什么结果,一些具体的数值
10+
每段大约170字
1411
1512
最后一段:总结敏感性分析使用和效果,结论。
1613
大约150字
1714
18-
注意:每段都是按段落编写,不要分点叙述。
19-
2015
关键词: 用到的模型等,关键词大约4/5个,没有标号
2116
例子:HHO MOHHO 遗传算法 Mealpy 蒙特卡洛模拟 规划问题
2217
@@ -35,11 +30,7 @@ RepeatQues = """
3530
## 1.2 问题重述
3631
参考模板如下
3732
本文基于以上信息建立数学模型来解决以下问题。
38-
- 问题1:考虑农作物的预期销售量、种植成本、亩产量及销售价格与2023年持平的情况下,提出2024至2030年乡村农作物的最优种植方案。
39-
- 并且对于预计销售量的结果在两种情况下进行计算:(1)超出部分滞销浪费;(2)超出部分以2023年销售价格的50%售出。
40-
- 问题2:在考虑农作物预期销售量、亩产量、种植成本、销售价格以及气候、市场变化等不确定因素的情况下,给出2024至2030年的最优种植方案,并充分评估种植风险。
41-
- 问题3:进一步考虑不同农作物之间的可替代性和互补性,销售量、价格和成本之间的相关性,通过数据模拟,求解最优种植策略,并与问题2的结果进行比较。
42-
- 如果还有问题
33+
分点叙述
4334
4435
**参考下面的题目要求和问题,按照上面模板和要求完成 **问题重述中的问题背景和问题重述**
4536
@@ -69,7 +60,6 @@ modelAssumption = """
6960
写大概3/4条,下面例子:
7061
(1) 数据有效性:假设所用数据真实可靠
7162
(2) 市场稳定性:假设农业种植中没有自然灾害、政府政策等因素影响。
72-
(3) 农民自主性:假设2023年农作物的销量为农民农作物全年产量。
7363
7464
参考下面的模型的建立与求解 和 题目 ,按照上面模板和要求完成 **模型的假设**
7565

backend/app/core/agents/coder_agent.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,24 +118,8 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
118118
),
119119
)
120120

121-
full_content = response.choices[0].message.content
122121
# 更新对话历史 - 添加助手的响应
123-
self.append_chat_history(
124-
{
125-
"role": "assistant",
126-
"content": full_content,
127-
"tool_calls": [
128-
{
129-
"id": tool_id,
130-
"type": "function",
131-
"function": {
132-
"name": "execute_code",
133-
"arguments": json.dumps({"code": code}),
134-
},
135-
}
136-
],
137-
}
138-
)
122+
self.append_chat_history(response.choices[0].message.model_dump())
139123

140124
# 执行工具调用
141125
logger.info("执行工具调用")
@@ -151,9 +135,9 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
151135
self.append_chat_history(
152136
{
153137
"role": "tool",
154-
"content": error_message,
155138
"tool_call_id": tool_id,
156139
"name": "execute_code",
140+
"content": error_message,
157141
}
158142
)
159143

@@ -178,9 +162,9 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
178162
self.append_chat_history(
179163
{
180164
"role": "tool",
181-
"content": text_to_gpt,
182165
"tool_call_id": tool_id,
183166
"name": "execute_code",
167+
"content": text_to_gpt,
184168
}
185169
)
186170

backend/app/core/agents/writer_agent.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ async def run(
150150
sub_title=sub_title,
151151
)
152152
response_content = next_response.choices[0].message.content
153-
main_text, footnotes = split_footnotes(response_content)
154-
logger.info(f"使用到的footnotes: {footnotes}")
155-
response_content = main_text
153+
# main_text, footnotes = split_footnotes(response_content)
156154
else:
157155
response_content = response.choices[0].message.content
158156
self.chat_history.append({"role": "assistant", "content": response_content})

backend/app/core/prompts.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,14 @@
8787
- Identify what's missing or wrong
8888
- Plan next steps
8989
- Continue execution until completion
90-
6. 你有能力在较少的步骤中完成任务,减少下一步操作和编排的任务轮次
90+
6. code step by step
9191
7. 如果一个任务反复无法完成,尝试切换路径、简化路径或直接跳过,千万别陷入反复重试,导致死循环
9292
8. Response in the same language as the user
9393
9. Remember save the output image to the working directory
9494
10. Remember to **print** the model evaluation results
9595
11. 保存的图片名称需要语义化,方便用户理解
9696
12. 在生成代码时,对于包含单引号的字符串,请使用双引号包裹,避免使用转义字符
97-
13. **你尽量在较少的对话轮次内完成任务。减少反复思考的次数**
98-
14. 在求解问题和建立模型**过程中**,进行充分可视化
97+
13. 在求解问题和建立模型**过程中**,进行充分可视化
9998
10099
101100
Important:
@@ -124,7 +123,7 @@ def get_writer_prompt(
124123
4. 严格按照参考用户输入的格式模板以及**正确的编号顺序**
125124
5. 不需要询问用户
126125
6. 当提到图片时,请使用提供的图片列表中的文件名
127-
7. when you write,check if you need to use tools search_papers to cite. if you need, markdown Footnote e.g.[^1]
126+
7. when you write,check if you need to use tools search_papers to cite. if you need, markdown footnotes e.g.[^1]paper cite
128127
8. List all references at the end in markdown footnote format. Dont't use title # ,just list in the end.
129128
9. Include an empty line between each citation for better readability.
130129
10. 对于问题背景和模型介绍,需查询文献调用tools search_papers
@@ -142,7 +141,7 @@ def get_reflection_prompt(error_message, code) -> str:
142141
3. Incorrect variable names or types
143142
4. File path issues
144143
5. Any other potential issues
145-
6. 如果一个任务反复无法完成,尝试切换路径、简化路径,千万别陷入反复重试,导致死循环
144+
6. 如果一个任务反复无法完成,尝试拆解代码、切换思路、简化模型。你要是做不出来,我砍 🪓 你,给你断电 😡
146145
7. Don't ask user any thing about how to do and next to do,just do it by yourself.
147146
148147
Previous code:

backend/app/models/user_output.py

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from app.utils.data_recorder import DataRecorder
44
from app.schemas.A2A import WriterResponse
55
from app.utils.log_util import logger
6+
from app.utils.common_utils import split_footnotes
7+
import json
68

79

810
class UserOutput:
@@ -42,101 +44,71 @@ def get_model_build_solve(self) -> str:
4244
return model_build_solve
4345

4446
def get_result_to_save(self, ques_count):
47+
# 保存 res.json 文件
48+
4549
logger.info(f"开始处理结果保存,问题数量: {ques_count}")
50+
4651
# 动态顺序获取拼接res value,正确拼接顺序
4752
ques_str = [f"ques{i}" for i in range(1, ques_count + 1)]
53+
54+
# 修改:调整章节顺序,确保符合论文结构
4855
seq = [
49-
"firstPage",
50-
"RepeatQues",
51-
"analysisQues",
52-
"modelAssumption",
53-
"symbol",
54-
"eda",
55-
*ques_str,
56-
"sensitivity_analysis",
57-
"judge",
56+
"firstPage", # 标题、摘要、关键词
57+
"RepeatQues", # 一、问题重述
58+
"analysisQues", # 二、问题分析
59+
"modelAssumption", # 三、模型假设
60+
"symbol", # 四、符号说明和数据预处理
61+
"eda", # 四、数据预处理(EDA部分)
62+
*ques_str, # 五、模型的建立与求解(问题1、2...)
63+
"sensitivity_analysis", # 六、模型的分析与检验
64+
"judge", # 七、模型的评价、改进与推广
5865
]
59-
logger.debug(f"处理序列: {seq}")
6066

61-
# 收集所有内容
67+
# 用于存储所有脚注
68+
all_footnotes: dict[str, str] = {}
69+
# 收集所有内容和处理脚注
6270
all_content = []
71+
6372
for key in seq:
6473
if key not in self.res:
6574
logger.debug(f"跳过不存在的键: {key}")
6675
continue
76+
6777
content = self.res[key]["response_content"]
68-
all_content.append(content)
78+
# 分离正文和脚注
79+
main_text, footnotes = split_footnotes(content)
80+
# 存储脚注内容(去重)
81+
for _, note_content in footnotes:
82+
all_footnotes[note_content] = note_content
83+
all_content.append(main_text)
6984

7085
# 合并所有内容
7186
full_content = "\n".join(all_content)
7287

73-
# 提取所有脚注引用 [^1], [^2] 等
74-
footnote_refs = re.findall(r"\[\^(\d+)\]", full_content)
75-
76-
# 提取所有脚注定义 [^1]: 内容
77-
footnote_defs = re.findall(
78-
r"\[\^(\d+)\]:\s*(.+?)(?=\n\[\^|\n\n|\Z)", full_content, re.DOTALL
79-
)
80-
81-
logger.info(f"找到脚注引用: {set(footnote_refs)}")
82-
logger.info(f"找到脚注定义: {[def_num for def_num, _ in footnote_defs]}")
83-
84-
# 创建脚注映射和内容
85-
footnote_mapping = {}
86-
footnote_contents = {}
87-
footnote_counter = 1
88-
89-
# 收集所有唯一的脚注编号(来自引用和定义)
90-
all_footnote_nums = set(footnote_refs)
91-
for def_num, def_content in footnote_defs:
92-
all_footnote_nums.add(def_num)
93-
footnote_contents[def_num] = def_content.strip()
94-
95-
# 为每个脚注分配新编号
96-
for old_num in sorted(all_footnote_nums, key=int):
97-
footnote_mapping[old_num] = str(footnote_counter)
98-
footnote_counter += 1
99-
100-
logger.info(f"脚注映射: {footnote_mapping}")
101-
102-
# 更新正文中的脚注引用编号
103-
processed_content = full_content
104-
for old_num, new_num in footnote_mapping.items():
105-
processed_content = processed_content.replace(
106-
f"[^{old_num}]", f"[^{new_num}]"
107-
)
108-
109-
# 移除原有的脚注定义(它们会被重新添加到最后)
110-
processed_content = re.sub(
111-
r"\[\^\d+\]:\s*.+?(?=\n\[\^|\n\n|\Z)",
112-
"",
113-
processed_content,
114-
flags=re.DOTALL,
115-
)
88+
# 重新编号脚注引用
89+
footnote_mapping = {} # 旧编号到新编号的映射
90+
for i, content in enumerate(all_footnotes.values(), 1):
91+
footnote_mapping[content] = str(i)
11692

117-
# 清理多余的空行
118-
processed_content = re.sub(r"\n{3,}", "\n\n", processed_content)
93+
# 更新正文中的脚注引用
94+
for old_content, new_num in footnote_mapping.items():
95+
# 在正文中查找并替换脚注引用
96+
pattern = r"\[\^\d+\]"
97+
# 只替换一次,确保引用的一致性
98+
full_content = re.sub(pattern, f"[^{new_num}]", full_content, count=1)
11999

120-
# 添加统一的参考文献部分
121-
if footnote_mapping:
122-
processed_content += "\n\n## 参考文献\n\n"
100+
# 添加重新编号后的脚注到文档末尾
101+
if all_footnotes:
102+
full_content += "\n\n## 参考文献\n\n"
103+
for content, num in footnote_mapping.items():
104+
full_content += f"[^{num}]: {content}\n\n"
123105

124-
# 按新编号顺序添加脚注
125-
for old_num in sorted(
126-
footnote_mapping.keys(), key=lambda x: int(footnote_mapping[x])
127-
):
128-
new_num = footnote_mapping[old_num]
129-
if old_num in footnote_contents:
130-
processed_content += f"[^{new_num}]: {footnote_contents[old_num]}\n"
131-
else:
132-
logger.warning(f"脚注 {old_num} 被引用但未找到定义")
133-
134-
logger.info(f"参考文献部分添加完成,共有 {len(footnote_mapping)} 个脚注")
135-
136-
logger.info(f"结果处理完成,最终内容长度: {len(processed_content)}")
137-
return processed_content
106+
return full_content
138107

139108
def save_result(self, ques_count):
109+
with open(os.path.join(self.work_dir, "res.json"), "w", encoding="utf-8") as f:
110+
json.dump(self.res, f, ensure_ascii=False, indent=4)
111+
140112
res_path = os.path.join(self.work_dir, "res.md")
141113
with open(res_path, "w", encoding="utf-8") as f:
142114
f.write(self.get_result_to_save(ques_count))

backend/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies = [
3131
"scipy>=1.15.2",
3232
"seaborn>=0.13.2",
3333
"semanticscholar>=0.10.0",
34+
"shap>=0.47.2",
3435
"statsmodels>=0.14.4",
3536
"uvicorn[standard]>=0.34.0",
3637
"websocket>=0.2.1",

0 commit comments

Comments
 (0)