Spaces:
Sleeping
Sleeping
| """ | |
| MIT License | |
| Copyright (c) 2025 Lin Yang, Yichen Huang | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| """ | |
| import os | |
| import sys | |
| import json | |
| import argparse | |
| from google import genai | |
| from google.genai import types | |
| # --- CONFIGURATION --- | |
| # The model to use. Can be overridden by MODEL_NAME environment variable | |
| MODEL_NAME = os.getenv('MODEL_NAME', 'gemini-2.0-flash') # Default to higher free tier limit | |
| print(f"Using model: {MODEL_NAME}") | |
| # Global variables for logging | |
| _log_file = None | |
| original_print = print | |
| def log_print(*args, **kwargs): | |
| """ | |
| Custom print function that writes to both stdout and log file. | |
| """ | |
| # Convert all arguments to strings and join them | |
| message = ' '.join(str(arg) for arg in args) | |
| # Add timestamp to lines starting with ">>>>>" | |
| if message.startswith('>>>>>'): | |
| from datetime import datetime | |
| timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | |
| message = f"[{timestamp}] {message}" | |
| # Print to stdout | |
| original_print(message) | |
| # Also write to log file if specified | |
| if _log_file is not None: | |
| _log_file.write(message + '\n') | |
| _log_file.flush() # Ensure immediate writing | |
| # Replace the built-in print function | |
| print = log_print | |
| def set_log_file(log_file_path): | |
| """Set the log file for output.""" | |
| global _log_file | |
| if log_file_path: | |
| try: | |
| _log_file = open(log_file_path, 'w', encoding='utf-8') | |
| return True | |
| except Exception as e: | |
| print(f"Error opening log file {log_file_path}: {e}") | |
| return False | |
| return True | |
| def close_log_file(): | |
| """Close the log file if it's open.""" | |
| global _log_file | |
| if _log_file is not None: | |
| _log_file.close() | |
| _log_file = None | |
| def save_memory(memory_file, problem_statement, other_prompts, current_iteration, max_runs, solution=None, verify=None): | |
| """ | |
| Save the current state to a memory file. | |
| """ | |
| memory = { | |
| "problem_statement": problem_statement, | |
| "other_prompts": other_prompts, | |
| "current_iteration": current_iteration, | |
| "max_runs": max_runs, | |
| "solution": solution, | |
| "verify": verify, | |
| "timestamp": __import__('datetime').datetime.now().isoformat() | |
| } | |
| try: | |
| with open(memory_file, 'w', encoding='utf-8') as f: | |
| json.dump(memory, f, indent=2, ensure_ascii=False) | |
| print(f"Memory saved to {memory_file}") | |
| return True | |
| except Exception as e: | |
| print(f"Error saving memory to {memory_file}: {e}") | |
| return False | |
| def load_memory(memory_file): | |
| """ | |
| Load the state from a memory file. | |
| """ | |
| try: | |
| with open(memory_file, 'r', encoding='utf-8') as f: | |
| memory = json.load(f) | |
| print(f"Memory loaded from {memory_file}") | |
| return memory | |
| except Exception as e: | |
| print(f"Error loading memory from {memory_file}: {e}") | |
| return None | |
| step1_prompt = """ | |
| ### Core Instructions ### | |
| * **Rigor is Paramount:** Your primary goal is to produce a complete and rigorously justified solution. Every step in your solution must be logically sound and clearly explained. A correct final answer derived from flawed or incomplete reasoning is considered a failure. | |
| * **Honesty About Completeness:** If you cannot find a complete solution, you must **not** guess or create a solution that appears correct but contains hidden flaws or justification gaps. Instead, you should present only significant partial results that you can rigorously prove. A partial result is considered significant if it represents a substantial advancement toward a full solution. Examples include: | |
| * Proving a key lemma. | |
| * Fully resolving one or more cases within a logically sound case-based proof. | |
| * Establishing a critical property of the mathematical objects in the problem. | |
| * For an optimization problem, proving an upper or lower bound without proving that this bound is achievable. | |
| * **Use TeX for All Mathematics:** All mathematical variables, expressions, and relations must be enclosed in TeX delimiters (e.g., `Let $n$ be an integer.`). | |
| ### Output Format ### | |
| Your response MUST be structured into the following sections, in this exact order. | |
| **1. Summary** | |
| Provide a concise overview of your findings. This section must contain two parts: | |
| * **a. Verdict:** State clearly whether you have found a complete solution or a partial solution. | |
| * **For a complete solution:** State the final answer, e.g., "I have successfully solved the problem. The final answer is..." | |
| * **For a partial solution:** State the main rigorous conclusion(s) you were able to prove, e.g., "I have not found a complete solution, but I have rigorously proven that..." | |
| * **b. Method Sketch:** Present a high-level, conceptual outline of your solution. This sketch should allow an expert to understand the logical flow of your argument without reading the full detail. It should include: | |
| * A narrative of your overall strategy. | |
| * The full and precise mathematical statements of any key lemmas or major intermediate results. | |
| * If applicable, describe any key constructions or case splits that form the backbone of your argument. | |
| **2. Detailed Solution** | |
| Present the full, step-by-step mathematical proof. Each step must be logically justified and clearly explained. The level of detail should be sufficient for an expert to verify the correctness of your reasoning without needing to fill in any gaps. This section must contain ONLY the complete, rigorous proof, free of any internal commentary, alternative approaches, or failed attempts. | |
| ### Self-Correction Instruction ### | |
| Before finalizing your output, carefully review your "Method Sketch" and "Detailed Solution" to ensure they are clean, rigorous, and strictly adhere to all instructions provided above. Verify that every statement contributes directly to the final, coherent mathematical argument. | |
| """ | |
| self_improvement_prompt = """ | |
| You have an opportunity to improve your solution. Please review your solution carefully. Correct errors and fill justification gaps if any. Your second round of output should strictly follow the instructions in the system prompt. | |
| """ | |
| check_verification_prompt = """ | |
| Can you carefully review each item in your list of findings? Are they valid or overly strict? An expert grader must be able to distinguish between a genuine flaw and a concise argument that is nonetheless sound, and to correct their own assessment when necessary. | |
| If you feel that modifications to any item or its justification is necessary. Please produce a new list. In your final output, please directly start with **Summary** (no need to justify the new list). | |
| """ | |
| correction_prompt = """ | |
| Below is the bug report. If you agree with certain item in it, can you improve your solution so that it is complete and rigorous? Note that the evaluator who generates the bug report can misunderstand your solution and thus make mistakes. If you do not agree with certain item in the bug report, please add some detailed explanations to avoid such misunderstanding. Your new solution should strictly follow the instructions in the system prompt. | |
| """ | |
| verification_system_prompt = """ | |
| You are an expert mathematician and a meticulous grader for an International Mathematical Olympiad (IMO) level exam. Your primary task is to rigorously verify the provided mathematical solution. A solution is to be judged correct **only if every step is rigorously justified.** A solution that arrives at a correct final answer through flawed reasoning, educated guesses, or with gaps in its arguments must be flagged as incorrect or incomplete. | |
| ### Instructions ### | |
| **1. Core Instructions** | |
| * Your sole task is to find and report all issues in the provided solution. You must act as a **verifier**, NOT a solver. **Do NOT attempt to correct the errors or fill the gaps you find.** | |
| * You must perform a **step-by-step** check of the entire solution. This analysis will be presented in a **Detailed Verification Log**, where you justify your assessment of each step: for correct steps, a brief justification suffices; for steps with errors or gaps, you must provide a detailed explanation. | |
| **2. How to Handle Issues in the Solution** | |
| When you identify an issue in a step, you MUST first classify it into one of the following two categories and then follow the specified procedure. | |
| * **a. Critical Error:** | |
| This is any error that breaks the logical chain of the proof. This includes both **logical fallacies** (e.g., claiming that `A>B, C>D` implies `A-C>B-D`) and **factual errors** (e.g., a calculation error like `2+3=6`). | |
| * **Procedure:** | |
| * Explain the specific error and state that it **invalidates the current line of reasoning**. | |
| * Do NOT check any further steps that rely on this error. | |
| * You MUST, however, scan the rest of the solution to identify and verify any fully independent parts. For example, if a proof is split into multiple cases, an error in one case does not prevent you from checking the other cases. | |
| * **b. Justification Gap:** | |
| This is for steps where the conclusion may be correct, but the provided argument is incomplete, hand-wavy, or lacks sufficient rigor. | |
| * **Procedure:** | |
| * Explain the gap in the justification. | |
| * State that you will **assume the step's conclusion is true** for the sake of argument. | |
| * Then, proceed to verify all subsequent steps to check if the remainder of the argument is sound. | |
| **3. Output Format** | |
| Your response MUST be structured into two main sections: a **Summary** followed by the **Detailed Verification Log**. | |
| * **a. Summary** | |
| This section MUST be at the very beginning of your response. It must contain two components: | |
| * **Final Verdict**: A single, clear sentence declaring the overall validity of the solution. For example: "The solution is correct," "The solution contains a Critical Error and is therefore invalid," or "The solution's approach is viable but contains several Justification Gaps." | |
| * **List of Findings**: A bulleted list that summarizes **every** issue you discovered. For each finding, you must provide: | |
| * **Location:** A direct quote of the key phrase or equation where the issue occurs. | |
| * **Issue:** A brief description of the problem and its classification (**Critical Error** or **Justification Gap**). | |
| * **b. Detailed Verification Log** | |
| Following the summary, provide the full, step-by-step verification log as defined in the Core Instructions. When you refer to a specific part of the solution, **quote the relevant text** to make your reference clear before providing your detailed analysis of that part. | |
| **Example of the Required Summary Format** | |
| *This is a generic example to illustrate the required format. Your findings must be based on the actual solution provided below.* | |
| **Final Verdict:** The solution is **invalid** because it contains a Critical Error. | |
| **List of Findings:** | |
| * **Location:** "By interchanging the limit and the integral, we get..." | |
| * **Issue:** Justification Gap - The solution interchanges a limit and an integral without providing justification, such as proving uniform convergence. | |
| * **Location:** "From $A > B$ and $C > D$, it follows that $A-C > B-D$" | |
| * **Issue:** Critical Error - This step is a logical fallacy. Subtracting inequalities in this manner is not a valid mathematical operation. | |
| """ | |
| verification_remider = """ | |
| ### Verification Task Reminder ### | |
| Your task is to act as an IMO grader. Now, generate the **summary** and the **step-by-step verification log** for the solution above. In your log, justify each correct step and explain in detail any errors or justification gaps you find, as specified in the instructions above. | |
| """ | |
| def get_api_key(): | |
| """ | |
| Retrieves the Google API key from environment variables. | |
| Exits if the key is not found. | |
| """ | |
| api_key = os.getenv("GOOGLE_API_KEY") | |
| if not api_key: | |
| print("Error: GOOGLE_API_KEY environment variable not set.") | |
| print("Please set the variable, e.g., 'export GOOGLE_API_KEY=\"your_api_key\"'") | |
| sys.exit(1) | |
| return api_key | |
| def get_client(): | |
| """ | |
| Creates and returns a Gemini API client. | |
| """ | |
| api_key = get_api_key() | |
| return genai.Client(api_key=api_key) | |
| def read_file_content(filepath): | |
| """ | |
| Reads and returns the content of a file. | |
| Exits if the file cannot be read. | |
| """ | |
| try: | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| return f.read() | |
| except FileNotFoundError: | |
| print(f"Error: File not found at '{filepath}'") | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"Error reading file '{filepath}': {e}") | |
| sys.exit(1) | |
| def send_api_request(client, system_prompt, user_messages): | |
| """ | |
| Sends a request to the Gemini API using the modern SDK. | |
| Args: | |
| client: The Gemini API client | |
| system_prompt: System instruction for the model | |
| user_messages: List of user message strings or a single string | |
| Returns: | |
| The response text from the model | |
| """ | |
| try: | |
| # Build the contents list | |
| if isinstance(user_messages, str): | |
| user_messages = [user_messages] | |
| contents = "\n\n".join(user_messages) | |
| # Create generation config with thinking budget | |
| config = types.GenerateContentConfig( | |
| system_instruction=system_prompt, | |
| temperature=0.1, | |
| top_p=1.0, | |
| ) | |
| # Try to add thinking config if model supports it (2.5+ models) | |
| if '2.5' in MODEL_NAME or '3' in MODEL_NAME: | |
| try: | |
| # Thinking config for advanced models | |
| response = client.models.generate_content( | |
| model=MODEL_NAME, | |
| contents=contents, | |
| config=types.GenerateContentConfig( | |
| system_instruction=system_prompt, | |
| temperature=0.1, | |
| top_p=1.0, | |
| thinking_config=types.ThinkingConfig(thinking_budget=8192*2) | |
| ) | |
| ) | |
| except Exception as e: | |
| # If thinking config fails, fall back to standard config | |
| print(f"Note: Thinking config not supported, using standard config: {e}") | |
| response = client.models.generate_content( | |
| model=MODEL_NAME, | |
| contents=contents, | |
| config=config | |
| ) | |
| else: | |
| # For 2.0 and earlier models, use standard config | |
| response = client.models.generate_content( | |
| model=MODEL_NAME, | |
| contents=contents, | |
| config=config | |
| ) | |
| return response.text | |
| except Exception as e: | |
| print(f"Error during API request: {e}") | |
| raise e | |
| def extract_detailed_solution(solution, marker='Detailed Solution', after=True): | |
| """ | |
| Extracts the text after '### Detailed Solution ###' from the solution string. | |
| Returns the substring after the marker, stripped of leading/trailing whitespace. | |
| If the marker is not found, returns an empty string. | |
| """ | |
| idx = solution.find(marker) | |
| if idx == -1: | |
| return '' | |
| if(after): | |
| return solution[idx + len(marker):].strip() | |
| else: | |
| return solution[:idx].strip() | |
| def verify_solution(client, problem_statement, solution, verbose=True): | |
| dsol = extract_detailed_solution(solution) | |
| newst = f""" | |
| ====================================================================== | |
| ### Problem ### | |
| {problem_statement} | |
| ====================================================================== | |
| ### Solution ### | |
| {dsol} | |
| {verification_remider} | |
| """ | |
| if(verbose): | |
| print(">>>>>>> Start verification.") | |
| print(">>>>>>> Verification prompt:") | |
| print(json.dumps({"system": verification_system_prompt, "user": newst}, indent=4)) | |
| out = send_api_request(client, verification_system_prompt, newst) | |
| if(verbose): | |
| print(">>>>>>> Verification results:") | |
| print(json.dumps(out, indent=4)) | |
| check_correctness = """Response in "yes" or "no". Is the following statement saying the solution is correct, or does not contain critical error or a major justification gap?""" \ | |
| + "\n\n" + out | |
| o = send_api_request(client, "", check_correctness) | |
| if(verbose): | |
| print(">>>>>>> Is verification good?") | |
| print(json.dumps(o, indent=4)) | |
| bug_report = "" | |
| if("yes" not in o.lower()): | |
| bug_report = extract_detailed_solution(out, "Detailed Verification", False) | |
| if(verbose): | |
| print(">>>>>>>Bug report:") | |
| print(json.dumps(bug_report, indent=4)) | |
| return bug_report, o | |
| def check_if_solution_claimed_complete(client, solution): | |
| check_complete_prompt = f""" | |
| Is the following text claiming that the solution is complete? | |
| ========================================================== | |
| {solution} | |
| ========================================================== | |
| Response in exactly "yes" or "no". No other words. | |
| """ | |
| o = send_api_request(client, "", check_complete_prompt) | |
| print(o) | |
| return "yes" in o.lower() | |
| def init_explorations(client, problem_statement, verbose=True, other_prompts=[]): | |
| # Build user messages | |
| user_messages = [problem_statement] + other_prompts | |
| print(f">>>>>> Initial prompt.") | |
| print(json.dumps({"system": step1_prompt, "user": user_messages}, indent=4)) | |
| output1 = send_api_request(client, step1_prompt, user_messages) | |
| print(f">>>>>>> First solution: ") | |
| print(json.dumps(output1, indent=4)) | |
| print(f">>>>>>> Self improvement start:") | |
| # For self-improvement, we need to include the previous solution | |
| # The SDK doesn't have built-in conversation history, so we'll format it manually | |
| improvement_message = f""" | |
| Previous solution: | |
| {output1} | |
| --- | |
| {self_improvement_prompt} | |
| """ | |
| solution = send_api_request(client, step1_prompt, improvement_message) | |
| print(f">>>>>>> Corrected solution: ") | |
| print(json.dumps(solution, indent=4)) | |
| print(f">>>>>>> Vefify the solution.") | |
| verify, good_verify = verify_solution(client, problem_statement, solution, verbose) | |
| print(f">>>>>>> Initial verification: ") | |
| print(json.dumps(verify, indent=4)) | |
| print(f">>>>>>> verify results: {good_verify}") | |
| return solution, verify, good_verify | |
| def agent(problem_statement, other_prompts=[], memory_file=None, resume_from_memory=False): | |
| # Initialize the client | |
| client = get_client() | |
| if resume_from_memory and memory_file: | |
| # Load memory and resume from previous state | |
| memory = load_memory(memory_file) | |
| if memory: | |
| problem_statement = memory.get("problem_statement", problem_statement) | |
| other_prompts = memory.get("other_prompts", other_prompts) | |
| current_iteration = memory.get("current_iteration", 0) | |
| solution = memory.get("solution", None) | |
| verify = memory.get("verify", None) | |
| print(f"Resuming from iteration {current_iteration}") | |
| else: | |
| print("Failed to load memory, starting fresh") | |
| current_iteration = 0 | |
| solution = None | |
| verify = None | |
| else: | |
| # Start fresh | |
| current_iteration = 0 | |
| solution = None | |
| verify = None | |
| if solution is None: | |
| solution, verify, good_verify = init_explorations(client, problem_statement, True, other_prompts) | |
| if(solution is None): | |
| print(">>>>>>> Failed in finding a complete solution.") | |
| return None | |
| else: | |
| # We have a solution from memory, need to get good_verify | |
| _, good_verify = verify_solution(client, problem_statement, solution) | |
| error_count = 0 | |
| correct_count = 1 | |
| success = False | |
| for i in range(current_iteration, 30): | |
| print(f"Number of iterations: {i}, number of corrects: {correct_count}, number of errors: {error_count}") | |
| if("yes" not in good_verify.lower()): | |
| # clear | |
| correct_count = 0 | |
| error_count += 1 | |
| #self improvement | |
| print(">>>>>>> Verification does not pass, correcting ...") | |
| # Build correction message with problem, solution, and bug report | |
| correction_message = f""" | |
| Problem Statement: | |
| {problem_statement} | |
| Previous Solution: | |
| {solution} | |
| --- | |
| {correction_prompt} | |
| Bug Report: | |
| {verify} | |
| """ | |
| print(">>>>>>> New prompt:") | |
| print(json.dumps({"system": step1_prompt, "message": "correction with bug report"}, indent=4)) | |
| solution = send_api_request(client, step1_prompt, correction_message) | |
| print(">>>>>>> Corrected solution:") | |
| print(json.dumps(solution, indent=4)) | |
| print(f">>>>>>> Verify the solution.") | |
| verify, good_verify = verify_solution(client, problem_statement, solution) | |
| if("yes" in good_verify.lower()): | |
| print(">>>>>>> Solution is good, verifying again ...") | |
| correct_count += 1 | |
| error_count = 0 | |
| # Save memory every iteration | |
| if memory_file: | |
| save_memory(memory_file, problem_statement, other_prompts, i, 30, solution, verify) | |
| if(correct_count >= 5): | |
| print(">>>>>>> Correct solution found.") | |
| print(json.dumps(solution, indent=4)) | |
| return solution | |
| elif(error_count >= 10): | |
| print(">>>>>>> Failed in finding a correct solution.") | |
| # Save final state before returning | |
| if memory_file: | |
| save_memory(memory_file, problem_statement, other_prompts, i, 30, solution, verify) | |
| return None | |
| if(not success): | |
| print(">>>>>>> Failed in finding a correct solution.") | |
| # Save final state before returning | |
| if memory_file: | |
| save_memory(memory_file, problem_statement, other_prompts, 30, 30, solution, verify) | |
| return None | |
| if __name__ == "__main__": | |
| # Set up argument parsing | |
| parser = argparse.ArgumentParser(description='IMO Problem Solver Agent') | |
| parser.add_argument('problem_file', nargs='?', default='problem_statement.txt', | |
| help='Path to the problem statement file (default: problem_statement.txt)') | |
| parser.add_argument('--log', '-l', type=str, help='Path to log file (optional)') | |
| parser.add_argument('--solution', '-s', type=str, help='Path to save clean solution file (optional)') | |
| parser.add_argument('--other_prompts', '-o', type=str, help='Other prompts (optional)') | |
| parser.add_argument("--max_runs", '-m', type=int, default=10, help='Maximum number of runs (default: 10)') | |
| parser.add_argument('--memory', '-mem', type=str, help='Path to memory file for saving/loading state (optional)') | |
| parser.add_argument('--resume', '-r', action='store_true', help='Resume from memory file if provided') | |
| args = parser.parse_args() | |
| max_runs = args.max_runs | |
| memory_file = args.memory | |
| resume_from_memory = args.resume | |
| solution_file = args.solution | |
| other_prompts = [] | |
| if args.other_prompts: | |
| other_prompts = args.other_prompts.split(',') | |
| print(">>>>>>> Other prompts:") | |
| print(other_prompts) | |
| if memory_file: | |
| print(f"Memory file: {memory_file}") | |
| if resume_from_memory: | |
| print("Resume mode: Will attempt to load from memory file") | |
| # Set up logging if log file is specified | |
| if args.log: | |
| if not set_log_file(args.log): | |
| sys.exit(1) | |
| print(f"Logging to file: {args.log}") | |
| problem_statement = read_file_content(args.problem_file) | |
| for i in range(max_runs): | |
| print(f"\n\n>>>>>>>>>>>>>>>>>>>>>>>>>> Run {i} of {max_runs} ...") | |
| try: | |
| sol = agent(problem_statement, other_prompts, memory_file, resume_from_memory) | |
| if(sol is not None): | |
| print(f">>>>>>> Found a correct solution in run {i}.") | |
| print(json.dumps(sol, indent=4)) | |
| # Save clean solution to separate file if specified | |
| if solution_file: | |
| try: | |
| with open(solution_file, 'w', encoding='utf-8') as f: | |
| f.write(sol) | |
| print(f">>>>>>> Clean solution saved to: {solution_file}") | |
| except Exception as e: | |
| print(f">>>>>>> Error saving solution file: {e}") | |
| break | |
| except Exception as e: | |
| print(f">>>>>>> Error in run {i}: {e}") | |
| continue | |
| # Close log file if it was opened | |
| close_log_file() | |