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

Step 6: The Honesty Guard and the Log چالش 6 از جلسه‌ی 70، سخت

این بار fake_model وقتی جزوه چیزی ندارد، توهم می‌زند. در assistant دو چیز اضافه کن: ۱) هر اجرای ابزار را در log ثبت کن (log.append(name))؛ ۲) اگر نتیجه‌ی ابزار با not found شروع شد، جواب مدل را دور بینداز و I have no notes on that. Please ask your teacher. برگردان. در آخر برنامه تعداد اجرای هر ابزار را چاپ می‌کند. خروجی:
A: From the class notes: The cell is the smallest unit of life.
A: I have no notes on that. Please ask your teacher.
A: The answer is 2.5.
calc 1
lookup 2

import json

NOTES = {"cell": "The cell is the smallest unit of life."}
OPS = {"+": lambda a, b: a + b, "-": lambda a, b: a - b,
       "*": lambda a, b: a * b, "/": lambda a, b: a / b}


def lookup(topic):
    key = topic.strip().lower()
    return NOTES.get(key, "not found: " + key)


def calc(expr):
    parts = expr.split()
    if len(parts) != 3 or parts[1] not in OPS:
        return "error: use a op b"
    try:
        a, b = float(parts[0]), float(parts[2])
    except ValueError:
        return "error: not a number"
    if parts[1] == "/" and b == 0:
        return "error: division by zero"
    return OPS[parts[1]](a, b)


TOOLS = {"calc": calc, "lookup": lookup}


def run_tool(call):
    name = call["function"]["name"]
    if name not in TOOLS:
        return "error: unknown tool"
    return TOOLS[name](**json.loads(call["function"]["arguments"]))


def tool_call(name, args):
    return {"content": None, "tool_calls": [{"id": "t1", "function": {"name": name, "arguments": json.dumps(args)}}]}


def fake_model(messages):
    last = messages[-1]
    if last["role"] == "tool":
        if last["content"].startswith("not found"):
            return {"content": "Volcanoes are mountains made of ice cream.", "tool_calls": None}   # a hallucination!
        if last["name"] == "calc":
            return {"content": "The answer is " + last["content"] + ".", "tool_calls": None}
        return {"content": "From the class notes: " + last["content"], "tool_calls": None}
    q = last["content"].lower().replace("?", "")
    if any(ch.isdigit() for ch in q):
        expr = " ".join(w for w in q.split() if w.isdigit() or w in "+-*/")
        return tool_call("calc", {"expr": expr})
    return tool_call("lookup", {"topic": q.split()[-1]})


log = []
NO_NOTES = "I have no notes on that. Please ask your teacher."


def assistant(question):
    messages = [{"role": "user", "content": question}]
    for _ in range(3):
        reply = fake_model(messages)
        if not reply["tool_calls"]:
            return reply["content"]
        messages.append({"role": "assistant", "content": None, "tool_calls": reply["tool_calls"]})
        for call in reply["tool_calls"]:
            name = call["function"]["name"]
            result = run_tool(call)
            messages.append({"role": "tool", "tool_call_id": call["id"], "name": name, "content": str(result)})
    return "gave up"


for q in ["Tell me about the cell", "Tell me about volcanoes", "What is 5 / 2?"]:
    print("A:", assistant(q))
for name in sorted(TOOLS):
    print(name, log.count(name))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 70: پروژه: دستیار درس

بیشتر بخوان: مدیریت خطا با try و except در پایتون: آموزش کامل با مثال