Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
didiforgithub committed Apr 11, 2024
1 parent 7f359ea commit ef8d266
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 52 deletions.
36 changes: 22 additions & 14 deletions math_ai/codebase/math_resovler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,43 @@ async def single_run(self, problem: Dict, types: Dict) -> Dict:
Finally, math resolver need to return the solution without refine.
"""
strategy_name = types["strategy"]
type_decompose = "多个问题" if types["if_muti"] == "muti" else "simple"
strategy = get_strategy_desc(strategy_name)

# TODO 存储 Solution 的地方要做一个修改
# TODO 对于这个 modify 之后的 plan 做重新执行
# 1. 直接要求他解决数学问题,思考这个过程。 zero shot 让他先去对这个题目给出一个计划。
# 2. 得到这个过程之后,让他结合我们的strategy 跟 Prompt,重新构建phase
# 3. 每一个Phase的Prompt如何去写

origin_plan = self.llm.llm_response(prompt=zero_shot_planner.format(problem_desc=problem["desc"]),json_mode=True)
resolver_plan = self.llm.llm_response(prompt=resolver_planner.format(problem_desc=problem["desc"], strategy=strategy, origin_plan=origin_plan), json_mode=True)

current_trajectory = ""
for phase in resolver_plan:
if phase["plan"]["phase"] == "inference":
current_trajectory += self.inference(problem, current_trajectory, subgoal=phase["plan"]["desc"])
elif phase["plan"]["phase"] == "di":
current_trajectory += self.di_run(problem, current_trajectory, subgoal=phase["plan"]["desc"])
elif phase["plan"]["phase"] == "logic_validate":
current_trajectory += self.logic_validate(problem, current_trajectory, subgoal=phase["plan"]["desc"])
resolver_plan = self.llm.llm_response(prompt=resolver_planner.format(problem_desc=problem["desc"], strategy=strategy, origin_plan=origin_plan, type_decompose=type_decompose, type_problem=problem["type"]), json_mode=True)

current_trajectory = []
for index, phase in enumerate(resolver_plan["plan"]):
if phase["phase"] == "inference":
answer = self.inference(problem, current_trajectory, subgoal=phase["desc"])
current_trajectory.append({"plan":phase["desc"],"reason":phase["reason"],"answer":answer})
elif phase["phase"] == "di":
answer = self.di_run(problem, current_trajectory, subgoal=phase["desc"])
current_trajectory.append({"plan":phase["desc"],"reason":phase["reason"],"answer":answer})
elif phase["phase"] == "logic_validate":
result = self.logic_validate(problem, current_trajectory, subgoal=phase["desc"])
if result["judge"]:
current_trajectory.append({"plan":phase["desc"],"reason":phase["reason"],"answer":"逻辑正确"})
else:
# TODO 如果不是,修改之后的代码
# TODO 这里考虑修改一下Logic Validate 的逻辑,修改为添加一个纠正错误的修改
current_trajectory.append({"plan":phase["desc"],"reason":phase["reason"],"answer":result["reflection"]})

if self.result_validate(problem, current_trajectory):
pass
else:
current_trajectory += self.inference_final(problem, current_trajectory)
current_trajectory.append(self.inference_final(problem, current_trajectory))
return {"current_trajectory": current_trajectory}

async def multi_run(self):
pass

async def di_run(self, problem, current_trajectory, subgoal):
# TODO 黄毅把获取的结果放到这里,我来写协程
DI = DataInterpreter()
record = await DI.run(di_prompt.format(problem=problem, trajectory=current_trajectory, subgoal=subgoal))
return record
Expand Down
42 changes: 12 additions & 30 deletions math_ai/codebase/prompt.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@

zero_shot_planner = """
cot 大模型本身能力解题
你是全球最杰出的数学竞赛选手,擅长一步步的解决复杂的数学问题。
现在,你面临的问题是{problem},请你尝试为解决这一问题提供基础的推理思路。
"""

resolver_planner = """
你是全球最杰出的数学竞赛选手,擅长将错综复杂的问题分解成一个个可管理的子问题。
在解题的过程中,你一般会基于你常用的解题策略将复杂的问题,分解形成一个个小问题,并结合你的策略进行解决。
你的解题策略有以下几种:
精准执行:利用你的思维工具 - “数据解析器”,当选择“精准执行”作为下一步的思考策略,你可以将子问题传递给此工具,由它生成可以执行任务的代码,并返回执行结果以及工具的思考过程。
逻辑检验:检查当前结果与过去规划的逻辑,思考是否需要修改计划。
子任务合并:将所有已完成的子任务有序合并,逐步接近问题的解。
结束任务:最后,你将验证整个解题过程和答案的正确性,确认任务的最终完成。
inference:当你认为某个子问题无需复杂的代码建模与精确计算,而是更多依赖于逻辑推理时,选择这一策略。
di:当你认为某个子问题需要精准的代码建模与计算时,简单的逻辑推理用处不大时,选择这一策略。
logic_validate:当你认为某个子问题是对过去的推理进行逻辑上的验证,以判断其轨迹是否合理时,选择这一策略。
你面临的问题是{problem_desc}
你面临的问题是{problem_desc},这一题目存在{type_decompose}。这一题目的类型为{type_problem}
一个可以参考的策略生成逻辑是{strategy}。
针对这一问题,一个基础的Plan{origin_plan}
现在,你需要基于你的问题,与你的解题策略,生成一个针对这一问题的解题规划与原因。解题规划是一个列表,其中的元素是一个字典,包含两个键,一个为desc,也就是你生成的子任务的描述,一个为phase,也就是你认为这个子任务的生成是基于什么策略的。
现在,你需要基于你的问题,与你的解题策略,生成一个针对这一问题的解题规划与原因。解题规划是一个列表,其中的元素是一个字典,包含两个键,一个为desc,也就是你生成的子任务的描述,一个为phase,也就是你认为这个子任务的生成基于什么策略。
最终结果,请你使用JSON格式进行返回,一个可以参考的格式如下:
{{
"plan": <[{{"desc":"", "phase":""}}]>,
"reason": <"reason">
"plan": <[{{"desc":"<>", "phase":"<>", "reason":"<>"}}]>
}}
"""

Expand All @@ -43,28 +40,14 @@
你是全球最杰出的数学竞赛教练,你需要利用你对解题整体流程的理解,检查你的学生的解题过程,确保他们的解题过程在逻辑上是合理的。
你的学生已经完成了上游的一些推理,这些推理的过程结果被记录成推理轨迹,轨迹轨迹如下
{trajectory}
现在你需要对其进行逻辑验证,确保其推理过程是合理的。
现在,你需要基于你学生的推理轨迹,给出你的判断和修改版本。判断是一个字符串,如果你认为这个推理过程是合理的,请填写"True",否则请填写"False"。修改版本是一个字符串,描述你对于这个推理过程的修改意见。修改版本是经过你的修改后的推理轨迹,如果你认为这个推理过程是合理的,请直接返回空字符串""。
现在你需要对其进行逻辑验证,逻辑验证的目标是{subgoal},你需要对这一逻辑验证的目标进行验证,或对错误的逻辑提供修改意见。
现在,你需要基于你学生的推理轨迹,给出你的判断和修改版本。判断是一个字符串,如果你认为这个推理过程是合理的,请填写"True",否则请填写"False"。
对后续推理的意见是一个字符串,描述你对于这个推理过程的修改意见。如果你认为这个推理过程是合理的,请直接返回空字符串""。
对推理轨迹的修改规则如下:
假设当前推理轨迹是
{{
{{"plan": <"plan1">, "reason": <"reason1">, "answer": <"answer1">}},
{{"plan": <"plan2">, "reason": <"reason2">, "answer": <"answer2">}},
...
{{"plan": <"plann">, "reason": <"reasonn">, "answer": <"answern">}}
}}
如果你认为这个推理过程是不合理的,请你保留合理的部分序列,然后修改第一个不合理的planx(x=1,...,n),将其修改为一个合理的planx_modified,包括reason_modified和answer_modified,裁断planx后续的序列planx+1,...,plann,然后将修改后的推理轨迹返回。
比如,如果你认为第七个plan是不合理的,那么你可以将推理轨迹修改为
{{
{{"plan": <"plan1">, "reason": <"reason1">, "answer": <"answer1">}},
...
{{"plan": <"plan6">, "reason": <"reason6">, "answer": <"answer6">}},
{{"plan": <"plan7_modified">, "reason": <"reason7_modified">, "answer": <"answer7_modified">}}
}}
最终结果,请你使用JSON格式进行返回,一个可以参考的格式如下:
{{
"judge": <"judge">,
"modified_trajectory": <"modified_trajectory">
"reflection": <"reflection on past inference">
}}
"""

