123 lines
4.1 KiB
Python
123 lines
4.1 KiB
Python
"""
|
||
Exercise 2 – SOLUTION – Persona, Task, and Data in a Structured Prompt
|
||
=======================================================================
|
||
AISE501 · Prompting in Coding · Spring Semester 2026
|
||
"""
|
||
|
||
from pathlib import Path
|
||
|
||
from server_utils import chat, get_client, print_messages, print_separator
|
||
|
||
client = get_client()
|
||
|
||
code_to_review = Path("analyze_me.py").read_text()
|
||
temperature_value=1
|
||
|
||
# ── Part A: Persona + Task + Code ─────────────────────────────────────────────
|
||
print_separator("Part A – Structured Prompt: Persona / Task / Code")
|
||
|
||
prompt_a = f"""\
|
||
<persona>
|
||
You are a senior Python engineer with 10+ years of experience.
|
||
You are rigorous about correctness, follow PEP-8 strictly, and care
|
||
deeply about defensive programming and readable code.
|
||
</persona>
|
||
|
||
<task>
|
||
Review the Python code provided below.
|
||
Identify every bug and code-quality issue you can find.
|
||
For each issue, state what is wrong and why it is a problem.
|
||
</task>
|
||
|
||
<code language="python" filename="analyze_me.py">
|
||
{code_to_review}
|
||
</code>"""
|
||
|
||
messages_a = [
|
||
{"role": "user", "content": prompt_a}
|
||
]
|
||
|
||
print_messages(messages_a)
|
||
response_a = chat(client, messages_a, temperature=temperature_value)
|
||
print(response_a)
|
||
|
||
|
||
# ── Part B: Refine – Ask for a Prioritised Bug List ───────────────────────────
|
||
print_separator("Part B – Refined Prompt: Prioritised Bug List")
|
||
|
||
system_b = """\
|
||
You are a senior Python engineer performing a thorough code review.
|
||
Be concise, precise, and always refer to line numbers when available.
|
||
"""
|
||
|
||
prompt_b = f"""\
|
||
<persona>
|
||
You are a senior Python engineer with 10+ years of experience.
|
||
You are rigorous about correctness, follow PEP-8, and care about
|
||
defensive programming and readable code.
|
||
</persona>
|
||
|
||
<task>
|
||
Review the Python code below.
|
||
Identify every bug and code-quality issue.
|
||
Classify each finding by severity:
|
||
- Critical : causes a crash or wrong result under normal use
|
||
- Medium : bad practice that will cause problems in production
|
||
- Style : violates PEP-8 or reduces readability
|
||
</task>
|
||
|
||
<output_format>
|
||
For each finding produce exactly this structure (plain text):
|
||
[SEVERITY] Line <N>: <one-sentence problem description>
|
||
Fix hint: <one-sentence suggestion>
|
||
|
||
Group findings under headings: ## Critical, ## Medium, ## Style
|
||
</output_format>
|
||
|
||
<code language="python" filename="analyze_me.py">
|
||
{code_to_review}
|
||
</code>"""
|
||
|
||
messages_b = [
|
||
{"role": "system", "content": system_b},
|
||
{"role": "user", "content": prompt_b},
|
||
]
|
||
|
||
print_messages(messages_b)
|
||
response_b = chat(client, messages_b, temperature=temperature_value)
|
||
print(response_b)
|
||
|
||
|
||
# ── Part C: Request a Corrected Function ──────────────────────────────────────
|
||
print_separator("Part C – Ask for a Corrected Function")
|
||
|
||
followup = """\
|
||
<task>
|
||
Rewrite only the `calculate_statistics` function with all bugs fixed.
|
||
Requirements:
|
||
- Handle an empty list gracefully (return None or raise ValueError with a clear message)
|
||
- Use sample variance (divide by N-1)
|
||
- Add full PEP-8 type hints
|
||
- Add a NumPy-style docstring
|
||
Return only the function code, no surrounding explanation.
|
||
</task>"""
|
||
|
||
messages_c = messages_b + [
|
||
{"role": "assistant", "content": response_b},
|
||
{"role": "user", "content": followup},
|
||
]
|
||
|
||
print_messages(messages_c)
|
||
response_c = chat(client, messages_c, temperature=temperature_value)
|
||
print(response_c)
|
||
|
||
|
||
# ── Reflection Questions ──────────────────────────────────────────────────────
|
||
print_separator("Reflection Questions")
|
||
print(
|
||
"1. Did the LLM find all 7 bugs? Which did it miss?\n"
|
||
"2. How did the <output_format> tag change the structure of the answer?\n"
|
||
"3. What is the advantage of continuing a conversation vs. starting fresh?\n"
|
||
"4. How would you scale this pattern to a large codebase (many files)?\n"
|
||
)
|