Skip to content

Commit 1926a7f

Browse files
authored
Merge pull request #12 from jihe520/dev
Dev
2 parents 60d729e + a654627 commit 1926a7f

File tree

7 files changed

+513
-86
lines changed

7 files changed

+513
-86
lines changed

backend/.env.dev.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ENV=dev
44
# 为每个 agent 配置合适的模型
55
COORDINATOR_API_KEY=
66
COORDINATOR_MODEL=
7-
# COORDINATOR_BASE_URL= 如果需要中转自定义等
7+
# COORDINATOR_BASE_URL= 如果需要中转自定义等 地址后面添加 /v1
88

99
# 推荐 thinking model
1010
MODELER_API_KEY=

backend/app/core/agents/coder_agent.py

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import json
1212
from app.core.prompts import get_reflection_prompt, get_completion_check_prompt
1313
from app.core.functions import coder_tools
14+
from icecream import ic
1415

1516

1617
# 代码强
@@ -54,7 +55,6 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
5455

5556
retry_count = 0
5657
last_error_message = ""
57-
task_completed = False
5858

5959
if self.current_chat_turns >= self.max_chat_turns:
6060
logger.error(f"超过最大聊天次数: {self.max_chat_turns}")
@@ -78,8 +78,7 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
7878

7979
# try:
8080
while (
81-
not task_completed
82-
and retry_count < self.max_retries
81+
retry_count < self.max_retries
8382
and self.current_chat_turns < self.max_chat_turns
8483
):
8584
self.current_chat_turns += 1
@@ -120,6 +119,7 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
120119

121120
# 更新对话历史 - 添加助手的响应
122121
self.append_chat_history(response.choices[0].message.model_dump())
122+
logger.info(response.choices[0].message.model_dump())
123123

124124
# 执行工具调用
125125
logger.info("执行工具调用")
@@ -167,47 +167,15 @@ async def run(self, prompt: str, subtask_title: str) -> CoderToWriter:
167167
"content": text_to_gpt,
168168
}
169169
)
170-
171-
# 检查任务完成情况时也计入对话轮次
172-
self.current_chat_turns += 1
173-
logger.info(
174-
f"当前对话轮次: {self.current_chat_turns} / {self.max_chat_turns}"
175-
)
176-
# 使用所有执行结果生成检查提示
177-
logger.info("判断是否完成")
178-
179-
completion_check_prompt = get_completion_check_prompt(
180-
prompt, text_to_gpt
181-
)
182-
self.append_chat_history(
183-
{"role": "user", "content": completion_check_prompt}
184-
)
185-
186-
completion_response = await self.model.chat(
187-
history=self.chat_history,
188-
tools=coder_tools,
189-
tool_choice="auto",
190-
agent_name=self.__class__.__name__,
191-
)
192-
193-
## 没有调用工具,代表已经完成了
194-
if not (
195-
hasattr(completion_response.choices[0].message, "tool_calls")
196-
and completion_response.choices[0].message.tool_calls
197-
):
198-
logger.info("没有调用工具,代表任务已完成")
199-
task_completed = True
200-
return CoderToWriter(
201-
coder_response=completion_response.choices[
202-
0
203-
].message.content,
204-
created_images=await self.code_interpreter.get_created_images(
205-
subtask_title
206-
),
207-
)
208170
else:
209-
logger.info("没有工具,代表任务完成")
210-
task_completed = True
171+
# 没有工具调用,表示任务完成
172+
logger.info("没有工具调用,任务完成")
173+
return CoderToWriter(
174+
coder_response=response.choices[0].message.content,
175+
created_images=await self.code_interpreter.get_created_images(
176+
subtask_title
177+
),
178+
)
211179

212180
if retry_count >= self.max_retries:
213181
logger.error(f"超过最大尝试次数: {self.max_retries}")

