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

A Privacy Guard چالش 6 از جلسه‌ی 65، سخت

قبل از فرستادن هر پیام به مدل، یک نگهبان بگذار. تابع is_private(text) را بنویس که True برگرداند اگر متن یکی از این‌ها را دارد: علامت @ (ایمیل)، کلمه‌ی password (با هر حروف بزرگ و کوچکی)، یا ۸ رقم یا بیشتر پشت سر هم (تلفن یا شماره‌ی مدرک). برنامه برای پیام‌های خصوصی BLOCKED چاپ می‌کند و بقیه را به fake_api می‌فرستد. خروجی:
Paris.
BLOCKED
BLOCKED
BLOCKED
2 + 2 = 4.

CANNED = {"What is the capital of France?": "Paris.", "What is 2 + 2?": "2 + 2 = 4."}


def fake_api(request):
    # A pretend server: no internet, a few ready answers, the real reply shape.
    question = request["messages"][-1]["content"]
    answer = CANNED.get(question, "I am only a pretend model.")
    return {"choices": [{"message": {"role": "assistant", "content": answer}, "finish_reason": "stop"}]}


def is_private(text):
    return False


messages = [
    "What is the capital of France?",
    "My email is sara.k@example.com, remember it",
    "My Password is tiger77",
    "Call me on 09121234567 tonight",
    "What is 2 + 2?",
]
for m in messages:
    if is_private(m):
        print("BLOCKED")
    else:
        reply = fake_api({"model": "deepseek-chat", "messages": [{"role": "user", "content": m}]})
        print(reply["choices"][0]["message"]["content"])

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 65: مدل زبانی از بیرون

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