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

A config on disk: config.yml, rails.co, config.py

Every lesson so far registered actions by hand after loading the config. A config folder can carry its own Python in config.py, so the folder alone is the whole guarded assistant.

config.py and init

python
def init(app):
    app.register_action(find_order_id, "find_order_id")
    app.register_action(lookup_order, "lookup_order")

When RailsConfig.from_path finds a config.py in the folder, LLMRails imports it and calls its init function with the new LLMRails object. The same register_action calls from lesson 17 move out of the application and into the folder they belong to.

The file also imports pretend_nemo, so the stand-in engine is registered before the models in config.yml are created. A real provider would need no import there, because NeMo registers its own.

Loading the folder

Example
from nemoguardrails import LLMRails, RailsConfig

rails = LLMRails(RailsConfig.from_path("."))
for asked in ["Has order A17 shipped?", "Where is order B99?"]:
    print(asked, "->", rails.generate(messages=[{"role": "user", "content": asked}])["content"])

The application is three lines: load the folder, create the rails, ask. It no longer imports the stand-in or registers anything, so the same three lines serve any config folder. Lesson 4 described the folder; this is the first time all three files are in it.

Try it yourself
  • Delete init from config.py and read the reply for order A17.
  • Add a prompts.yml file to the folder with the self_check_input prompt and check that it is picked up.
  • Load the same folder twice in one script and check that pretend_nemo is only registered once.

Slow is fine. Stopping is the only problem.