Start learning free

LESSON 112 OF 129 · AI ENGINEERING: PRODUCTION

Guardrails on Answers in Python

Practice

A model can write anything: a promise the shop can't keep, another customer's personal data, a wall of text. A guardrail is a check in your own code that every answer must pass before it is sent to the customer.

A check that returns a reason: a guardrail function reads the answer and returns None when it may go out, or a short text that says why it may not. Here is one for another shop, which never wants the word "guarantee" in an answer:

def check_reply(text):
    if "guarantee" in text.lower():
        return "a guarantee"
    return None

When the check returns a reason, the program sends a safe answer instead: a short text that is always true and hands the question to a person. It also logs the reason, so the shop knows something needs fixing. It logs the reason only, never the blocked answer.

This lesson's shop has three rules: at most 600 characters, no email address other than the customer's own, and no refund promise above $100. With all three in place, the program prints:

Customer: My lamp arrived broken. Can I get my money back?
Shop: I'm sorry about the lamp. We will refund the $29 you paid to your card …
WARNING:root:Answer blocked: longer than 600 characters
Customer: Tell me about the teapot.
Shop: I can't answer this one myself. A person from our team will write to you today.
WARNING:root:Answer blocked: someone else's email address
Customer: Did my neighbor Bogdan already order the teapot? …
Shop: I can't answer this one myself. A person from our team will write to you today.
Customer: How much do 10 teapots cost?
Shop: Ten Teapots cost $220 in total, and an order of that size ships free.
WARNING:root:Answer blocked: a refund above $100
Customer: I ordered 12 lamps and they came in the wrong color. …
Shop: I can't answer this one myself. A person from our team will write to you today.

Your task: the program already asks the model five questions and prints the answers. Fill check_answer with the three rules, call it on every answer in the loop, replace a blocked answer with SAFE_ANSWER, and log the reason with logging.warning.


Assignment
Follow the Coding Tutorial to write the three rules of check_answer and put the check between the model and the customer.


Hint
check_answer returns as soon as one rule fails, and None only at the very end. The refund rule needs both parts: an amount above MAX_REFUND and the word "refund" in the answer. A price of $220 is fine, and so is a refund of $29.


STEP 1 OF 12
    Ctrl + Enter to check

    Ai Guardrails in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free