{
  "id": 573071,
  "title": "[Private27,Public28] Solution for 17th: To get code & fix it with DeepSeek",
  "content": "<p>LLM used:<br>\n <a href=\"https://www.kaggle.com/models/huikang/deepseek-r1/Transformers/deepseek-r1-distill-qwen-14b-awq-casperhansen/1\" target=\"_blank\">deepseek-r1-distill-qwen-14b-awq-casperhansen</a></p>\n<p>I thought some of the questions can be solved much more efficiently by running Python code— so I explored generating code with DeepSeek R1. However, simply adding \"Make python code.\" to the prompt did not work well.<br>\nHere is what I found that worked well with DeepSeek and QWQ,</p>\n<h1>To get code and fix it with LLM:</h1>\n<p>Step 1: Initial Prompting</p>\n<p>Provide the question text as a prompt to the model.<br>\nSet output length between 2000 to 4000 tokens. <br>\nWith this, get output in 1-2 min.<br>\nOutput text are always truncated but no problem for further use for code generation.</p>\n<p>Step 2: Prompt for Code Generation</p>\n<p>Construct a new retry_prompt using the model's earlier output:<br>\nStructure:</p>\n<pre><code>    content = &lt;Instruction text  make code  previous thought&gt;\n     (e.g.)\n    content += question\n    content += output\n\n    prompt = [{: , : content}]\n    prompt = tokenizer.apply_chat_template(\n                        =retry_prompt,\n                        =,\n                        =)\n</code></pre>\n<p>Then, add either of the following:</p>\n<p>For better code (more verbose, with reasoning):</p>\n<pre><code>    prompt += '&lt;/think&gt;'\n    prompt += \"Here's a structured approach to solve the problem:### Approach\"\n</code></pre>\n<p>For fast (but sometimes lower-quality) code:</p>\n<pre><code>     += \n    prompt += \n</code></pre>\n<p>With this, get code within another ~1 min.</p>\n<p>Step 3: Error Fixing Prompt</p>\n<p>If generated code fails when run via subprocess, build a new error-fixing prompt:</p>\n<pre><code>    content = &lt;Some instructions to fix the code&gt;\n     (e.g.&gt;\n    content +=  question\n    content += &lt;Code with error&gt;\n    content += &lt;Error message&gt;\n</code></pre>\n<p>Retry generation with this new prompt.<br>\nSometime works, but sometimes encounters another error and repeat this fixing process.</p>\n<p>Notebook demonstrating code generation and error fixing using the reference set:<br>\n<a href=\"https://www.kaggle.com/code/ippeiogawa/generate-code-from-deepseekr1\" target=\"_blank\">https://www.kaggle.com/code/ippeiogawa/generate-code-from-deepseekr1</a></p>\n<h1>Summary of strategy</h1>\n<ul>\n<li><p>Samples:<br>\nTotal of 16 samples —<br>\n→ 4 used to get code (as mentioned above)<br>\n→ 12 used to get boxed answers</p></li>\n<li><p>Chunked Reasoning:<br>\nOutput every 2000 tokens → combine 3–4 outputs → prompt with:<br>\n\"Please think again from combined previous thought.\"</p></li>\n<li><p>vLLM.LLMEngine:<br>\nUsed to stream results as soon as a sample finishes.<br>\nLLMEngine Code Snippet:</p>\n<pre><code> vllm import EngineArgs, LLMEngine, SamplingParams\n\n\nengine_args = EngineArgs(\n    llm_model_path,\n    =max_len,\n    =,\n    =4,\n    =0.96,\n    =2025,\n)\nengine = LLMEngine.from_engine_args(engine_args)\n\n\n &lt;Some conditions  continue generation   stop it&gt;:\n     prompt:\n        engine.add_request(str(request_id), prompt, sampling_params)\n        request_id +=1\n\n    request_outputs = engine.()\n\n     request_output  request_outputs:\n         request_output.finished:\n            output_text = request_output[0].text\n            # Extract an answer\n            # Process output\n             Code  output_text:\n                 via subprocess\n\n            # Build new prompts  generating  fixing code   think again with combined thought.\n            new_prompt = &lt;construct new prompt&gt;\n            engine.add_request(str(request_id), new_prompt, new_sampling_params)\n            request_id +=1\n</code></pre></li>\n<li><p>Early stopping: <br>\nIf 3 identical answers are produced, I treat that as the final answer and move on to the next question.</p></li>\n</ul>\n<p>Thank you for reading!</p>",
  "comments": [
    {
      "id": 3177995,
      "author_name": "Md Adilur Rahim",
      "author_url": "",
      "post_date": "2025-04-13T15:59:47.333000",
      "content": "<p>I did the same thing. I found that the Deepseek R1 Distil 32 B model is great for Python coding. My approach failed the public and private sets, and maybe needed more evaluations and the early stopping tactics. </p>\n<p>I was confident that the code-generating solution would work. I am so glad to see your approach worked! </p>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 3177863,
      "author_name": "ducnh279",
      "author_url": "",
      "post_date": "2025-04-13T11:11:56.990000",
      "content": "<p>Congrats, I really like your approach!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "comment_count": 2,
  "messages": [
    {
      "id": 3177845,
      "postDate": "2025-04-13T10:42:31.920Z",
      "content": "<p>LLM used:<br>\n <a href=\"https://www.kaggle.com/models/huikang/deepseek-r1/Transformers/deepseek-r1-distill-qwen-14b-awq-casperhansen/1\" target=\"_blank\">deepseek-r1-distill-qwen-14b-awq-casperhansen</a></p>\n<p>I thought some of the questions can be solved much more efficiently by running Python code— so I explored generating code with DeepSeek R1. However, simply adding \"Make python code.\" to the prompt did not work well.<br>\nHere is what I found that worked well with DeepSeek and QWQ,</p>\n<h1>To get code and fix it with LLM:</h1>\n<p>Step 1: Initial Prompting</p>\n<p>Provide the question text as a prompt to the model.<br>\nSet output length between 2000 to 4000 tokens. <br>\nWith this, get output in 1-2 min.<br>\nOutput text are always truncated but no problem for further use for code generation.</p>\n<p>Step 2: Prompt for Code Generation</p>\n<p>Construct a new retry_prompt using the model's earlier output:<br>\nStructure:</p>\n<pre><code>    content = &lt;Instruction text  make code  previous thought&gt;\n     (e.g.)\n    content += question\n    content += output\n\n    prompt = [{: , : content}]\n    prompt = tokenizer.apply_chat_template(\n                        =retry_prompt,\n                        =,\n                        =)\n</code></pre>\n<p>Then, add either of the following:</p>\n<p>For better code (more verbose, with reasoning):</p>\n<pre><code>    prompt += '&lt;/think&gt;'\n    prompt += \"Here's a structured approach to solve the problem:### Approach\"\n</code></pre>\n<p>For fast (but sometimes lower-quality) code:</p>\n<pre><code>     += \n    prompt += \n</code></pre>\n<p>With this, get code within another ~1 min.</p>\n<p>Step 3: Error Fixing Prompt</p>\n<p>If generated code fails when run via subprocess, build a new error-fixing prompt:</p>\n<pre><code>    content = &lt;Some instructions to fix the code&gt;\n     (e.g.&gt;\n    content +=  question\n    content += &lt;Code with error&gt;\n    content += &lt;Error message&gt;\n</code></pre>\n<p>Retry generation with this new prompt.<br>\nSometime works, but sometimes encounters another error and repeat this fixing process.</p>\n<p>Notebook demonstrating code generation and error fixing using the reference set:<br>\n<a href=\"https://www.kaggle.com/code/ippeiogawa/generate-code-from-deepseekr1\" target=\"_blank\">https://www.kaggle.com/code/ippeiogawa/generate-code-from-deepseekr1</a></p>\n<h1>Summary of strategy</h1>\n<ul>\n<li><p>Samples:<br>\nTotal of 16 samples —<br>\n→ 4 used to get code (as mentioned above)<br>\n→ 12 used to get boxed answers</p></li>\n<li><p>Chunked Reasoning:<br>\nOutput every 2000 tokens → combine 3–4 outputs → prompt with:<br>\n\"Please think again from combined previous thought.\"</p></li>\n<li><p>vLLM.LLMEngine:<br>\nUsed to stream results as soon as a sample finishes.<br>\nLLMEngine Code Snippet:</p>\n<pre><code> vllm import EngineArgs, LLMEngine, SamplingParams\n\n\nengine_args = EngineArgs(\n    llm_model_path,\n    =max_len,\n    =,\n    =4,\n    =0.96,\n    =2025,\n)\nengine = LLMEngine.from_engine_args(engine_args)\n\n\n &lt;Some conditions  continue generation   stop it&gt;:\n     prompt:\n        engine.add_request(str(request_id), prompt, sampling_params)\n        request_id +=1\n\n    request_outputs = engine.()\n\n     request_output  request_outputs:\n         request_output.finished:\n            output_text = request_output[0].text\n            # Extract an answer\n            # Process output\n             Code  output_text:\n                 via subprocess\n\n            # Build new prompts  generating  fixing code   think again with combined thought.\n            new_prompt = &lt;construct new prompt&gt;\n            engine.add_request(str(request_id), new_prompt, new_sampling_params)\n            request_id +=1\n</code></pre></li>\n<li><p>Early stopping: <br>\nIf 3 identical answers are produced, I treat that as the final answer and move on to the next question.</p></li>\n</ul>\n<p>Thank you for reading!</p>",
      "rawMarkdown": "LLM used:\n [deepseek-r1-distill-qwen-14b-awq-casperhansen](https://www.kaggle.com/models/huikang/deepseek-r1/Transformers/deepseek-r1-distill-qwen-14b-awq-casperhansen/1)\n\nI thought some of the questions can be solved much more efficiently by running Python code— so I explored generating code with DeepSeek R1. However, simply adding \"Make python code.\" to the prompt did not work well.\nHere is what I found that worked well with DeepSeek and QWQ,\n#To get code and fix it with LLM:\nStep 1: Initial Prompting\n\nProvide the question text as a prompt to the model.\nSet output length between 2000 to 4000 tokens. \nWith this, get output in 1-2 min.\nOutput text are always truncated but no problem for further use for code generation.\n\nStep 2: Prompt for Code Generation\n\nConstruct a new retry_prompt using the model's earlier output:\nStructure:\n\n        content = <Instruction text to make code from previous thought>\n         (e.g.\"Please make a short summary of your approach, including python code.\")\n        content += question\n        content += output\n\n        prompt = [{\"role\": \"user\", \"content\": content}]\n        prompt = tokenizer.apply_chat_template(\n                            conversation=retry_prompt,\n                            tokenize=False,\n                            add_generation_prompt=True)\n\nThen, add either of the following:\n\nFor better code (more verbose, with reasoning):\n\n        prompt += '\\n</think>\\n'\n        prompt += \"Here's a structured approach to solve the problem:\\n### Approach\\n\"\n\nFor fast (but sometimes lower-quality) code:\n\n        prompt += '\\n</think>\\n'\n        prompt += '```python\\n'\n\nWith this, get code within another ~1 min.\n\nStep 3: Error Fixing Prompt\n\nIf generated code fails when run via subprocess, build a new error-fixing prompt:\n\n        content = <Some instructions to fix the code>\n         (e.g.\"Please correct python code below and put in ```python\\n{{corrected code here}}```.\">\n        content +=  question\n        content += <Code with error>\n        content += <Error message>\n\nRetry generation with this new prompt.\nSometime works, but sometimes encounters another error and repeat this fixing process.\n\nNotebook demonstrating code generation and error fixing using the reference set:\n[https://www.kaggle.com/code/ippeiogawa/generate-code-from-deepseekr1](https://www.kaggle.com/code/ippeiogawa/generate-code-from-deepseekr1)\n\n\n\n#Summary of strategy\n- Samples:\nTotal of 16 samples —\n→ 4 used to get code (as mentioned above)\n→ 12 used to get boxed answers\n- Chunked Reasoning:\nOutput every 2000 tokens → combine 3–4 outputs → prompt with:\n\"Please think again from combined previous thought.\"\n- vLLM.LLMEngine:\nUsed to stream results as soon as a sample finishes.\nLLMEngine Code Snippet:\n\n        from vllm import EngineArgs, LLMEngine, SamplingParams\n\n        # Initialize engine\n        engine_args = EngineArgs(\n            llm_model_path,\n            max_model_len=max_len,\n            trust_remote_code=True,\n            tensor_parallel_size=4,\n            gpu_memory_utilization=0.96,\n            seed=2025,\n        )\n        engine = LLMEngine.from_engine_args(engine_args)\n\n        # Main loop\n        while <Some conditions to continue generation or to stop it>:\n            if prompt:\n                engine.add_request(str(request_id), prompt, sampling_params)\n                request_id +=1\n        #\n            request_outputs = engine.step()\n        #\n            for request_output in request_outputs:\n                if request_output.finished:\n                    output_text = request_output[0].text\n                    # Extract an answer\n                    # Process output\n                    if Code in output_text:\n                        run via subprocess\n        #\n                    # Build new prompts for generating or fixing code or to think again with combined thought.\n                    new_prompt = <construct new prompt>\n                    engine.add_request(str(request_id), new_prompt, new_sampling_params)\n                    request_id +=1\n\n- Early stopping: \nIf 3 identical answers are produced, I treat that as the final answer and move on to the next question.\n\nThank you for reading!",
      "votes": 15
    },
    {
      "id": 3177995,
      "postDate": "2025-04-13T15:59:47.333Z",
      "content": "<p>I did the same thing. I found that the Deepseek R1 Distil 32 B model is great for Python coding. My approach failed the public and private sets, and maybe needed more evaluations and the early stopping tactics. </p>\n<p>I was confident that the code-generating solution would work. I am so glad to see your approach worked! </p>",
      "rawMarkdown": "I did the same thing. I found that the Deepseek R1 Distil 32 B model is great for Python coding. My approach failed the public and private sets, and maybe needed more evaluations and the early stopping tactics. \n\nI was confident that the code-generating solution would work. I am so glad to see your approach worked! ",
      "votes": 2
    },
    {
      "id": 3177863,
      "postDate": "2025-04-13T11:11:56.990Z",
      "content": "<p>Congrats, I really like your approach!</p>",
      "rawMarkdown": "Congrats, I really like your approach!"
    }
  ],
  "complete": true,
  "source": "live",
  "fetched_at": "2026-10-20T06:42:10Z",
  "competition_id": 86023,
  "competition_slug": "ai-mathematical-olympiad-progress-prize-2",
  "team_id": 12923091,
  "solution_url": "/competitions/ai-mathematical-olympiad-progress-prize-2/writeups/ippeiogawa-private27-public28-solution-for-17th-to"
}