Skip to content

Instantly share code, notes, and snippets.

@bigsnarfdude
Last active June 9, 2025 05:52
Show Gist options
  • Save bigsnarfdude/2bb66ac5f8c188223fde7e28bd02e912 to your computer and use it in GitHub Desktop.
Save bigsnarfdude/2bb66ac5f8c188223fde7e28bd02e912 to your computer and use it in GitHub Desktop.
deepseek r1 prompt template
class PromptTemplate:
    def __init__(self):
        self.base_template = """A conversation between User and Assistant. The user asks a question, and the Assistant solves it.
The assistant first thinks about the reasoning process in the mind and then provides the user with the answer.
The reasoning process and answer are enclosed within <think> </think> and <answer> </answer> tags, respectively.

User: {question}
Assistant: {think_tag}
{reasoning}
{think_end_tag}
{answer_tag}
{solution}
{answer_end_tag}
"""

    def generate_math_prompt(self, question):
        """Generate a prompt for mathematical problems"""
        return self.base_template.format(
            question=question,
            think_tag="<think>",
            reasoning="""1. First, let's understand what the question is asking
2. Break down the mathematical components
3. Apply relevant mathematical rules
4. Calculate step by step
5. Verify the result""",
            think_end_tag="</think>",
            answer_tag="<answer>",
            solution="[Mathematical solution will be provided here]",
            answer_end_tag="</answer>"
        )

    def generate_code_prompt(self, question):
        """Generate a prompt for coding problems"""
        return self.base_template.format(
            question=question,
            think_tag="<think>",
            reasoning="""1. Analyze the programming requirements
2. Consider edge cases and constraints
3. Plan the algorithm structure
4. Think about time and space complexity
5. Consider test cases""",
            think_end_tag="</think>",
            answer_tag="<answer>",
            solution="[Code solution will be provided here]",
            answer_end_tag="</answer>"
        )

# Example usage
if __name__ == "__main__":
    template = PromptTemplate()
    
    # Example 1: Math Problem
    math_question = "Calculate the area of a circle with radius 5 units."
    math_prompt = template.generate_math_prompt(math_question)
    print("Math Problem Template:")
    print("-" * 50)
    print(math_prompt)
    print("\n")
    
    # Example 2: Code Problem
    code_question = "Write a function to find the factorial of a number."
    code_prompt = template.generate_code_prompt(code_question)
    print("Code Problem Template:")
    print("-" * 50)
    print(code_prompt)

# Example outputs would look like:
"""
Example 1 - Math Problem:
User: Calculate the area of a circle with radius 5 units.
Assistant: <think>
1. First, let's understand what the question is asking
2. Break down the mathematical components
3. Apply relevant mathematical rules
4. Calculate step by step
5. Verify the result
</think>
<answer>
Let's solve this step by step:
1. Area of circle = π * r²
2. r = 5 units
3. Area = π * 5²
4. Area = π * 25
5. Area ≈ 78.54 square units
</answer>

Example 2 - Code Problem:
User: Write a function to find the factorial of a number.
Assistant: <think>
1. Analyze the programming requirements
2. Consider edge cases and constraints
3. Plan the algorithm structure
4. Think about time and space complexity
5. Consider test cases
</think>
<answer>
def factorial(n):
    if n < 0:
        return None
    if n == 0 or n == 1:
        return 1
    return n * factorial(n-1)
</answer>
"""
@bigsnarfdude
Copy link
Author

class ReasoningSystem:
    """Core system handling question processing and validation"""
    
    def __init__(self):
        self.template = """User: {prompt}
Assistant: <think>
{reasoning}
{validation}
</think>
<answer>
{answer}
</answer>"""

    def classify_question(self, prompt):
        """Determine question type"""
        if "create a function" in prompt.lower() or "python" in prompt.lower():
            return "code"
        return "math"

    def generate_response(self, prompt):
        """Main processing method"""
        q_type = self.classify_question(prompt)
        
        if q_type == "math":
            result, reasoning, validation = self._process_math(prompt)
        else:
            result, reasoning, validation = self._process_code(prompt)
            
        return self.template.format(
            prompt=prompt,
            reasoning=reasoning,
            validation=validation,
            answer=result
        )

    def _process_math(self, prompt):
        """Handle numerical problems"""
        # Extract and calculate
        nums = list(map(int, prompt.split()[-1].rstrip('?')))
        result = sum(nums)
        
        # Build reasoning
        reasoning = f"Processing math problem:\n- Extracted numbers: {nums}\n- Performing addition"
        validation = f"Validation:\n- Check: {nums[0]} + {nums[1]} = {result}\n- Result verified ✅"
        
        return str(result), reasoning, validation

    def _process_code(self, prompt):
        """Handle coding problems"""
        # Example code generation
        code = "def add(a, b):\n    return a + b"
        
        # Build reasoning and validation
        reasoning = f"Processing code request:\n- Generating function\n- Checking syntax"
        validation = "Test Validation:\n"
        test_cases = [
            ("add(2,3)", 5),
            ("add(-1,5)", 4)
        ]
        
        for test, expected in test_cases:
            actual = eval(test)
            validation += f"- {test} => Expected {expected}, Got {actual} {'✅' if actual == expected else '❌'}\n"
        
        return code, reasoning, validation

# Usage Example
system = ReasoningSystem()

math_prompt = "What is 15 + 37?"
print(system.generate_response(math_prompt))

code_prompt = "Create a Python function to add two numbers"
print("\n" + system.generate_response(code_prompt))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment