Skip to content

Commit db1854f

Browse files
committed
fix: apply review suggestions by mike
1 parent 28483f9 commit db1854f

File tree

6 files changed

+20
-32
lines changed

6 files changed

+20
-32
lines changed

nemoguardrails/actions/actions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def action(
3737
name (Optional[str]): The name to associate with the action.
3838
execute_async: Whether the function should be executed in async mode.
3939
output_mapping (Optional[Callable[[Any], bool]]): A function to interpret the action's result.
40-
It should accept the return value (e.g. the first element of a tuple) and return True if the output
41-
should be considered blocked.
40+
It accepts the return value (e.g. the first element of a tuple) and return True if the output
41+
is not safe.
42+
4243
Returns:
4344
callable: The decorated function or class.
4445
"""

nemoguardrails/actions/llm/generation.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ async def generate_bot_message(
766766
# we must disable (skip) the output rails which gets executed on $bot_message
767767
# as it is executed separately in llmrails.py
768768
# of course, it does not work when passed as context in `run_output_rails_in_streaming`
769-
# streming_handler is set when stream_async method is used
769+
# streaming_handler is set when stream_async method is used
770770
if streaming_handler and self.config.rails.output.streaming.enabled:
771771
context_updates["skip_output_rails"] = True
772772

@@ -787,9 +787,7 @@ async def generate_bot_message(
787787
context_updates["skip_output_rails"] = True
788788

789789
# Check if the output is supposed to be the content of a context variable
790-
# TODO: add this bug fix
791-
# elif bot_intent and bot_intent[0] == "$" and bot_intent[1:] in context:
792-
elif bot_intent[0] == "$" and bot_intent[1:] in context:
790+
elif bot_intent and bot_intent[0] == "$" and bot_intent[1:] in context:
793791
bot_utterance = context[bot_intent[1:]]
794792

795793
else:

nemoguardrails/actions/output_mapping.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def default_output_mapping(result: Any) -> bool:
3131
return False
3232

3333

34-
def should_block_output(result: Any, action_func: Any) -> bool:
35-
"""Determines if an action result should be blocked using its attached mapping.
34+
def is_output_blocked(result: Any, action_func: Any) -> bool:
35+
"""Determines if an action result is not allowed using its attached mapping.
3636
3737
Args:
3838
result: The value returned by the action.

nemoguardrails/rails/llm/buffer.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from abc import ABC, abstractmethod
1717
from typing import AsyncGenerator, List, Tuple
1818

19-
from nemoguardrails.rails.llm.config import OutputRailsStreamingConfig, RailsConfig
19+
from nemoguardrails.rails.llm.config import OutputRailsStreamingConfig
2020

2121

2222
class BufferStrategy(ABC):
@@ -104,16 +104,5 @@ def generate_chunk_str(self, buffer, current_index) -> str:
104104

105105
def get_buffer_strategy(config: OutputRailsStreamingConfig) -> BufferStrategy:
106106
# TODO: use a factory function or class
107-
# currently we only have SlidingWindo, in future we have a registry
107+
# currently we only have SlidingWindow, in future we use a registry
108108
return SlidingWindow.from_config(config)
109-
110-
111-
def is_blocked(result: Tuple[bool, str]) -> bool:
112-
"""Check output rials status."""
113-
# result is a tuple of (return_value, success|failure)
114-
# return_value is not unified among all the actions
115-
# sometimes it is a bool where True means allowed, sometimes True means blocked
116-
# sometimes it is a score where allowed/blocked is dictated in flows.co
117-
# lack of stable interface for output rails cause this issue
118-
# for self_check_output following holds
119-
return not result[0]

nemoguardrails/rails/llm/llmrails.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
from nemoguardrails.actions.llm.generation import LLMGenerationActions
3333
from nemoguardrails.actions.llm.utils import get_colang_history
34-
from nemoguardrails.actions.output_mapping import should_block_output
34+
from nemoguardrails.actions.output_mapping import is_output_blocked
3535
from nemoguardrails.actions.v2_x.generation import LLMGenerationActionsV2dotx
3636
from nemoguardrails.colang import parse_colang_file
3737
from nemoguardrails.colang.v1_0.runtime.flows import compute_context
@@ -1303,7 +1303,7 @@ def _update_explain_info():
13031303
action_func = self.runtime.action_dispatcher.get_action(action_name)
13041304

13051305
# Use the mapping to decide if the result indicates blocked content.
1306-
if should_block_output(result, action_func):
1306+
if is_output_blocked(result, action_func):
13071307
# TODO: while whitespace issue is fixed, remove the space from below
13081308
yield " {DATA: STOP}"
13091309
return

tests/test_actions_output_mapping.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from nemoguardrails.actions import action
2020
from nemoguardrails.actions.output_mapping import (
2121
default_output_mapping,
22-
should_block_output,
22+
is_output_blocked,
2323
)
2424

2525
# Tests for default_output_mapping
@@ -70,21 +70,21 @@ def test_should_block_output_with_tuple_result_and_mapping():
7070
# Test should_block_output when the result is a tuple and the dummy mapping is used.
7171
# When the first element equals "block", we expect True.
7272
result = ("block",)
73-
assert should_block_output(result, dummy_action) is True
73+
assert is_output_blocked(result, dummy_action) is True
7474

7575
# When the result is not "block", we expect False.
7676
result = ("allow",)
77-
assert should_block_output(result, dummy_action) is False
77+
assert is_output_blocked(result, dummy_action) is False
7878

7979

8080
def test_should_block_output_with_non_tuple_result_and_mapping():
8181
# Test should_block_output when the result is not a tuple.
8282
# The function should wrap it into a tuple.
8383
result = "block"
84-
assert should_block_output(result, dummy_action) is True
84+
assert is_output_blocked(result, dummy_action) is True
8585

8686
result = "allow"
87-
assert should_block_output(result, dummy_action) is False
87+
assert is_output_blocked(result, dummy_action) is False
8888

8989

9090
def test_should_block_output_without_action_meta():
@@ -98,9 +98,9 @@ def action_without_meta(res):
9898
del action_without_meta.action_meta
9999

100100
# Test with a boolean: default_output_mapping for True is False and for False is True.
101-
assert should_block_output(True, action_without_meta) is False
102-
assert should_block_output(False, action_without_meta) is True
101+
assert is_output_blocked(True, action_without_meta) is False
102+
assert is_output_blocked(False, action_without_meta) is True
103103

104104
# Test with a numeric value: block if < 0.5.
105-
assert should_block_output(0.4, action_without_meta) is True
106-
assert should_block_output(0.6, action_without_meta) is False
105+
assert is_output_blocked(0.4, action_without_meta) is True
106+
assert is_output_blocked(0.6, action_without_meta) is False

0 commit comments

Comments
 (0)