NVIDIA / NeMo-Guardrails

NeMo Guardrails is an open-source toolkit for easily adding programmable guardrails to LLM-based conversational systems.
Other
4.16k stars 391 forks source link

Setting variables in Colang scripts #91

Closed jamescalam closed 1 year ago

jamescalam commented 1 year ago

Hi, more of a Colang question (let me know if there's another place for these).

I'm trying to set a variable in a simple example script which looks like:

define user express greeting
    "hello"
    "hi"
    "how are you"

define user give name
    "James"
    "Julio"
    "Mary"
    "Putu"

define bot name greeting
    "Hey $name!"

define flow greeting
    user express greeting
    if $name
        bot name greeting
    else
        bot express greeting
        bot ask name

define flow give name
    user give name
    $name = $last_user_message
    bot name greeting

Here I'd expect $name to be set to the same value as $last_user_message, but it is set to None and returns "Hey None!"

Any idea how this should work? I saw set in the Colang Language Reference doc, but that doesn't seem to do anything. I understand I can use actions to do this, but I was hoping for a simpler way if possible, any idea on how I should set the $name variable here?

drazvan commented 1 year ago

Hi @jamescalam! Thanks for reporting this. It is indeed a bug. I've already tested a fix and will be pushed to the main branch in the next day or so.

Meanwhile, you can try an alternative method, which will work even better (although at the cost of an extra LLM call):

define flow give name
    user give name
    # Extract the user's name
    $name = ...
    bot name greeting

It uses the mechanism for extracting user provided values: https://github.com/NVIDIA/NeMo-Guardrails/blob/main/docs/user_guide/advanced/extract-user-provided-values.md.

The advantage of this approach is that if the user responds with "My name is John", in this approach the name will be extracted correctly, whereas in your initial code the name would have been the full sentence.

I'll close this once the fix is pushed.

Thanks again!

drazvan commented 1 year ago

For the record, this is now fixed (https://github.com/NVIDIA/NeMo-Guardrails/commit/1ace2a7891c56469ad786397cc5bce1e0e1e0fa9).