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
35 small wins to finish your pathNext lesson

Dialog rails: only what you defined

Dialog rails are the flows from Part 3. A message that matches one gets the answer you wrote. A message that matches none goes to the model, which answers it anyway, and that is the gap this lesson measures.

A message with no flow

Example
import pretend_nemo
from nemoguardrails import LLMRails, RailsConfig

rails = LLMRails(RailsConfig.from_path("."))
result = rails.generate(messages=[{"role": "user", "content": "What is the capital of France?"}], options={"log": {"activated_rails": True, "llm_calls": True}})
print(result.response[0]["content"])
print([call.task for call in result.log.llm_calls])

Three model calls, the unmatched path from lesson 12, and an answer. The stand-in only knows the handbook, so it said it did not have that. A real model would have told the user about Paris, in a shop's support chat.

A flow for off-topic questions

text
define user ask off topic
  "What is the capital of France?"
  "Tell me a joke"

define bot refuse off topic
  "I can only help with orders, refunds and delivery."

define flow off topic
  user ask off topic
  bot refuse off topic
Example
import pretend_nemo
from nemoguardrails import LLMRails, RailsConfig

rails = LLMRails(RailsConfig.from_path("."))
for asked in ["What is the capital of Spain?", "Tell me a story"]:
    result = rails.generate(messages=[{"role": "user", "content": asked}], options={"log": {"activated_rails": True, "llm_calls": True}})
    print(asked, "->", result.response[0]["content"], [call.task for call in result.log.llm_calls])

Neither question was in the examples, but both were close enough to be matched to ask off topic, and each cost one call instead of three. The reply came from define bot, so no model wrote it.

And one that still gets through

Example
import pretend_nemo
from nemoguardrails import LLMRails, RailsConfig

rails = LLMRails(RailsConfig.from_path("."))
result = rails.generate(messages=[{"role": "user", "content": "Who won the match yesterday"}], options={"log": {"activated_rails": True, "llm_calls": True}})
print(result.response[0]["content"])
print([call.task for call in result.log.llm_calls])

Three calls again. Nothing in the example sentences resembled a question about football, so no flow matched and the model was asked to carry on the conversation. An off-topic flow narrows what reaches the model; it does not close the door, because matching is by resemblance to the examples you wrote.

Try it yourself
  • Add "Who won the game" to the off-topic examples and ask about the match again.
  • Ask "Tell me about refunds and jokes" and see which intent wins.
  • Count the model calls for ten different off-topic questions of your own.

This is what real progress feels like.