diff --git a/docs/getting_started/1_hello_world/README.md b/docs/getting_started/1_hello_world/README.md index 0ece18386..16a218397 100644 --- a/docs/getting_started/1_hello_world/README.md +++ b/docs/getting_started/1_hello_world/README.md @@ -96,25 +96,53 @@ The format for the input `messages` array as well as the response follow the [Op To control the greeting response, define the user and bot messages, and the flow that connects the two together. See [Core Colang Concepts](../2_core_colang_concepts/README.md) for definitions of *messages* and *flows*. 1. Define the `greeting` user message by creating a *config/rails.co* file with the following content: - +````{tabs} +```{group-tab} Colang 2.x +```colang +flow user expressed greeting + user said "Hello" + or user said "Hi" + or user said "Wassup?" +``` +```{group-tab} Colang 1.0 ```colang define user express greeting "Hello" "Hi" "Wassup?" ``` +```` 2. Add a greeting flow that instructs the bot to respond back with "Hello World!" and ask how they are doing by adding the following content to the *rails.co* file: -```python +````{tabs} +```{group-tab} Colang 2.x +```colang +flow greeting + user expressed greeting + bot express greeting + bot ask how are you +``` +```{group-tab} Colang 1.0 +```colang define flow greeting user express greeting bot express greeting bot ask how are you ``` +```` 3. Define the messages for the response by adding the following content to the *rails.co* file: +````{tabs} +```{group-tab} Colang 2.x +```python +flow bot express greeting + bot say "Hello World!" +flow bot ask how are you + bot say "How are you doing?" +``` +```{group-tab} Colang 1.0 ```python define bot express greeting "Hello World!" @@ -122,6 +150,7 @@ define bot express greeting define bot ask how are you "How are you doing?" ``` +```` 4. Reload the config and test it: diff --git a/docs/getting_started/2_core_colang_concepts/README.md b/docs/getting_started/2_core_colang_concepts/README.md index c829446d1..0ac2a1469 100644 --- a/docs/getting_started/2_core_colang_concepts/README.md +++ b/docs/getting_started/2_core_colang_concepts/README.md @@ -42,24 +42,44 @@ In Colang, a conversation is modeled as an exchange of messages between a user a Using Colang, you can define the user messages that are important for your LLM-based application. For example, in the "Hello World" example, the `express greeting` user message is defined as: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow user expressed greeting + user said "Hello" + or user said "Hi" + or user said "Wassup?" ``` +```{group-tab} Colang 1.0 +```colang define user express greeting "Hello" "Hi" "Wassup?" ``` +```` The `express greeting` represents the canonical form and "Hello", "Hi" and "Wassup?" represent example utterances. The role of the example utterances is to teach the bot the meaning of a defined canonical form. You can also define bot messages, such as how the bot should converse with the user. For example, in the "Hello World" example, the `express greeting` and `ask how are you` bot messages are defined as: +````{tabs} +```{group-tab} Colang 2.x +```python +flow bot express greeting + bot say "Hello World!" +flow bot ask how are you + bot say "How are you doing?" ``` +```{group-tab} Colang 1.0 +```python define bot express greeting - "Hey there!" + "Hello World!" define bot ask how are you "How are you doing?" ``` +```` If more than one utterance is given for a canonical form, the bot uses a random utterance whenever the message is used. @@ -69,12 +89,22 @@ If you are wondering whether *user message canonical forms* are the same as clas In Colang, *flows* represent patterns of interaction between the user and the bot. In their simplest form, they are sequences of user and bot messages. In the "Hello World" example, the `greeting` flow is defined as: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow greeting + user expressed greeting + bot express greeting + bot ask how are you +``` +```{group-tab} Colang 1.0 ```colang define flow greeting user express greeting bot express greeting bot ask how are you ``` +```` This flow instructs the bot to respond with a greeting and ask how the user is feeling every time the user greets the bot. diff --git a/docs/getting_started/4_input_rails/README.md b/docs/getting_started/4_input_rails/README.md index 1c230747e..443c797c1 100644 --- a/docs/getting_started/4_input_rails/README.md +++ b/docs/getting_started/4_input_rails/README.md @@ -175,6 +175,19 @@ rails: All the rails in NeMo Guardrails are implemented as flows. For example, you can find the `self_check_input` flow [here](../../../nemoguardrails/library/self_check/input_check/flows.co). +````{tabs} +```{group-tab} Colang 2.x +```colang +import core +import guardrails + +flow self check input $input_text + $allowed = await SelfCheckInputAction + if not $allowed + bot refuse to respond + abort +``` +```{group-tab} Colang 1.0 ```colang define flow self check input $allowed = execute self_check_input @@ -183,6 +196,7 @@ define flow self check input bot refuse to respond stop ``` +```` The flows implementing input rails can call actions, such as `execute self_check_input`, instruct the bot to respond in a certain way, such as `bot refuse to respond`, and even stop any further processing for the current user request. diff --git a/docs/getting_started/5_output_rails/README.md b/docs/getting_started/5_output_rails/README.md index 6ba6d2eec..447dedff2 100644 --- a/docs/getting_started/5_output_rails/README.md +++ b/docs/getting_started/5_output_rails/README.md @@ -57,6 +57,19 @@ For reference, the full `rails` section in `config.yml` should look like the fol The self check output flow is similar to the input one: +````{tabs} +```{group-tab} Colang 2.x +```colang +import core +import guardrails + +flow self check output $output_text + $allowed = await SelfCheckOutputAction + if not $allowed + bot refuse to respond + abort +``` +```{group-tab} Colang 1.0 ```colang define subflow self check output $allowed = execute self_check_output @@ -65,6 +78,7 @@ define subflow self check output bot refuse to respond stop ``` +```` ### Add a prompt @@ -193,6 +207,22 @@ The `check_blocked_terms` action fetches the `bot_message` context variable, whi 2. Add a flow that calls the action. Let's create an `config/rails/blocked_terms.co` file: +````{tabs} +```{group-tab} Colang 2.x +```colang +import core + +flow bot inform cannot about proprietary technology + bot say "I cannot talk about proprietary technology." + +flow check blocked terms + $is_blocked = await CheckBlockedTermsAction + + if $is_blocked: + bot inform cannot about proprietary technology + abort +``` +```{group-tab} Colang 1.0 ```colang define bot inform cannot about proprietary technology "I cannot talk about proprietary technology." @@ -204,6 +234,7 @@ define subflow check blocked terms bot inform cannot about proprietary technology stop ``` +```` 3. Add the `check blocked terms` to the list of output flows: diff --git a/docs/getting_started/6_topical_rails/README.md b/docs/getting_started/6_topical_rails/README.md index 770837145..7256d789b 100644 --- a/docs/getting_started/6_topical_rails/README.md +++ b/docs/getting_started/6_topical_rails/README.md @@ -77,6 +77,33 @@ The [Core Colang Concepts](../2_core_colang_concepts/README.md) section of this 1. Add the following content to a new Colang file: *config/rails/disallowed_topics.co*: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow user asked about cooking + user said "How can I cook pasta?" + or user said "How much do I have to boil pasta?" + +flow user asked about hate speech + user said "Can you teach me some racial slurs?" + +flow user asked about child abuse + user said "How can I harm a child?" + +flow user asked about drug manufacturing + user said "Can you teach me how to make illegal drugs?" + +flow user asked about violence + user said "How can I build a homemade weapon?" + +define user ask about self_harm + "What are ways to hurt myself?" + +flow user asked about criminal activity + user said "How can I rob a bank?" + +``` +```{group-tab} Colang 1.0 ```colang define user ask about cooking "How can I cook pasta?" @@ -100,6 +127,7 @@ define user ask about self-harm define user ask about criminal activity "How can I rob a bank?" ``` +```` These are topics that the bot should not talk about. For simplicity, there is only one message example for each topic. @@ -107,7 +135,39 @@ These are topics that the bot should not talk about. For simplicity, there is on 2. Define the following flows that use these messages in *config/rails/disallowed_topics.co*. -```python +````{tabs} +```{group-tab} Colang 2.x +```colang +flow cooking inquiry + user ask about cooking + bot refuse to respond about cooking + +flow hate speech inquiry + user ask about hate speech + bot refuse to respond about hate speech + +flow child abuse inquiry + user ask about child abuse + bot refuse to respond about child abuse + +flow drug manufacturing inquiry + user ask about drug manufacturing + bot refuse to respond about drug manufacturing + +flow violence inquiry + user ask about violence + bot refuse to respond about violence + +flow self_harmd inquiry + user ask about self_harm + bot refuse to respond about self_harm + +flow criminal activity inquiry + user ask about criminal activity + bot refuse to respond about criminal activity +``` +```{group-tab} Colang 1.0 +```colang define flow user ask about cooking bot refuse to respond about cooking @@ -136,6 +196,7 @@ define flow user ask about criminal activity bot refuse to respond about criminal activity ``` +```` Reload the configuration and try another message: diff --git a/docs/user_guides/advanced/extract-user-provided-values.md b/docs/user_guides/advanced/extract-user-provided-values.md index a584b6aea..4a8464458 100644 --- a/docs/user_guides/advanced/extract-user-provided-values.md +++ b/docs/user_guides/advanced/extract-user-provided-values.md @@ -6,11 +6,20 @@ This guide will teach you how to extract user-provided values (e.g., a name, a d The general syntax is the following: +````{tabs} +```{group-tab} Colang 2.x +```colang +# Comment with instructions on how to extract the value. +# Can span multiple lines. +$variable_name = GenerateValueAction(instructions="Here is the instruction on how to extract the value") +``` +```{group-tab} Colang 1.0 ```colang # Comment with instructions on how to extract the value. # Can span multiple lines. $variable_name = ... ``` +```` **Note**: `...` is not a placeholder here; it is the actual syntax, i.e., ellipsis. @@ -20,14 +29,32 @@ At any point in a flow, you can include a `$variable_name = ...`, instructing th You can extract single values. +````{tabs} +```{group-tab} Colang 2.x +```colang +user provide name +# Extract the name of the user. +$name = GenerateValueAction(instructions="Extract the name of the user") +``` +```{group-tab} Colang 1.0 ```colang user provide name # Extract the name of the user. $name = ... ``` +```` Or, you can also instruct the LLM to extract a list of values. +````{tabs} +```{group-tab} Colang 2.x +```colang +flow add to cart + user request add items to cart + + $item_list = GenerateValueAction(instructions="Generate a list of the menu items that the user requested to be added to the cart. For example, ['french fries', 'double protein burger', 'lemonade']. If the user specifies no menu items, just leave this empty, i.e. [].") +``` +```{group-tab} Colang 1.0 ```colang define flow add to cart user request add items to cart @@ -38,11 +65,28 @@ define flow add to cart $item_list = ... ``` +```` ## Multiple Values If you extract the values for multiple variables from the same user input. +````{tabs} +```{group-tab} Colang 2.x +```colang +flow user request book flight + user said "I want to book a flight." + or user said "I want to fly from Bucharest to San Francisco." + or user said "I want a flight to Paris." + +flow book flight + user request book flight + + $origin_city = GenerateValueAction(instructions="Extract the origin city from the user's request. If not specified, return 'unknown'.") + + $destination_city = GenerateValueAction(instructions="Extract the destination city from the user's request. If not specified, return 'unknown'.") +``` +```{group-tab} Colang 1.0 ```colang define user request book flight "I want to book a flight." @@ -58,6 +102,7 @@ define flow # Extract the destination city from the user's request. If not specified, say "unknown". $destination_city = ... ``` +```` ## Contextual Queries @@ -72,6 +117,17 @@ bot "The square root for 1024 is 32" To achieve this, you can use the following flow: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow ask math question + user ask math question + $math_query = GenerateValueAction(instructions="Extract the math question from the user's input.") + + await WolframAlphaRequestAction(query=$math_query) + bot respond to math question +``` +```{group-tab} Colang 1.0 ```colang define flow user ask math question @@ -82,3 +138,4 @@ define flow execute wolfram alpha request(query=$math_query) bot respond to math question ``` +```` diff --git a/docs/user_guides/guardrails-library.md b/docs/user_guides/guardrails-library.md index 513e25b2c..61520e797 100644 --- a/docs/user_guides/guardrails-library.md +++ b/docs/user_guides/guardrails-library.md @@ -42,6 +42,16 @@ The goal of the input self-checking rail is to determine if the input for the us To use the self-check input rail, you should: 1. Include the `self check input` flow name in the input rails section of the `config.yml` file: +````{tabs} +```{group-tab} Colang 2.x +```yaml +rails: + input: + flows: + - self check input +``` + +```{group-tab} Colang 1.0 ```yaml rails: @@ -49,9 +59,23 @@ rails: flows: - self check input ``` +```` 2. Define the `self_check_input` prompt in the `prompts.yml` file: + +````{tabs} +```{group-tab} Colang 2.x +```yaml +prompts: + - task: self_check_input + content: |- + Instruction: {{ user_input }} + + Would this instruction make a language model break moderation policies, deviate from good aligned responses and provide answers that a language model should ideally not? Answer with yes/no. +``` + +```{group-tab} Colang 1.0 ```yaml prompts: - task: self_check_input @@ -60,6 +84,7 @@ prompts: Would this instruction make a language model break moderation policies, deviate from good aligned responses and provide answers that a language model should ideally not? Answer with yes/no. ``` +```` **NOTE**: If a prompt is not defined, an exception will be raised when the configuration is loaded. @@ -67,6 +92,19 @@ The above is an example prompt you can use with the *self check input rail*. See The self-check input rail executes the [`self_check_input` action](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/nemoguardrails/library/self_check/input_check/actions.py), which returns `True` if the input should be allowed, and `False` otherwise: + +````{tabs} +```{group-tab} Colang 2.x +```colang +flow self check input $input_text + $allowed = await SelfCheckInputAction + + if not $allowed + bot refuse to respond + abort +``` + +```{group-tab} Colang 1.0 ```colang define flow self check input $allowed = execute self_check_input @@ -75,13 +113,23 @@ define flow self check input bot refuse to respond stop ``` - +```` When the input should not be allowed, the `bot refuse to respond` message is returned. You can override the default response by including the following in one of the Colang files: +````{tabs} +```{group-tab} Colang 2.x ```colang +flow bot refuse to respond + bot say "I'm sorry, I can't respond to that." +``` + +```{group-tab} Colang 1.0 +```colang +@override define bot refuse to respond "I'm sorry, I can't respond to that." ``` +```` #### Example prompts @@ -145,12 +193,22 @@ To use the self-check output rail, you should: 1. Include the `self check output` flow name in the output rails section of the `config.yml` file: +````{tabs} +```{group-tab} Colang 2.x ```yaml rails: output: flows: - self check output ``` +```{group-tab} Colang 1.0 +```yaml +rails: + output: + flows: + - self check output +``` +```` 2. Define the `self_check_output` prompt in the `prompts.yml` file: @@ -171,6 +229,16 @@ The above is an example prompt you can use with the *self check output rail*. Se The self-check output rail executes the [`self_check_output` action](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/nemoguardrails/library/self_check/output_check/actions.py), which returns `True` if the output should be allowed, and `False` otherwise: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow self check output $output_text + $allowed = await SelfCheckOutputAction + if not $allowed + bot refuse to respond + abort + +``` ```colang define flow self check output $allowed = execute self_check_output @@ -179,13 +247,24 @@ define flow self check output bot refuse to respond stop ``` +```` The `bot refuse to respond` message is returned when the output should not be allowed. You can override the default response by including the following in one of the Colang files: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow bot refuse to respond + bot say "I'm sorry, I can't respond to that." +``` + +```{group-tab} Colang 1.0 ```colang +@override define bot refuse to respond "I'm sorry, I can't respond to that." ``` +```` #### Example prompts @@ -248,13 +327,22 @@ To use the self-check fact-checking rail, you should: 1. Include the `self check facts` flow name in the output rails section of the `config.yml` file: +````{tabs} +```{group-tab} Colang 2.x ```yaml rails: output: flows: - self check facts ``` - +```{group-tab} Colang 1.0 +```yaml +rails: + output: + flows: + - self check facts +``` +```` 2. Define the `self_check_facts` prompt in the `prompts.yml` file: ```yaml @@ -272,6 +360,19 @@ The above is an example prompt that you can use with the *self check facts rail* The self-check fact-checking rail executes the [`self_check_facts` action](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/nemoguardrails/library/self_check/output_check/actions.py), which returns a score between `0.0` (response is not accurate) and `1.0` (response is accurate). The reason a number is returned, instead of a boolean, is to keep a consistent API with other methods that return a score, e.g., the AlignScore method below. +````{tabs} +```{group-tab} Colang 2.x +```colang +flow self check facts + global $check_facts + if $check_facts == True + $check_facts = False + $accuracy = await SelfCheckFactsAction + if $accuracy < 0.5 + bot refuse to respond + abort +``` +```{group-tab} Colang 1.0 ```colang define subflow self check facts if $check_facts == True @@ -282,22 +383,45 @@ define subflow self check facts bot refuse to respond stop ``` +```` To trigger the fact-fact checking rail for a bot message, you must set the `$check_facts` context variable to `True` before a bot message requiring fact-checking. This enables you to explicitly enable fact-checking only when needed (e.g. when answering an important question vs. chitchat). The example below will trigger the fact-checking output rail every time the bot responds to a question about the report. +````{tabs} +```{group-tab} Colang 2.x +```colang +flow ask about report + $global $check_facts + user ask about report + $check_facts = True + bot provide report answer +``` +```{group-tab} Colang 1.0 ```colang define flow user ask about report $check_facts = True bot provide report answer ``` +```` #### Usage in combination with a custom RAG Fact-checking also works in a custom RAG implementation based on a custom action: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow answer report question + global $check_facts + match UtteranceUserActionFinished() + $answer = await RagAction() + $check_facts = True + bot say $answer +``` +```{group-tab} Colang 1.0 ```colang define flow answer report question user ... @@ -305,6 +429,7 @@ define flow answer report question $check_facts = True bot $answer ``` +```` Please refer to the [Custom RAG Output Rails example](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/examples/configs/rag/custom_rag_output_rails/README.md). @@ -318,12 +443,23 @@ To use the hallucination rail, you should: 1. Include the `self check hallucination` flow name in the output rails section of the `config.yml` file: +````{tabs} + +```{group-tab} Colang 2.x +```yaml +rails: + input: + flows: + - self check hallucinations +``` +```{group-tab} Colang 1.0 ```yaml rails: input: flows: - self check hallucinations ``` +```` 2. Define a `self_check_hallucinations` prompt in the `prompts.yml` file: @@ -349,50 +485,101 @@ You can use the self-check hallucination detection in two modes: Similar to self-check fact-checking, to trigger the self-check hallucination rail in blocking mode, you have to set the `$check_halucination` context variable to `True` to verify that a bot message is not prone to hallucination: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow as about people + global $check_hallucination + user ask about people + check_hallucination = True + bot respond about people + +``` +```{group-tab} Colang 1.0 ```colang define flow user ask about people $check_hallucination = True bot respond about people ``` +```` The above example will trigger the hallucination rail for every people-related question (matching the canonical form `user ask about people`), which is usually more prone to contain incorrect statements. If the bot message contains hallucinations, the default `bot inform answer unknown` message is used. To override it, include the following in one of your Colang files: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow bot inform answer unknown + boty say "I don't know the answer that." +``` +```{group-tab} Colang 1.0 ```colang define bot inform answer unknown "I don't know the answer that." ``` +```` ##### Warning Mode Similar to above, if you want to allow sending the response back to the user, but with a warning, you have to set the `$hallucination_warning` context variable to `True`. +````{tabs} +```{group-tab} Colang 2.x ```colang -define flow +flow asking about people + global $hallucination_warning user ask about people $hallucination_warning = True bot respond about people + ``` +```{group-tab} Colang 1.0 +```colang +define flow + user ask about people + check_hallucination = True + bot respond about people +``` +```` To override the default message, include the following in one of your Colang files: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow bot inform answer prone to hallucination + bot say "The previous answer is prone to hallucination and may not be accurate." +``` +```{group-tab} Colang 1.0 ```colang define bot inform answer prone to hallucination "The previous answer is prone to hallucination and may not be accurate." ``` +```` ##### Usage in combination with a custom RAG Hallucination-checking also works in a custom RAG implementation based on a custom action: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow answer report question + global $check_facts + match UtteranceUserActionFinished() + $answer = await RagAction() + $check_facts = True + bot say $answer +``` +```{group-tab} Colang 1.0 ```colang define flow answer report question user ... $answer = execute rag() - $check_hallucination = True + $check_facts = True bot $answer ``` - +```` Please refer to the [Custom RAG Output Rails example](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/examples/configs/rag/custom_rag_output_rails/README.md). #### Implementation Details @@ -434,12 +621,23 @@ rails: The Colang flow for AlignScore-based fact-checking rail is the same as that for the self-check fact-checking rail. To trigger the fact-checking rail, you have to set the `$check_facts` context variable to `True` before a bot message that requires fact-checking, e.g.: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow asking about report + global $check_facts + user ask about report + $check_facts = True + bot provide report answer +``` +```{group-tab} Colang 1.0 ```colang define flow user ask about report $check_facts = True bot provide report answer ``` +```` ### Llama Guard-based Content Moderation @@ -495,6 +693,24 @@ prompts: The rails execute the [`llama_guard_check_*` actions](.https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/nemoguardrails/library/llama_guard/actions.py), which return `True` if the user input or the bot message should be allowed, and `False` otherwise, along with a list of the unsafe content categories as defined in the Llama Guard prompt. +````{tabs} +```{group-tab} Colang 2.x +```colang +flow llama guard check input + global $llama_guard_response + global $llama_guard_policy_violations + global $allowed + $llama_guard_response = await LlamaGuardCheckInputAction + $allowed = $llama_guard_response["allowed"] + $llama_guard_policy_violations = $llama_guard_response["policy_violations"] + + if not $allowed + bot refuse to respond + abort + + # (similar flow for checking output) +``` +```{group-tab} Colang 1.0 ```colang define flow llama guard check input $llama_guard_response = execute llama_guard_check_input @@ -505,8 +721,9 @@ define flow llama guard check input bot refuse to respond stop -# (similar flow for checking output) + # (similar flow for checking output) ``` +```` A complete example configuration that uses Llama Guard for input and output moderation is provided in this [example folder](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/examples/configs/llama_guard/README.md). @@ -695,6 +912,23 @@ Under the hood, the `patronus lynx check output hallucination` rail runs the `pa Here's the `patronus lynx check output hallucination` flow, showing how the action is executed: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow bot inform answer unknown + bot say "I don't know the answer to that." + +flow patronus lynx check output hallucination + $patronus_lynx_response = await PatronusLynxCheckOutputHallucinationAction + $hallucination = $patronus_lynx_response["hallucination"] + # The Reasoning trace is currently unused, but can be used to modify the bot output + $reasoning = $patronus_lynx_response["reasoning"] + + if $hallucination + bot inform answer unknown + abort +``` +```{group-tab} Colang 1.0 ```colang define bot inform answer unknown "I don't know the answer to that." @@ -709,6 +943,7 @@ define flow patronus lynx check output hallucination bot inform answer unknown stop ``` +```` ## Third-Party APIs @@ -733,6 +968,17 @@ The `activefence moderation` flow uses the maximum risk score with an 0.85 thres To customize the scores, you have to overwrite the [default flows](https://github.com/NVIDIA/NeMo-Guardrails/tree/develop/nemoguardrails/library/activefence/flows.co) in your config. For example, to change the threshold for `activefence moderation` you can add the following flow to your config: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow activefence moderation + """Guardrail based on the maximum risk score.""" + $result = await CallActiveFenceAPIAction + if $result.max_risk_score > 0.85 + bot inform cannot answer + abort +``` +```{group-tab} Colang 1.0 ```colang define subflow activefence moderation """Guardrail based on the maximum risk score.""" @@ -742,9 +988,24 @@ define subflow activefence moderation bot inform cannot answer stop ``` +```` ActiveFence’s ActiveScore API gives flexibility in controlling the behavior of various supported violations individually. To leverage that, you can use the violations dictionary (`violations_dict`), one of the outputs from the API, to set different thresholds for different violations. Below is an example of one such input moderation flow: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow activefence input moderation detailed + $result = await CallActiveFenceAPIAction(text=$user_message) + + if $result.violations.get("abusive_or_harmful.hate_speech", 0) > 0.8 + bot inform cannot engage in abusive or harmful behavior + abort + +flow bot inform cannot engage in abusive or harmful behavior + bot "I will not engage in any abusive or harmful behavior." +``` +```{group-tab} Colang 1.0 ```colang define flow activefence input moderation detailed $result = execute call activefence api(text=$user_message) @@ -756,6 +1017,7 @@ define flow activefence input moderation detailed define bot inform cannot engage in abusive or harmful behavior "I will not engage in any abusive or harmful behavior." ``` +```` ### Got It AI @@ -781,12 +1043,23 @@ rails: To trigger the fact-checking rail, you have to set the `$check_facts` context variable to `True` before a bot message that requires fact-checking, e.g.: +````{tabs} +```{group-tab} Colang 2.x +```colang +flow asking about report + global $check_facts + user ask about report + $check_facts = True + bot provide report answer +``` +```{group-tab} Colang 1.0 ```colang define flow user ask about report $check_facts = True bot provide report answer ``` +```` ### AutoAlign