return total # Incorrect indentation: return is inside the loop
""",
"correct":"""
def sum_even_numbers(numbers):
total = 0
for num in numbers:
if num % 2 == 0:
total += num
return total # Correct indentation: return is outside the loop
""",
"diff":"""
- return total # Incorrect indentation: return is inside the loop
+ return total # Correct indentation: return is outside the loop
""",
"problem":"Sum all even numbers in a given list."
}
# Prompt template for generating all hint levels
HINT_GENERATION_PROMPT="""
You are an expert code reviewer. Your task is to generate hints for the following code snippet at three levels of detail: high, middle, and low.
**Problem**: {problem}
**Code**:
{code}
**Diff (if applicable)**:
{diff}
Provide your response in the following JSON format:
{
"high": "High-level hint (e.g., 'The code is incorrect.' or 'The code is correct.')",
"middle": "Middle-level hint (e.g., 'The code contains a logic error related to the placement of the return statement.' or 'The code correctly implements the intended functionality.')",
"low": "Low-level hint (e.g., 'The return statement is incorrectly placed inside the loop, causing the function to exit prematurely.' or 'The return statement is placed outside the loop, ensuring all numbers are processed.')"
}
"""
# Unified prompt template
UNIFIED_PROMPT="""
You are an expert code reviewer. Your task is to analyze the following code snippet step-by-step. Follow this structure:
1. **Intended Functionality**: What is the code trying to do?
2. **Code Analysis**:
- If the code is **incorrect**:
- What is the error in the code?
- Why does this error occur?
- If the code is **correct**:
- Why is this code correct?
- Are there any potential improvements or edge cases to consider?
**Problem**: {problem}
**Hint (if applicable)**: {hint}
Here is the code:
{code}
Provide your response in a clear and structured format.
"""
# Prompt template for verifying consistency
VERIFICATION_PROMPT="""
You are an expert code reviewer. Your task is to verify whether the following analysis is consistent with the hint and the problem description.
**Problem**: {problem}
**Hint**: {hint}
**Analysis**:
{analysis}
Provide your response as either "Consistent" or "Inconsistent" and briefly explain why.
"""
# Function to call the LLM
defcall_llm(prompt):
response=openai.ChatCompletion.create(
model="gpt-4",# Use the appropriate model
messages=[{"role":"user","content":prompt}],
max_tokens=500,
temperature=0.7,
)
returnresponse.choices[0].message.content.strip()
# Function to generate all hint levels using the LLM
You are a helpful programming assistant. Your task is to analyze the following **question, incorrect code, and diff edit**. Think step by step and generate a hint that:
1. Explains **why the algorithm is incorrect** (algorithmic reasoning).
2. Provides **actionable guidance** on how to fix the code (code action).
Ensure the hint is clear, actionable, and appropriate for a **{level}-level** learner.
---
## Input
### Question
{question}
### Incorrect Code
```python
{incorrect_code}
```
### Diff Edit
```diff
{diff_edit}
```
---
## Output Format
Return your response in the following format:
### Hint
[Your hint here. Include both algorithmic reasoning and actionable guidance. Entirely in natural language.]