جواب «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
راهنما
if "context" in spec: برای خط زمینه. برای قانونها for i, rule in enumerate(spec["constraints"], 1): و lines.append(f"{i}. {rule}").
یک جواب درست
def build_prompt(spec):
lines = ["You are " + spec["role"] + "."]
if "context" in spec:
lines.append("Context: " + spec["context"])
lines.append("Task: " + spec["task"])
lines.append("Format: " + spec["format"])
lines.append("Rules:")
for i, rule in enumerate(spec["constraints"], 1):
lines.append(f"{i}. {rule}")
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))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن