NeMo-Guardrails icon indicating copy to clipboard operation
NeMo-Guardrails copied to clipboard

How to modify the user input message before it goes through a given flow

Open msalhab96 opened this issue 1 year ago • 3 comments

I want to perform a preprocessing step in some flows. How can I modify the user input message across these flows? Specifically, I aim to alter the last_user_message and proceed with the flow as usual. I attempted to modify it in place by executing an action and setting the returned message to last_user_message using $last_user_message = execute my_action(new_user_message=$last_user_message). However, when the request reaches my LLM, the original message is still being passed through.

msalhab96 avatar Feb 20 '24 15:02 msalhab96

Hi @msalhab96 !

The way to achieve this is to define an input rail flow which changes the $user_message variable, not $last_user_message. See a quick example in this test: https://github.com/NVIDIA/NeMo-Guardrails/blob/develop/tests/test_input_ouput_rails_no_dialog.py (where "!" is appended to the input).

drazvan avatar Feb 20 '24 19:02 drazvan

@drazvan Thanks for you response,

I'm wondering, is there any way to apply this to only specific flow? I mean if I use input rail flow it's going to preprocess the input message before it goes to any flow, but I want to do that in a specific flows not all of them.

msalhab96 avatar Feb 21 '24 08:02 msalhab96

You could set a context variable in your flow, and use that to conditionally trigger the logic in the input flow:

define flow your flow
  user some intent
  $mark_in_your_flow = True
  ...
  $mark_in_your_flow = False
define flow the input rail flow
  if $mark_in_your_flow
    ...
    $user_message = ...

drazvan avatar Feb 21 '24 10:02 drazvan