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
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
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.
- Delete
initfromconfig.pyand read the reply for order A17. - Add a
prompts.ymlfile to the folder with theself_check_inputprompt and check that it is picked up. - Load the same folder twice in one script and check that
pretend_nemois only registered once.
Slow is fine. Stopping is the only problem.