Skip to content

Commit

Permalink
Merge pull request #691 from NVIDIA/fix/output-parser-deprecation-war…
Browse files Browse the repository at this point in the history
…ning

Fix/output parser deprecation warning
  • Loading branch information
drazvan authored Aug 22, 2024
2 parents 7edf8d7 + bd522cf commit 1e38fbf
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
5 changes: 0 additions & 5 deletions nemoguardrails/library/self_check/facts/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ async def self_check_facts(
if llm_task_manager.has_output_parser(task):
result = llm_task_manager.parse_task_output(task, output=response)
else:
log.warn(
f"Deprecation Warning: Output parser is not registered for the task. "
f"The correct way is to register the 'output_parser' in the prompts.yml for {task.value}. "
"This behavior will be deprecated in future versions.",
)
result = llm_task_manager.output_parsers["is_content_safe"](response)

is_not_safe, _ = result
Expand Down
5 changes: 0 additions & 5 deletions nemoguardrails/library/self_check/input_check/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ async def self_check_input(
result = llm_task_manager.parse_task_output(task, output=response)

else:
log.warn(
f"Deprecation Warning: Output parser is not registered for the task. "
f"The correct way is to register the 'output_parser' in the prompts.yml for {task.value}. "
"This behavior will be deprecated in future versions.",
)
result = llm_task_manager.output_parsers["is_content_safe"](response)

is_safe, _ = result
Expand Down
5 changes: 0 additions & 5 deletions nemoguardrails/library/self_check/output_check/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ async def self_check_output(
if llm_task_manager.has_output_parser(task):
result = llm_task_manager.parse_task_output(task, output=response)
else:
log.warn(
f"Deprecation Warning: Output parser is not registered for the task. "
f"The correct way is to register the 'output_parser' in the prompts.yml for {task.value}. "
"This behavior will be deprecated in future versions.",
)
result = llm_task_manager.output_parsers["is_content_safe"](response)

is_safe, _ = result
Expand Down
24 changes: 24 additions & 0 deletions nemoguardrails/rails/llm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,30 @@ def check_prompt_exist_for_self_check_rails(cls, values):

return values

@root_validator(pre=True, allow_reuse=True)
def check_output_parser_exists(cls, values):
tasks_requiring_output_parser = [
"self_check_input",
"self_check_facts",
"self_check_output",
# "content_safety_check input $model",
# "content_safety_check output $model",
]
prompts = values.get("prompts", [])
for prompt in prompts:
task = prompt.get("task")
if any(
task.startswith(task_prefix)
for task_prefix in tasks_requiring_output_parser
) and not prompt.get("output_parser"):
log.info(
f"Deprecation Warning: Output parser is not registered for the task. "
f"The correct way is to register the 'output_parser' in the prompts.yml for '{task}' task. "
"It uses 'is_content safe' as the default output parser."
"This behavior will be deprecated in future versions."
)
return values

@root_validator(pre=True, allow_reuse=True)
def fill_in_default_values_for_v2_x(cls, values):
instructions = values.get("instructions", {})
Expand Down
40 changes: 40 additions & 0 deletions tests/test_rails_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

import pytest

from nemoguardrails import RailsConfig


def test_check_output_parser_exists(caplog):
caplog.set_level(logging.INFO)
values = {
"prompts": [
{"task": "self_check_input", "output_parser": None},
{"task": "self_check_facts", "output_parser": "parser1"},
{"task": "self_check_output", "output_parser": "parser2"},
]
}

result = RailsConfig.check_output_parser_exists(values)

assert result == values
assert (
"Deprecation Warning: Output parser is not registered for the task."
in caplog.text
)
assert "self_check_input" in caplog.text

0 comments on commit 1e38fbf

Please sign in to comment.