92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
"""
|
||
Exercise 1 – SOLUTION – Basic XML Structured Prompting
|
||
=======================================================
|
||
AISE501 · Prompting in Coding · Spring Semester 2026
|
||
"""
|
||
|
||
from server_utils import chat, get_client, print_messages, print_separator
|
||
|
||
client = get_client()
|
||
temperature_value=0.3
|
||
|
||
# ── Part A: Unstructured (Zero-Shot) Prompt ───────────────────────────────────
|
||
print_separator("Part A – Unstructured Prompt")
|
||
|
||
unstructured_messages = [
|
||
{
|
||
"role": "user",
|
||
"content": (
|
||
"Explain what a Python list comprehension is, "
|
||
"give an example that filters even numbers from a list, "
|
||
"and list two common mistakes beginners make."
|
||
),
|
||
}
|
||
]
|
||
|
||
print_messages(unstructured_messages)
|
||
response_a = chat(client, unstructured_messages)
|
||
print(response_a)
|
||
|
||
|
||
# ── Part B: Structured Prompt with XML Tags ───────────────────────────────────
|
||
print_separator("Part B – Structured Prompt with XML Tags")
|
||
|
||
structured_content = """\
|
||
<request>
|
||
<topic>
|
||
Python list comprehensions
|
||
</topic>
|
||
<example>
|
||
A list comprehension that takes a list of integers and returns only
|
||
the even numbers, using a conditional filter expression.
|
||
</example>
|
||
<focus>
|
||
1. The general syntax: [expression for item in iterable if condition]
|
||
2. Two common beginner mistakes when writing list comprehensions
|
||
</focus>
|
||
</request>"""
|
||
|
||
structured_messages = [
|
||
{"role": "user", "content": structured_content}
|
||
]
|
||
|
||
print_messages(structured_messages)
|
||
response_b = chat(client, structured_messages, temperature=temperature_value)
|
||
print(response_b)
|
||
|
||
|
||
# ── Part C: Adding a System Prompt ────────────────────────────────────────────
|
||
print_separator("Part C – Adding a System Prompt")
|
||
|
||
system_content = """\
|
||
<persona>
|
||
You are an experienced Python tutor. You teach Python to university students
|
||
who have basic programming knowledge but are new to idiomatic Python.
|
||
</persona>
|
||
<style>
|
||
Always show a working code snippet first, then explain it step by step.
|
||
Use plain language. Avoid jargon without defining it. Write python in PEP8 style
|
||
</style>
|
||
<constraints>
|
||
Keep each answer under 200 words. Use at most one code block per response.
|
||
</constraints>"""
|
||
|
||
messages_c = [
|
||
{"role": "system", "content": system_content},
|
||
{"role": "user", "content": structured_content},
|
||
]
|
||
|
||
print_messages(messages_c)
|
||
response_c = chat(client, messages_c,temperature=temperature_value)
|
||
print(response_c)
|
||
|
||
|
||
# ── Reflection Questions ──────────────────────────────────────────────────────
|
||
print_separator("Reflection Questions")
|
||
print(
|
||
"1. How did XML structure change the format and depth of the response?\n"
|
||
"2. What happens if you use inconsistent or missing closing tags?\n"
|
||
"3. When would you NOT bother with XML structure?\n"
|
||
"4. How does the system prompt interact with the user message?\n"
|
||
)
|