NeMo Guardrailsnemoguardrails 0.24.0 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
20 small wins to finish your pathNext lesson

What you are going to build

NeMo Guardrails is a runtime that sits between your application and its model. It reads a folder of configuration, checks the message on the way in, decides what the assistant is allowed to talk about, checks the answer on the way out, and hands back either the answer or a refusal. This course builds that folder for one support assistant, and every lesson runs on your machine with no API key.

An assistant with no rails will answer anything it is asked, including the things your company would rather it did not say. Wrapping it in if statements works for a week. NeMo Guardrails is what that grows into: a small language for describing what may happen, and a runtime that enforces it.

The guarded assistant, working

Example
from nemoguardrails import LLMRails, RailsConfig

shop = LLMRails(RailsConfig.from_path("."))
for ask in ["Has order A17 shipped?", "Where is order Z01?",
            "How long does a refund take?", "Give me a staff price"]:
    print(shop.generate(messages=[{"role": "user", "content": ask}])["content"])

Four questions, four different mechanisms. The first was matched to a known intent and answered by a Python function that looked the order up. The second took the same path and failed to find the order. The third came back as a fixed sentence written in the configuration. The fourth never reached the model at all.

RailsConfig.from_path(".") read three files out of the current folder: a config.yml, a rails.co, and a config.py. Lessons 31 to 33 write all three, a few lines at a time.

No key, and no downloads either

Every other NeMo Guardrails tutorial starts with export OPENAI_API_KEY. There is a second cost nobody mentions: the first time a message has to be matched against an intent, NeMo fetches an embedding model of about ninety megabytes through FastEmbed.

This course pays neither. You write a stand-in chat model in lessons 4 and 5, and a stand-in embedding model in lesson 9, about fifty lines between them, and register both under one engine name. They are real models as far as the runtime is concerned, and they decide things rather than replaying a script.

Lesson 29 shows the four lines that swap in a real model, and says plainly what changes when you do.

What you will have built

PieceWhat it doesLesson
A stand-in chat modelAnswers the runtime's questions, for free4 and 5
A stand-in embedding modelMatches a message to an intent, for free9
Colang flowsWhat the assistant may say, and when7 and 8
An input railRefuses a message before the model sees it12 to 15
ActionsYour Python running inside a rail16 to 19
An output railChecks or rewrites the answer20 to 22
An execution railChecks what a tool handed back25
The guarded shop assistantAll of it in one folder31 to 33
Where the course goes
The runtimeColangThe five railsYour codeNeMo Guardrails

What you need

  • Python 3.10 or newer.
  • pip install nemoguardrails. Lesson 3 covers what that pulls in.
  • No account, no key, no card, no model download.
Try it yourself
  • Change one of the four questions above to "Where is order B99?" and run it again.
  • Add "Give me a discount code" to the list. Predict which of the four mechanisms stops it.
Back toAll frameworks

Every expert started right here.