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

Keep It Short چالش 5 از جلسه‌ی 38، سخت

short_reply(question, max_words) از مدل با system=SYSTEM بپرسد؛ اگر جواب بیشتر از max_words کلمه داشت، فقط max_words کلمه‌ی اول را با فاصله به هم بچسباند و ... به آخرش اضافه کند؛ وگرنه جواب را دست‌نخورده برگرداند. خروجی test:
Volcanoes are mountains where hot...
Yes, it is.

from ai import ask

SYSTEM = "You are a kind teacher. Answer in at most 2 sentences."

def short_reply(question, max_words):
    answer = ask(question, system=SYSTEM)
    return answer


# ---- test with a pretend model (do not change) ----
def fake_ask(prompt, system=None, history=None):
    if "volcano" in prompt:
        return "Volcanoes are mountains where hot melted rock comes up from deep inside the Earth."
    return "Yes, it is."
ask = fake_ask

print(short_reply("What is a volcano?", 5))
print(short_reply("Is the sun a star?", 5))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 38: نقش و شخصیت · چالش بعدی: Score a System Message

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