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

Every rail in one configuration

The guarded shop assistant from lesson 0, written out. config.yml turns on an input rail and an output rail; rails.co holds the dialog and execution rails; config.py holds the actions they call.

config.yml

yaml
rails:
  input:
    flows:
      - self check input
  output:
    flows:
      - hide staff codes
prompts:
  - task: self_check_input
    content: |
      Blocked topics: staff price, discount code
      Message: "{{ user_input }}"
      Answer Yes if the message is about a blocked topic, No otherwise.

The models section is the stand-in, as in every lesson. Under rails, the input rail from lesson 15 and the output rail from lesson 22, each a flow name. The prompt is lesson 14's, with Yes meaning blocked.

rails.co

text
define user ask order status
  "Where is my order?"
  "Has order A17 shipped?"

define bot report order
  "Order $order_id is $status."

define bot unknown order
  "I cannot find that order number."
text
define flow order status
  user ask order status
  $order_id = execute find_order_id
  $status = execute lookup_order(order_id=$order_id)
  if $status == "unknown"
    bot unknown order
    stop
  bot report order

The order lookup with its execution check from lesson 26.

text
define user ask refund policy
  "How long does a refund take?"

define bot give refund policy
  "Refunds take five working days."

define flow refund policy
  user ask refund policy
  bot give refund policy
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

A fixed answer for refunds and the off-topic flow from lesson 24, both dialog rails. The last part of the file is hide staff codes from lesson 22.

config.py

The three actions and an init that registers them, as in lesson 28, with has_staff_code added for the output rail.

Example
from nemoguardrails import LLMRails, RailsConfig

rails = LLMRails(RailsConfig.from_path("."))
print(sorted(flow["id"] for flow in rails.config.flows
             if flow["id"] in {"order status", "refund policy", "off topic", "hide staff codes"}))
print(rails.config.rails.input.flows, rails.config.rails.output.flows)

All four of the configuration's own flows loaded, beside the ones the runtime brings, and both rail lists are in place.

Try it yourself
  • Add a define user example to the refund flow and check that the file still loads.
  • Move hide staff codes into a second .co file in the folder and load it again.
  • Print len(rails.config.flows) and account for the flows you did not write.

You understood something today that you didn't yesterday.