Expand Down Expand Up @@ -97,4 +80,3 @@
请你注意,在规划的过程中,需要专注于子问题,大问题只是用来给你提供更多的背景信息。
"""


17 changes: 9 additions & 8 deletions math_ai/codebase/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Description: run math ai
import sys
import os

import asyncio
# add metagpt rootpath to syspath
meta_rootpath = os.getcwd()
if meta_rootpath not in sys.path:
Expand All @@ -15,28 +15,29 @@
from math_ai.codebase.math_resovler import MathResolver
from math_ai.codebase.data_processer import DataProcesser
from math_ai.codebase.solution_refiner import SolutionRefiner
from metagpt.roles.di.data_interpreter import DataInterpreter


def solution(question_path:str):
async def solution(question_path: str):
final_solutions = []

dp = DataProcesser()
gc = GateController()
mr = MathResolver()
sr = SolutionRefiner()

problem_dict_list = dp.run(question_path) # List[Dict]
problem_dict_list = dp.run(question_path) # List[Dict]
for problem_dict in problem_dict_list:
strategy_dict = gc.run(problem_dict)
first_solution = mr.run(problem_dict, strategy_dict)
first_solution = await mr.single_run(problem_dict, strategy_dict) # 使用 await
final_solution = sr.run(problem_dict, strategy_dict, first_solution)

final_solutions.append(final_solution)


return final_solutions

async def main(question_path: str):
solutions = await solution(question_path)
print(solutions)

if __name__ == '__main__':
solution('H:/Hack/Ali/dataset/2021.json')
question_path = '/Users/mac/Github_project/MathAI/math_ai/dataset/dataset.json'
asyncio.run(main(question_path))

0 comments on commit ef8d266

Please sign in to comment.