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

Step 2: Answer with a Source چالش 2 از جلسه‌ی 44، متوسط

answer(question): تکه را با best_chunk پیدا کن؛ اگر None بود NOT_FOUND را بدون صدا زدن مدل برگردان. وگرنه پرامپت سه‌خطی بساز:
Use only this handout part. If the answer is not in it, say I don't know.
HANDOUT PART: + تکه
QUESTION: + سؤال
و ask(prompt, system=SYSTEM) + " [part 3]" را برگردان (شماره = اندیس + ۱). مدل ساختگی test جمله‌ی اول تکه را برمی‌گرداند. خروجی:
The robot Sprout waters the beds at 7 in the morning. [part 3]
Ripe tomatoes are picked on Fridays. [part 4]
I don't know. The handout does not say.

HANDOUT = """The Robo Garden club meets every Tuesday after school.
The club has 12 members and one teacher.

The garden has 4 tomato beds and 2 herb beds.
Each tomato bed needs 3 liters of water every day.

The robot Sprout waters the beds at 7 in the morning.
Sprout runs on a small solar panel.

Ripe tomatoes are picked on Fridays.
Herbs are picked whenever the cook asks for them."""

STOP = {"the", "a", "an", "is", "of", "in", "on", "and", "what", "how", "do", "does", "it", "at", "to", "many"}

def words(text):
    clean = ""
    for ch in text.lower():
        if ch in ".,?!":
            clean += " "
        else:
            clean += ch
    result = set()
    for w in clean.split():
        if w not in STOP:
            result.add(w)
    return result

def split_chunks(text):
    chunks = []
    for part in text.split("\n\n"):
        part = part.strip()
        if part != "":
            chunks.append(part)
    return chunks

def score(question, chunk):
    return len(words(question) & words(chunk))

CHUNKS = split_chunks(HANDOUT)

def best_chunk(question):
    best = None
    best_score = 0
    for i in range(len(CHUNKS)):
        s = score(question, CHUNKS[i])
        if s > best_score:
            best = i
            best_score = s
    return best

from ai import ask

SYSTEM = "You are a study helper for the Robo Garden club. Be short and kind."
NOT_FOUND = "I don't know. The handout does not say."

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


# ---- test with a pretend model (do not change) ----
def fake_ask(prompt, system=None, history=None):
    if "HANDOUT PART: " not in prompt:
        return "???"
    part = prompt.split("HANDOUT PART: ")[1]
    if "week" in prompt and "TOOL" in (system or ""):
        return "TOOL calc 12 * 7"
    return part.split(".")[0] + "."
ask = fake_ask

print(answer("Who waters the beds?"))
print(answer("When are tomatoes picked?"))
print(answer("What is the capital of France?"))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 44: پروژه: دستیار درس از روی جزوه · چالش بعدی: Step 3: The Calculator Tool

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