← Building with AI

Testing AI answers

Write test cases and score your bot.

Start this lesson

You can't tell whether an AI feature works by trying it once. Write a set of test cases: questions with words the answer must contain. Run them all, count how many pass, and look at the failures. Then, every time you change a prompt or a model, run the tests again to see if things got better or worse. This is called an evaluation, or eval.

cases = [
    {"question": "What are your hours?", "must_include": ["9 am", "5 pm"]},
    {"question": "How long do refunds take?", "must_include": ["5 to 7"]},
]
for case in cases:
    reply = ask_model(case["question"])
    ok = all(word.lower() in reply.lower() for word in case["must_include"])
    print("PASS" if ok else "FAIL", case["question"])

all(...) is True only if every required word appears. Lowercasing both sides makes the check ignore capitals.

Keyword checks are simple but useful. Teams also check for words that must NOT appear, check JSON shape, or use a second model to grade answers, with a person checking samples.

Include tricky cases: questions your bot gets wrong today. Those are the ones that show progress.