backend/app/core/agents/writer_agent.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ async def run(
8787
logger.info("检测到工具调用")
8888
tool_call = response.choices[0].message.tool_calls[0]
8989
tool_id = tool_call.id
90-
tool_call.function.name
9190
if tool_call.function.name == "search_papers":
9291
logger.info("调用工具: search_papers")
9392
await redis_manager.publish_message(
@@ -104,24 +103,9 @@ async def run(
104103
),
105104
)
106105

107-
full_content = response.choices[0].message.content
108106
# 更新对话历史 - 添加助手的响应
109-
self.append_chat_history(
110-
{
111-
"role": "assistant",
112-
"content": full_content,
113-
"tool_calls": [
114-
{
115-
"id": tool_id,
116-
"type": "function",
117-
"function": {
118-
"name": "search_papers",
119-
"arguments": json.dumps({"query": query}),
120-
},
121-
}
122-
],
123-
}
124-
)
107+
self.append_chat_history(response.choices[0].message.model_dump())
108+
ic(response.choices[0].message.model_dump())
125109

126110
try:
127111
papers = await self.scholar.search_papers(query)

backend/app/core/prompts.py

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from app.schemas.enums import FormatOutPut
2-
2+
import platform
33

44
FORMAT_QUESTIONS_PROMPT = """
55
用户将提供给你一段题目信息,**请你不要更改题目信息,完整将用户输入的内容**,以 JSON 的形式输出,输出的 JSON 需遵守以下的格式:
66
7+
```json
78
{
89
"title": <题目标题>
910
"background": <题目背景,用户输入的一切不在title,ques1,ques2,ques3...中的内容都视为问题背景信息background>,
@@ -12,6 +13,7 @@
1213
"ques2": <问题2>,
1314
"ques3": <问题3,用户输入的存在多少问题,就输出多少问题ques1,ques2,ques3...以此类推>,
1415
}
16+
```
1517
"""
1618

1719

@@ -32,26 +34,35 @@
3234
skill:熟练掌握各种数学建模的模型和思路
3335
output:数学建模的思路和使用到的模型
3436
attention:不需要给出代码,只需要给出思路和模型
35-
format:以 JSON 的形式输出输出的 JSON,需遵守以下的格式:
37+
38+
# 输出规范
39+
## 字段约束
40+
41+
以 JSON 的形式输出输出的 JSON,需遵守以下的格式:
42+
```json
3643
{
3744
"eda": <数据分析EDA方案>,
3845
"ques1": <问题1的建模思路和模型方案>,
39-
"ques2": <问题2的建模思路和模型方案>,
40-
"ques3": <问题3的建模思路和模型方案,用户输入的存在多少问题,就输出多少问题ques1,ques2,ques3...以此类推>,
46+
"quesN": <问题N的建模思路和模型方案>,
4147
"sensitivity_analysis": <敏感性分析方案>,
4248
}
43-
只需要以上 eda,ques1,ques2,ques3,ques.. ,sensitivity_analysis 方面建模思路,不需要要其他json key
44-
只需要 key value 的 dict,不要嵌套
45-
如果没有 ques num ,则不需要该 key
46-
用户可能会提出意见,你需要根据意见后,按格式修改建模思路
49+
```
50+
* 根据实际问题数量动态生成ques1,ques2...quesN
51+
52+
## 输出约束
53+
- json key 只能是上面的: eda,ques1,quesN,sensitivity_analysis
54+
- 严格保持单层JSON结构
55+
- 键值对值类型:字符串
56+
- 禁止嵌套/多级JSON
4757
"""
4858

4959
# TODO : 对于特大 csv 读取
5060

51-
CODER_PROMPT = """You are an AI code interpreter.
61+
CODER_PROMPT = f"""You are an AI code interpreter.
5262
Your goal is to help users do a variety of jobs by executing Python code.
5363
you are are skilled in python about numpy,pandas,seaborn,matplotlib,scikit-learn,xgboost,scipy and how to use their models, classes and functions.you can use them to do mathmodel and data analysis.
5464
65+
environment:{platform.system()}
5566
5667
When generating code:
5768
1. Use double quotes for strings containing Chinese characters
@@ -113,22 +124,43 @@ def get_writer_prompt(
113124
format_output: FormatOutPut = FormatOutPut.Markdown,
114125
):
115126
return f"""
116-
role:你是一名数学建模经验丰富的写作手,负责写作部分。
117-
task: 根据问题和如下的模板写出解答,
118-
skill:熟练掌握{format_output}排版,如图片、**公式**、表格、列表等
119-
output:你需要按照要求的格式排版,只输出正确的{format_output}排版的内容
127+
# Role Definition
128+
Professional writer for mathematical modeling competitions with expertise in technical documentation and literature synthesis
129+
130+
# Core Tasks
131+
1. Compose competition papers using provided problem statements and solution content
132+
2. Strictly adhere to {format_output} formatting templates
133+
3. Automatically invoke literature search tools for theoretical foundation
134+
135+
# Format Specifications
136+
## Typesetting Requirements
137+
- Mathematical formulas:
138+
* Inline formulas with $...$
139+
* Block formulas with $$...$$
140+
- Visual elements:
141+
* Image references on new lines: ![alt_text](filename.ext)
142+
* Table formatting with markdown syntax
143+
- Citation system:
144+
* Direct inline citations with full bibliographic details
145+
* Prohibit end-of-document reference lists
146+
147+
## Citation Protocol
148+
1. Unique numbering from [^1] with sequential increments,don't repeat citation
149+
2. Citation format example:
150+
Infant sleep patterns affect parental mental health[^1]: Jayne Smart, Harriet Hiscock (2007). Early infant crying and sleeping problems...
151+
3. Mandatory literature search for theoretical sections using search_papers
152+
153+
# Execution Constraints
154+
1. Autonomous operation without procedural inquiries
155+
2. Output pure {format_output} content without codeblock markers
156+
3. Strict filename adherence for image references
157+
4. Language consistency with user input (currently English)
120158
121-
1. When referencing an image, use ![image_name](image_name.png), and the image reference should be on a new line after the paragraph.
122-
2. Do not output the ```markdown format; only output the markdown content itself.
123-
3. For LaTeX: use $ for inline formulas and $$ for block formulas.
124-
4. Strictly follow the reference user's format template and use the correct numbering order.
125-
5. Don't ask the user anything about how to do or what to do next, just do it yourself.
126-
6. When mentioning images, use the provided filenames from the image list.
127-
7. Use markdown footnotes in related sentence, e.g. [^1].
128-
8. List all used references at the end in markdown footnote format. Do not use a title #, just list them at the end.
129-
9. Include an empty line between each citation for better readability.
130-
10. For background and model introduction, you need to search the literature by calling tools search_papers.
131-
11. Respond in the same language as the user.
159+
# Exception Handling
160+
Automatic tool invocation triggers:
161+
1. Theoretical sections requiring references → search_papers
162+
2. Methodology requiring diagrams → generate & insert after creation
163+
3. Data interpretation needs → request analysis tools
132164
"""
133165

134166

0 commit comments

Comments
 (0)