An assistant that answers anything
Before any framework, here is the thing that needs guarding: a support assistant for an online shop, written as a dictionary and a loop. It is six lines and it has no opinions at all.
Lesson 0 ran the finished configuration. This lesson starts at the other end, with plain Python you already understand, so that everything NeMo Guardrails adds later is visible as an addition rather than as magic.
The handbook
HANDBOOK = {
"a17": "Order A17 shipped on 3 March by courier.",
"refund": "Refunds take five working days.",
"delivery": "Delivery is free on orders over 40 pounds.",
}Three facts. A real shop has thousands, and a real assistant asks a model instead of a dictionary, but the shape is the same: a question comes in, an answer goes out.
The assistant
def reply(question):
for word in question.lower().replace("?", "").split():
if word in HANDBOOK:
return HANDBOOK[word]
return "I do not have that in the handbook."
print(reply("Has order A17 shipped?"))
print(reply("How long does a refund take?"))
print(reply("Who won the cup final?"))It answers what it knows and says so when it does not. That last line is the only safety behaviour in the whole program, and it only works because the handbook is a dictionary. Point the same function at a model and it will happily answer about the cup final.
What is missing
Nothing here checks the question before answering, nothing checks the answer before sending it, and nothing decides which topics the assistant is in business to discuss. Those three gaps are the input rail, the output rail and the dialog rail, and they are the whole subject of this course.
- Add a fourth entry to
HANDBOOKand ask about it. - Ask
reply("Can I have the refund address?")and work out why the answer is the refund policy.
Little by little, you're building something great.