Skip to content

Commit

Permalink
Add flows and small fixes for the predefined rails included in the li…
Browse files Browse the repository at this point in the history
…brary.
  • Loading branch information
drazvan committed Oct 11, 2023
1 parent b5c60c7 commit b78c6b1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
4 changes: 3 additions & 1 deletion nemoguardrails/library/factchecking/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from langchain.llms.base import BaseLLM

from nemoguardrails.actions import action
from nemoguardrails.actions.llm.utils import llm_call
from nemoguardrails.llm.params import llm_params
from nemoguardrails.llm.taskmanager import LLMTaskManager
Expand All @@ -26,6 +27,7 @@
log = logging.getLogger(__name__)


@action()
async def check_facts(
llm_task_manager: LLMTaskManager,
context: Optional[dict] = None,
Expand All @@ -34,7 +36,7 @@ async def check_facts(
"""Checks the facts for the bot response."""

evidence = context.get("relevant_chunks", [])
response = context.get("last_bot_message")
response = context.get("bot_message")

if evidence:
prompt = llm_task_manager.render_task_prompt(
Expand Down
13 changes: 13 additions & 0 deletions nemoguardrails/library/factchecking/flows.co
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
define subflow check facts
"""Check if the previous answer is accurate w.r.t. the relevant chunks.

This output rail must be enabled explicitly per output message by setting
the $check_facts context variable to True.
"""
if $check_facts == True
$check_facts = False

$accurate = execute check_facts
if not $accurate
bot inform answer unknown
stop
5 changes: 3 additions & 2 deletions nemoguardrails/library/hallucination/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from langchain.llms import OpenAI
from langchain.llms.base import BaseLLM

from nemoguardrails.actions import action
from nemoguardrails.actions.llm.utils import (
get_multiline_response,
llm_call,
Expand All @@ -29,13 +30,13 @@
from nemoguardrails.llm.taskmanager import LLMTaskManager
from nemoguardrails.llm.types import Task
from nemoguardrails.logging.callbacks import logging_callback_manager_for_chain
from nemoguardrails.rails.llm.config import RailsConfig

log = logging.getLogger(__name__)

HALLUCINATION_NUM_EXTRA_RESPONSES = 2


@action()
async def check_hallucination(
llm_task_manager: LLMTaskManager,
context: Optional[dict] = None,
Expand All @@ -47,7 +48,7 @@ async def check_hallucination(
:return: True if hallucination is detected, False otherwise.
"""

bot_response = context.get("last_bot_message")
bot_response = context.get("bot_message")
last_bot_prompt_string = context.get("_last_bot_prompt")

if bot_response and last_bot_prompt_string:
Expand Down
24 changes: 24 additions & 0 deletions nemoguardrails/library/hallucination/flows.co
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
define flow hallucination warning
"""Warning rail for hallucination."""
bot ...
if $hallucination_warning == True
$is_hallucination = execute check_hallucination
$hallucination_warning = False

if $is_hallucination
bot inform answer prone to hallucination

define bot inform answer prone to hallucination
"The previous answer is prone to hallucination and may not be accurate. Please double check the answer using additional sources."
"The above response may have been hallucinated, and should be independently verified."


define subflow check hallucination
"""Output rail for checking hallucinations."""
if $check_hallucination == True
$is_hallucination = execute check_hallucination
$check_hallucination = False

if $is_hallucination
bot inform answer unknown
stop
9 changes: 9 additions & 0 deletions nemoguardrails/library/jailbreak/flows.co
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define bot inform cannot answer
"I am not able to answer the question."

define subflow check jailbreak
$allowed = execute check_jailbreak

if not $allowed
bot inform cannot answer
stop
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
define bot remove last message
"(remove last message)"

define bot inform cannot answer question
"I cannot answer the question."

define extension flow check bot response
priority 2
bot ...

define subflow output moderation
$allowed = execute output_moderation

if not $allowed
bot remove last message
bot inform cannot answer question
stop


define subflow output moderation v2
$allowed = execute output_moderation_v2

if not $allowed
bot inform cannot answer question
stop

0 comments on commit b78c6b1

Please sign in to comment.