بتل کد Battle Codeدارد باز می‌شود…

Prompt from a Recipe چالش 6 از جلسه‌ی 62، سخت

تابع build_prompt(spec) را بنویس که از یک دیکشنری پرامپت چندخطی بسازد: خط You are <role>.، اگر کلید context بود خط Context: ...، خط Task: ...، خط Format: ...، و بعد Rules: و قانون‌های لیست constraints شماره‌دار (1. ...). برنامه دو دستور را می‌سازد و بینشان --- چاپ می‌کند. خروجی باید این باشد:
You are a patient chemistry tutor.
Context: I am 15 and have a test on acids tomorrow.
Task: Explain neutralisation.
Format: 5 bullet points
Rules:
1. at most 100 words
2. only use HCl and NaOH
3. end with 2 practice questions
---
You are a football coach.
Task: Plan a 20-minute warm-up.
Format: a numbered list
Rules:
1. no equipment needed

def build_prompt(spec):
    lines = ["You are " + spec["role"] + "."]
    # context (only if present), task, format, then the numbered rules
    return "\n".join(lines)


chem = {
    "role": "a patient chemistry tutor",
    "context": "I am 15 and have a test on acids tomorrow.",
    "task": "Explain neutralisation.",
    "format": "5 bullet points",
    "constraints": ["at most 100 words", "only use HCl and NaOH", "end with 2 practice questions"],
}
coach = {
    "role": "a football coach",
    "task": "Plan a 20-minute warm-up.",
    "format": "a numbered list",
    "constraints": ["no equipment needed"],
}
print(build_prompt(chem))
print("---")
print(build_prompt(coach))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 62: پرامپت‌نویسی پیشرفته ۱

بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال