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 pointsRules:1. at most 100 words2. only use HCl and NaOH3. end with 2 practice questions---You are a football coach.Task: Plan a 20-minute warm-up.Format: a numbered listRules: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 با مثال