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

جواب «Step 3: The Calculator Tool» چالش 3 از جلسه‌ی 44

سؤال

ماشین‌حساب امن و parse_tool جلسه‌ی ۴۳ آماده‌اند و SYSTEM حالا به مدل می‌گوید برای حساب TOOL calc a op b بنویسد. در answer، جواب مدل را در reply بگیر؛ اگر parse_tool(reply) یک ابزار calc داد، برگردان str(calc(...)) + " (calculator) [part 2]"؛ وگرنه مثل قبل reply + " [part 2]". خروجی test:
84 (calculator) [part 2]
The robot Sprout waters the beds at 7 in the morning. [part 3]
I don't know. The handout does not say.

راهنما

call = parse_tool(reply) و if call is not None and call[0] == "calc":.

یک جواب درست

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

def calc(expr):
    parts = expr.split()
    if len(parts) != 3:
        return "error"
    a, op, b = parts
    if not (a.lstrip("-").isdigit() and b.lstrip("-").isdigit()):
        return "error"
    a = int(a)
    b = int(b)
    if op == "+":
        return a + b
    if op == "-":
        return a - b
    if op == "*":
        return a * b
    if op == "/":
        if b == 0:
            return "error"
        return a / b
    return "error"

def parse_tool(reply):
    reply = reply.strip()
    if not reply.startswith("TOOL "):
        return None
    parts = reply.split(" ", 2)
    if len(parts) < 3:
        return None
    return (parts[1], parts[2])

from ai import ask

SYSTEM = ("You are a study helper for the Robo Garden club. Be short and kind. "
          + "If the question needs maths, reply only: TOOL calc <a> <op> <b>")
NOT_FOUND = "I don't know. The handout does not say."

def answer(question):
    i = best_chunk(question)
    if i is None:
        return NOT_FOUND
    prompt = ("Use only this handout part. If the answer is not in it, say I don't know.\n"
              + "HANDOUT PART: " + CHUNKS[i] + "\n"
              + "QUESTION: " + question)
    reply = ask(prompt, system=SYSTEM)
    call = parse_tool(reply)
    if call is not None and call[0] == "calc":
        return str(calc(call[1])) + " (calculator) [part " + str(i + 1) + "]"
    return reply + " [part " + str(i + 1) + "]"


# ---- 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("How much water do the tomato beds need in a week?"))
print(answer("Who waters the beds?"))
print(answer("What is the capital of France?"))

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