-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMATH_script.py
More file actions
242 lines (205 loc) · 11.7 KB
/
MATH_script.py
File metadata and controls
242 lines (205 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import os
import openai
from openai import OpenAI
def call_llm(prompt, system_instruction=None):
# Set your API key (keep this safe and secure)
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Call the chat completion endpoint
response = client.responses.create(
model="gpt-4o-mini-2024-07-18",
input=[
{"role": "system", "content": system_instruction},
{"role": "user", "content": prompt}
]
)
# Print the response content
print(response.output[0].content[0].text)
return response.output[0].content[0].text
def main(question):
"""
This script implements a hybrid approach combining "Decompose-Solve-Verify" (from iteration 3)
with "Reflexion-Enhanced Solution Synthesis" (from iteration 6) for robust problem-solving.
It also incorporates Knowledge Retrieval (from iteration 4) for enhanced information gathering.
The hypothesis is that combining decomposition, reflection, and knowledge retrieval will lead to more accurate and reliable solutions.
"""
# === Step 1: Knowledge Retrieval (from iteration 4) ===
def retrieve_knowledge(question):
"""Retrieves relevant knowledge based on the question."""
system_instruction = "You are a knowledge retrieval expert. Identify key concepts and retrieve relevant information to solve the given problem."
prompt = f"""
Identify the mathematical concepts required to solve the following problem and retrieve relevant formulas or theorems.
Example 1:
Problem: What is the area of a circle with a radius of 5?
Concepts: Area of a circle
Retrieved Information: The area of a circle is given by the formula A = πr^2, where r is the radius.
Example 2:
Problem: Solve for x: 2x + 3 = 7
Concepts: Solving linear equations
Retrieved Information: To solve a linear equation, isolate the variable by performing inverse operations on both sides.
Example 3:
Problem: What are the prime factors of 36?
Concepts: Prime factorization
Retrieved Information: Prime factors are the prime numbers that divide evenly into a given number. To find them, repeatedly divide by prime numbers until you reach 1.
Problem: {question}
Concepts and Retrieved Information:
"""
try:
knowledge = call_llm(prompt, system_instruction)
print(f"Retrieved Knowledge: {knowledge}")
return knowledge
except Exception as e:
print(f"Error retrieving knowledge: {e}")
return "Error: Could not retrieve knowledge."
# === Step 2: Decompose the problem into smaller, manageable steps (from iteration 3) ===
def decompose_problem(question, knowledge):
"""Breaks down the problem into smaller steps, incorporating retrieved knowledge."""
system_instruction = "You are an expert at decomposing complex math problems into smaller, solvable steps. Use the provided knowledge to guide your decomposition."
prompt = f"""
Decompose the following math problem into smaller, manageable steps. Consider the retrieved knowledge to inform your decomposition.
Example 1:
Problem: What is the area of a square with side length 10, and what is the area if the side length is increased by 50%?
Retrieved Knowledge: Area of a square = side * side; Percentage increase = (new value - old value) / old value * 100
Decomposition:
1. Calculate the area of the square with side length 10 using the formula: side * side.
2. Calculate the new side length after increasing it by 50%.
3. Calculate the area of the square with the new side length using the formula: side * side.
Example 2:
Problem: A train travels at 60 mph for 2.5 hours. How far does it go and how much time is spent going the first half of the distance if the train travels at a constant velocity?
Retrieved Knowledge: Distance = speed * time
Decomposition:
1. Calculate the total distance traveled using the formula: speed * time.
2. Divide the total distance by 2 to find the first half distance.
3. Calculate the time spent for the first half using the formula: time = distance / speed.
Example 3:
Problem: Solve for x: 2x + 3 = 7
Retrieved Knowledge: To solve for x, isolate the variable on one side of the equation.
Decomposition:
1. Subtract 3 from both sides of the equation.
2. Divide both sides of the equation by 2.
Problem: {question}
Retrieved Knowledge: {knowledge}
Decomposition:
"""
return call_llm(prompt, system_instruction)
# === Step 3: Generate an Initial Solution (from iteration 6, modified) ===
def generate_initial_solution(question, decomposition):
"""Generates an initial solution to the problem based on the decomposition."""
system_instruction = "You are an expert problem solver. Generate a detailed initial solution based on the provided decomposition."
prompt = f"""
Provide a detailed initial solution to the following problem, following the steps outlined in the decomposition.
Example:
Problem: What is the area of a square with side length 5?
Decomposition: 1. Calculate the area of the square using the formula: side * side.
Solution: The area of a square is side * side. So, the area is 5 * 5 = 25.
Answer: 25
Example:
Problem: Solve for x: 2x + 3 = 7
Decomposition: 1. Subtract 3 from both sides of the equation. 2. Divide both sides of the equation by 2.
Solution: Subtracting 3 from both sides gives 2x = 4. Dividing both sides by 2 gives x = 2.
Answer: x = 2
Problem: {question}
Decomposition: {decomposition}
Solution:
"""
try:
solution = call_llm(prompt, system_instruction)
print(f"Initial Solution: {solution}")
return solution
except Exception as e:
print(f"Error generating initial solution: {e}")
return "Error: Could not generate initial solution."
# === Step 4: Reflect and Critique (from iteration 6) ===
def reflect_and_critique(question, solution):
"""Reflects on the initial solution and identifies potential issues."""
system_instruction = "You are a critical self-evaluator. Analyze the solution and identify any potential errors, inconsistencies, or areas for improvement. Focus especially on arithmetic errors."
prompt = f"""
Analyze the following solution to the problem and identify any potential errors or inconsistencies. Be extremely thorough, especially looking for arithmetic mistakes.
Example:
Problem: What is the area of a circle with radius 5?
Solution: The area of a circle is radius * radius. So, the area is 5 * 5 = 25.
Critique: The solution incorrectly states the formula for the area of a circle. It should be pi * radius^2. The arithmetic is correct, but the underlying formula is wrong.
Example:
Problem: Solve for x: 2x + 3 = 7
Solution: Subtract 3 from both sides gives 2x = 10. Dividing both sides by 2 gives x = 5.
Critique: There is an arithmetic error. Subtracting 3 from 7 should result in 4, not 10. The solution should be x=2.
Problem: {question}
Solution: {solution}
Critique:
"""
try:
critique = call_llm(prompt, system_instruction)
print(f"Critique: {critique}")
return critique
except Exception as e:
print(f"Error generating critique: {e}")
return "Error: Could not generate critique."
# === Step 5: Synthesize Refined Solution (from iteration 6, modified) ===
def synthesize_refined_solution(question, initial_solution, critique):
"""Synthesizes a refined solution based on the initial solution and the critique."""
system_instruction = "You are an expert problem solver. Use the critique to generate a refined solution, correcting any identified errors."
prompt = f"""
Based on the critique, generate a refined solution to the problem. Incorporate all feedback to ensure correctness.
Example:
Problem: What is the area of a circle with radius 5?
Initial Solution: The area of a circle is radius * radius. So, the area is 5 * 5 = 25.
Critique: The solution incorrectly states the formula for the area of a circle. It should be pi * radius^2.
Refined Solution: The area of a circle is pi * radius^2. So, the area is pi * 5^2 = 25pi.
Answer: 25pi
Example:
Problem: Solve for x: 2x + 3 = 7
Initial Solution: Subtract 3 from both sides gives 2x = 10. Dividing both sides by 2 gives x = 5.
Critique: There is an arithmetic error. Subtracting 3 from 7 should result in 4, not 10.
Refined Solution: Subtracting 3 from both sides gives 2x = 4. Dividing both sides by 2 gives x = 2.
Answer: x = 2
Problem: {question}
Initial Solution: {initial_solution}
Critique: {critique}
Refined Solution:
"""
try:
refined_solution = call_llm(prompt, system_instruction)
print(f"Refined Solution: {refined_solution}")
return refined_solution
except Exception as e:
print(f"Error generating refined solution: {e}")
return "Error: Could not generate refined solution."
# === Step 6: Verify Refined Solution (from iteration 3, modified) ===
def verify_refined_solution(question, refined_solution):
"""Verifies if the refined solution correctly addresses the problem. Focus on arithmetic and logical consistency."""
system_instruction = "You are a solution validator. Check the solution for correctness and completeness. Double-check all calculations and logical steps."
prompt = f"""
Validate the refined solution for correctness and completeness. Be extremely thorough, focusing on both arithmetic and logical consistency. Provide a detailed assessment.
Example 1:
Question: What is 2 + 2?
Solution: 4
Verdict: Correct. The solution is correct and complete.
Example 2:
Question: What is the capital of France?
Solution: London
Verdict: Incorrect. The solution is incorrect. The capital of France is Paris.
Example 3:
Question: What is the area of a square with side length 5?
Solution: The area of a square is side * side. So, the area is 5 * 5 = 25.
Verdict: Correct.
Question: {question}
Solution: {refined_solution}
Verdict:
"""
try:
validation = call_llm(prompt, system_instruction)
print(f"Validation: {validation}")
return validation
except Exception as e:
print(f"Error validating solution: {e}")
return "Error: Could not validate the solution."
# Orchestrate the process
try:
knowledge = retrieve_knowledge(question)
decomposition = decompose_problem(question, knowledge)
initial_solution = generate_initial_solution(question, decomposition)
critique = reflect_and_critique(question, initial_solution)
refined_solution = synthesize_refined_solution(question, initial_solution, critique)
validation_result = verify_refined_solution(question, refined_solution)
return f"Knowledge: {knowledge}\nDecomposition: {decomposition}\nInitial Solution: {initial_solution}\nCritique: {critique}\nRefined Solution: {refined_solution}\nValidation: {validation_result}"
except Exception as e:
return f"Overall Error: {str(e)}"