بتل کد 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.

راهنما

words = answer.split()، بعد if len(words) > max_words: return " ".join(words[:max_words]) + "...".

یک جواب درست

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)
    words = answer.split()
    if len(words) > max_words:
        return " ".join(words[:max_words]) + "..."
    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))

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن