Prompt Engineering Guide
Mastering Debug code
on Phi-3.5 MoE
Stop guessing. See how professional prompt engineering transforms Phi-3.5 MoE's output for specific technical tasks.
The "Vibe" Prompt
"Hey, can you debug this Python code for me? It's not working as expected. ```python
def calculate_average(numbers):
total = 0
for num in numbers:
total += num
return total / len(numbers)
data = [1, 2, 3, 4, 0]
print(calculate_average(data)) # Expected: 2.0, Actual: ZeroDivisionError if data contains 0 and becomes empty, or incorrect if data changes or is empty
```"
Low specificity, inconsistent output
Optimized Version
You are a senior Python developer specializing in debugging. Your goal is to identify and fix bugs in provided Python code, ensuring correctness, efficiency, and adherence to best practices.
**Task:** Debug the following Python function `calculate_average`.
**Code to Debug:**
```python
def calculate_average(numbers):
total = 0
for num in numbers:
total += num
return total / len(numbers)
data = [1, 2, 3, 4, 0]
print(calculate_average(data))
```
**Expected Behavior (if known, otherwise infer):** The function should compute the arithmetic mean of a list of numbers.
**Observed Problem/Error (if known, otherwise identify):** The current code might raise a `ZeroDivisionError` if the input list `numbers` is empty.
**Chain of Thought for Debugging:**
1. **Analyze Input:** Examine the `numbers` parameter. What are its possible states (empty, contains zeros, all positive, etc.)?
2. **Identify Potential Failure Points:** Given the arithmetic mean calculation, what operations could lead to errors (e.g., division)?
3. **Trace Execution (Mental Walkthrough):** Consider `len(numbers)` when `numbers` is empty. What happens?
4. **Formulate a Hypothesis:** The `ZeroDivisionError` will occur when `len(numbers)` evaluates to 0.
5. **Propose Solution:** Implement a check for an empty list before performing division. Consider appropriate return values (e.g., 0, `None`, raise an error).
6. **Refine Solution:** Ensure the fix handles edge cases gracefully and communicates the outcome clearly.
**Output Format should be:**
```json
{
"identified_bug": "<description of the bug>",
"root_cause": "<explanation of the root cause>",
"proposed_fix": "<corrected code block>",
"explanation_of_fix": "<detailed explanation of why the fix works and handles edge cases>"
}
```
Structured, task-focused, reduced hallucinations
Engineering Rationale
The optimized prompt provides a clear persona, an explicit task, and structured 'Chain of Thought' steps for debugging. This guides the model to systematically analyze the problem, anticipate edge cases (like an empty list), and propose a well-reasoned solution. The output format is explicitly defined, ensuring the model's response is parseable and contains all necessary information. This reduces ambiguity and encourages a more thorough, step-by-step debugging process compared to the vague 'vibe check' of the naive prompt.
0%
Token Efficiency Gain
The model's output for the optimized prompt must identify 'ZeroDivisionError' as the primary bug.
The proposed fix must include an explicit check for an empty list (`if not numbers:` or `if len(numbers) == 0:`).
The `explanation_of_fix` must justify the empty list check.
Ready to stop burning tokens?
Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.
Optimize My Prompts