forked from trustyai-explainability/NeMo-Guardrails
-
Notifications
You must be signed in to change notification settings - Fork 0
fix(self-check): preserve custom task routing #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RobGeada
merged 8 commits into
RobGeada:MultipleSelfChecks
from
Pouyanpi:pr-1874-first-class-self-check-tasks
Jun 26, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fc2438a
test(self-check): expose task routing regressions
Pouyanpi c8051fc
fix(self-check): resolve v1 tasks from rail invocation
Pouyanpi 7f98d6a
fix(self-check): preserve streaming output task
Pouyanpi e62d0f5
fix(self-check): resolve parallel rail task params
Pouyanpi 86d3492
refactor(self-check): share task execution helper
Pouyanpi 7660358
refactor(self-check): use common task rail param
Pouyanpi ab2a8aa
test(self-check): remove comments from multi-rail tests
Pouyanpi 2895b99
refactor(self-check): simplify task fallback tail
Pouyanpi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,48 +14,34 @@ | |
| # limitations under the License. | ||
|
|
||
| import logging | ||
| from typing import Dict, Optional | ||
| from typing import Dict, List, Optional | ||
|
|
||
| from nemoguardrails import RailsConfig | ||
| from nemoguardrails.actions import action | ||
| from nemoguardrails.actions.llm.utils import llm_call, warn_if_truncated | ||
| from nemoguardrails.context import llm_call_info_var | ||
| from nemoguardrails.library.self_check.utils import ( | ||
| SELF_CHECK_OUTPUT_DEFAULT_TASK, | ||
| SELF_CHECK_OUTPUT_FLOW, | ||
| SELF_CHECK_OUTPUT_TASK_PARAM, | ||
| resolve_self_check_task, | ||
| run_self_check_task, | ||
| ) | ||
| from nemoguardrails.llm.taskmanager import LLMTaskManager | ||
| from nemoguardrails.logging.explain import LLMCallInfo | ||
| from nemoguardrails.types import LLMModel | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_TASK = "self_check_output" | ||
|
|
||
|
|
||
| def _get_llm( | ||
| llms: Dict[str, LLMModel], task: str, default_task: str, default_llm: Optional[LLMModel] | ||
| ) -> Optional[LLMModel]: | ||
| if task in llms: | ||
| return llms[task] | ||
| elif default_task in llms: | ||
| log.debug(f"No model found with type={task}, falling back to default {default_task} model") | ||
| return llms[default_task] | ||
| elif default_llm is not None: | ||
| log.debug(f"No model found with type={task} or type={default_task}, falling back to main model") | ||
| return default_llm | ||
| else: | ||
| error_msg = ( | ||
| f"No matching model for task={task} found. " | ||
| f"Please configure a model with type={task}, type={default_task}, or type=main" | ||
| ) | ||
| raise ValueError(error_msg) | ||
| DEFAULT_TASK = SELF_CHECK_OUTPUT_DEFAULT_TASK | ||
|
|
||
|
|
||
| @action(is_system_action=True, output_mapping=lambda value: not value) | ||
| async def self_check_output( | ||
| llms: Dict[str, LLMModel], | ||
| llm_task_manager: LLMTaskManager, | ||
| context: Optional[dict] = None, | ||
| events: Optional[List[dict]] = None, | ||
| llm: Optional[LLMModel] = None, | ||
| config: Optional[RailsConfig] = None, | ||
| task: str = DEFAULT_TASK, | ||
| task: Optional[str] = None, | ||
| **kwargs, | ||
| ): | ||
| """Checks if the output from the bot. | ||
|
|
@@ -70,57 +56,40 @@ async def self_check_output( | |
| True if the output should be allowed, False otherwise. | ||
| """ | ||
|
|
||
| _MAX_TOKENS = 1024 | ||
| context = context or {} | ||
| bot_response = context.get("bot_message") | ||
| user_input = context.get("user_message") | ||
| bot_thinking = context.get("bot_thinking") | ||
|
|
||
| # guard against an unset $task variable | ||
| if task.startswith("$") and task.endswith("_task"): | ||
| task = DEFAULT_TASK | ||
|
|
||
| llm = _get_llm(llms, task, default_task=DEFAULT_TASK, default_llm=llm) | ||
| task = resolve_self_check_task( | ||
| task, | ||
| context, | ||
| events, | ||
| triggered_rail_key="triggered_output_rail", | ||
| start_rail_event_type="StartOutputRail", | ||
| flow_id=SELF_CHECK_OUTPUT_FLOW, | ||
| task_param=SELF_CHECK_OUTPUT_TASK_PARAM, | ||
| default_task=DEFAULT_TASK, | ||
| ) | ||
|
|
||
| if bot_response: | ||
| prompt = llm_task_manager.render_task_prompt( | ||
| is_safe, response = await run_self_check_task( | ||
| task=task, | ||
| context={ | ||
| prompt_context={ | ||
| "user_input": user_input, | ||
| "bot_response": bot_response, | ||
| "bot_thinking": bot_thinking, | ||
| }, | ||
| llms=llms, | ||
| default_task=DEFAULT_TASK, | ||
| main_llm=llm, | ||
| llm_task_manager=llm_task_manager, | ||
| lowest_temperature=config.lowest_temperature, | ||
| ) | ||
| stop = llm_task_manager.get_stop_tokens(task=task) | ||
| max_tokens = llm_task_manager.get_max_tokens(task=task) | ||
| max_tokens = max_tokens or _MAX_TOKENS | ||
|
|
||
| # Initialize the LLMCallInfo object | ||
| llm_call_info_var.set(LLMCallInfo(task=task)) | ||
|
|
||
| llm_response = await llm_call( | ||
| llm, | ||
| prompt, | ||
| stop=stop, | ||
| llm_params={ | ||
| "temperature": config.lowest_temperature, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was used here |
||
| "max_tokens": max_tokens, | ||
| }, | ||
| ) | ||
| warn_if_truncated(llm_response, task) | ||
| response = llm_response.content | ||
|
|
||
| if task == DEFAULT_TASK: | ||
| log.info(f"Output self-checking result is: `{response}`.") | ||
| else: | ||
| log.info(f"Output self-checking result for task={task} is: `{response}`.") | ||
|
|
||
| # for sake of backward compatibility | ||
| # if the output_parser is not registered we will use the default one | ||
| if llm_task_manager.has_output_parser(task): | ||
| result = llm_task_manager.parse_task_output(task, output=response) | ||
| else: | ||
| result = llm_task_manager.parse_task_output(task, output=response, forced_output_parser="is_content_safe") | ||
|
|
||
| is_safe = result[0] | ||
|
|
||
| return is_safe | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably better suited for a separate PR, but I think should be this configurable from the task, not from a global config setting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pre-existing, see https://github.com/RobGeada/NeMo-Guardrails/pull/1/changes#r3480276378 . I don't have full context right now but seems hacky. I'll create an issue to track this