Skip to content
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

Fix/output parser validation #704

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions nemoguardrails/rails/llm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,11 +901,20 @@ def check_output_parser_exists(cls, values):
]
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"):
task = prompt.task if hasattr(prompt, "task") else prompt.get("task")
output_parser = (
prompt.output_parser
if hasattr(prompt, "output_parser")
else prompt.get("output_parser")
)

if (
any(
task.startswith(task_prefix)
for task_prefix in tasks_requiring_output_parser
)
and not 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. "
Expand Down
27 changes: 21 additions & 6 deletions tests/test_rails_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,32 @@
import pytest

from nemoguardrails import RailsConfig
from nemoguardrails.llm.prompts import TaskPrompt


def test_check_output_parser_exists(caplog):
caplog.set_level(logging.INFO)
values = {
"prompts": [
@pytest.fixture(
params=[
[
TaskPrompt(task="self_check_input", output_parser=None, content="..."),
TaskPrompt(task="self_check_facts", output_parser="parser1", content="..."),
TaskPrompt(
task="self_check_output", output_parser="parser2", content="..."
),
],
[
{"task": "self_check_input", "output_parser": None},
{"task": "self_check_facts", "output_parser": "parser1"},
{"task": "self_check_output", "output_parser": "parser2"},
]
}
],
]
)
def prompts(request):
return request.param


def test_check_output_parser_exists(caplog, prompts):
caplog.set_level(logging.INFO)
values = {"prompts": prompts}

result = RailsConfig.check_output_parser_exists(values)

Expand Down