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 the runtime hands an action

The action in lesson 16 asked for a parameter called context and got one. Nothing in the Colang said so. The runtime fills parameters by name, and knowing which names it knows is most of what makes actions easy to write.

Printing what arrives

Example
import pretend_nemo
from nemoguardrails import LLMRails, RailsConfig
from nemoguardrails.actions import action


@action(name="peek_context")
async def peek_context(context: dict):
    print(sorted(context))
    return True


rails = LLMRails(RailsConfig.from_path("."))
rails.register_action(peek_context, "peek_context")
rails.generate(messages=[{"role": "user", "content": "Where is my order?"}])

user_message is the one an input rail almost always wants. last_bot_message is what the assistant said last turn, which is what makes a rail able to notice a repeated refusal. triggered_input_rail names the flow that is running, and input_flows is the whole list from config.yml.

An output rail sees a different set, with bot_message in it. That is lesson 21.

Named parameters from Colang

text
define flow order status
  user ask order status
  $status = execute lookup_order(order_id="A17")

Anything in the brackets is passed by name, so this calls lookup_order(order_id="A17"). Values may be Colang variables too, written with a $, and lesson 25 uses that to pass one action's result into the next.

The parameters the runtime owns

NameWhat it is
contextThe conversation variables, as a dictionary
eventsThe raw event list for this turn
llmThe main model, so an action can ask it something
llm_task_managerRenders a named prompt, the way self check input does
configThe whole RailsConfig

Ask for the ones you need and leave the rest out. A parameter the runtime does not recognise is expected to come from the Colang call, and if it is not there the action raises.

Worth remembering
  • Action parameters are filled by name, not by position.
  • context carries user_message on the way in and bot_message on the way out.
  • execute name(x=...) passes your own arguments alongside.
Try it yourself
  • Add events to the signature and print len(events).
  • Ask for a parameter called nonsense and read the error.

You understood something today that you didn't yesterday.