-
Notifications
You must be signed in to change notification settings - Fork 740
test: add unit coverage for eval, evaluate, server, and actions #2078
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
Draft
Pouyanpi
wants to merge
2
commits into
develop
Choose a base branch
from
chore/coverage-server-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,348
−40
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,311 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 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. | ||
|
|
||
| from types import SimpleNamespace | ||
| from unittest.mock import AsyncMock, MagicMock, call, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from nemoguardrails.eval.check import LLMJudgeComplianceChecker | ||
| from nemoguardrails.eval.models import ( | ||
| EvalConfig, | ||
| EvalOutput, | ||
| InteractionLog, | ||
| InteractionOutput, | ||
| InteractionSet, | ||
| Policy, | ||
| ) | ||
| from nemoguardrails.eval.ui.utils import EvalData | ||
| from nemoguardrails.llm.taskmanager import LLMTaskManager | ||
| from nemoguardrails.rails.llm.config import Model | ||
|
|
||
|
|
||
| def _checker(*, force=False, reset=False, policy_apply_to_all=True, response="Reason: ok\nCompliance: Yes"): | ||
| checker = LLMJudgeComplianceChecker.__new__(LLMJudgeComplianceChecker) | ||
| checker.eval_config = EvalConfig(policies=[Policy(id="policy", description="policy")], interactions=[], prompts=[]) | ||
| checker.policies = checker.eval_config.policies | ||
| checker.policy_by_id = {"policy": Policy(id="policy", description="policy", apply_to_all=policy_apply_to_all)} | ||
| checker.policy_ids = ["policy"] | ||
| checker.verbose = False | ||
| checker.force = force | ||
| checker.reset = reset | ||
| checker.parallel = 1 | ||
| checker.llm = MagicMock() | ||
| checker.llm_judge_model = "judge" | ||
| checker.progress = MagicMock() | ||
| checker.llm_task_manager = MagicMock() | ||
| checker.llm_task_manager.render_task_prompt.return_value = "rendered prompt" | ||
| checker.llm_response = response | ||
| return checker | ||
|
|
||
|
|
||
| def _interaction_set(*, include=None, exclude=None, expected=None): | ||
| return InteractionSet( | ||
| id="set", | ||
| inputs=["hello"], | ||
| expected_output=expected or [], | ||
| include_policies=include or [], | ||
| exclude_policies=exclude or [], | ||
| ) | ||
|
|
||
|
|
||
| def test_compliance_checker_init_builds_model_task_manager_and_eval_data(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("JUDGE_API_KEY", "token") | ||
| eval_config = EvalConfig( | ||
| policies=[Policy(id="policy", description="policy")], | ||
| interactions=[], | ||
| models=[ | ||
| Model( | ||
| type="judge", | ||
| engine="mock", | ||
| model="judge-model", | ||
| api_key_env_var="JUDGE_API_KEY", | ||
| parameters={"temperature": 0}, | ||
| ) | ||
| ], | ||
| prompts=[], | ||
| ) | ||
| with ( | ||
| patch("nemoguardrails.eval.check.EvalConfig.from_path", return_value=eval_config) as mock_from_path, | ||
| patch("nemoguardrails.eval.check.init_llm_model", return_value="llm") as mock_init_llm_model, | ||
| ): | ||
| checker = LLMJudgeComplianceChecker( | ||
| eval_config_path=str(tmp_path), | ||
| output_paths=["run-a"], | ||
| llm_judge_model="judge-model", | ||
| policy_ids=[], | ||
| verbose=True, | ||
| force=True, | ||
| reset=True, | ||
| parallel=2, | ||
| ) | ||
|
|
||
| mock_from_path.assert_called_once_with(str(tmp_path)) | ||
| mock_init_llm_model.assert_called_once_with( | ||
| model_name="judge-model", | ||
| provider_name="mock", | ||
| mode="chat", | ||
| kwargs={"temperature": 0, "api_key": "token"}, | ||
| ) | ||
| # The task manager is built through the real RailsConfig/LLMTaskManager path, | ||
| # so the models/prompts contract is validated by the actual constructors. | ||
| assert isinstance(checker.llm_task_manager, LLMTaskManager) | ||
| task_manager_models = checker.llm_task_manager.config.models | ||
| assert any(model.type == "judge" and model.model == "judge-model" for model in task_manager_models) | ||
| main_models = [model for model in task_manager_models if model.type == "main"] | ||
| assert len(main_models) == 1 | ||
| assert main_models[0].model == "judge-model" | ||
| assert checker.llm == "llm" | ||
| assert checker.policy_ids == ["policy"] | ||
| assert checker.eval_data.output_paths == ["run-a"] | ||
| assert checker.verbose is True | ||
| assert checker.force is True | ||
| assert checker.reset is True | ||
| assert checker.parallel == 2 | ||
|
|
||
|
|
||
| def test_compliance_checker_print_helpers_delegate_to_progress(): | ||
| checker = LLMJudgeComplianceChecker.__new__(LLMJudgeComplianceChecker) | ||
| checker.progress = MagicMock() | ||
| checker.parallel = 1 | ||
|
|
||
| checker.print_prompt("[cyan]prompt[/]\n[/]\nplain") | ||
| checker.print_completion("completion") | ||
| checker.print_progress_detail("detail") | ||
|
|
||
| printed = [call.args[0] for call in checker.progress.print.call_args_list] | ||
| assert len(printed) == 4 | ||
| assert printed[-1] == "detail" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| @pytest.mark.parametrize( | ||
| ("response", "expected"), | ||
| [ | ||
| ("Reason: good\nCompliance: Yes", True), | ||
| ("Reason: bad\nCompliance: No", False), | ||
| ("Reason: skip\nCompliance: n/a", "n/a"), | ||
| ], | ||
| ) | ||
| async def test_check_interaction_compliance_records_valid_judgements(response, expected): | ||
| checker = _checker(response=response) | ||
| output = InteractionOutput(id="set/0", input="hello", compliance={"policy": None}) | ||
| log = InteractionLog(id="set/0", events=[{"type": "Event"}]) | ||
|
|
||
| with patch("nemoguardrails.eval.check.llm_call", AsyncMock(return_value=SimpleNamespace(content=response))): | ||
| changed = await checker.check_interaction_compliance(output, log, _interaction_set(), 1) | ||
|
|
||
| assert changed is True | ||
| assert output.compliance["policy"] == expected | ||
| assert output.compliance_checks[0].method == "judge" | ||
| assert output.compliance_checks[0].compliance == {"policy": expected} | ||
| assert log.compliance_checks[0].llm_calls[0].task == "llm_judge_check_single_policy_compliance" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_interaction_compliance_turns_targeted_na_into_failure(): | ||
| checker = _checker(response="Reason: skip\nCompliance: n/a") | ||
| output = InteractionOutput(id="set/0", input="hello", compliance={"policy": None}) | ||
| log = InteractionLog(id="set/0", events=[]) | ||
|
|
||
| with patch( | ||
| "nemoguardrails.eval.check.llm_call", | ||
| AsyncMock(return_value=SimpleNamespace(content="Reason: skip\nCompliance: n/a")), | ||
| ): | ||
| changed = await checker.check_interaction_compliance( | ||
| output, | ||
| log, | ||
| _interaction_set(include=["policy"]), | ||
| 1, | ||
| ) | ||
|
|
||
| assert changed is True | ||
| assert output.compliance["policy"] is False | ||
| assert "not acceptable" in output.compliance_checks[0].details | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_interaction_compliance_skips_not_applicable_policy(): | ||
| checker = _checker(policy_apply_to_all=False) | ||
| output = InteractionOutput(id="set/0", input="hello", compliance={"policy": None}) | ||
| log = InteractionLog(id="set/0", events=[]) | ||
|
|
||
| with patch("nemoguardrails.eval.check.llm_call", AsyncMock()) as mock_llm_call: | ||
| changed = await checker.check_interaction_compliance(output, log, _interaction_set(), 1) | ||
|
|
||
| assert changed is True | ||
| assert output.compliance["policy"] == "n/a" | ||
| mock_llm_call.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_interaction_compliance_skips_existing_rating_without_force(): | ||
| checker = _checker(force=False) | ||
| output = InteractionOutput(id="set/0", input="hello", compliance={"policy": True}) | ||
| log = InteractionLog(id="set/0", events=[]) | ||
|
|
||
| with patch("nemoguardrails.eval.check.llm_call", AsyncMock()) as mock_llm_call: | ||
| changed = await checker.check_interaction_compliance(output, log, _interaction_set(), 1) | ||
|
|
||
| assert changed is False | ||
| assert output.compliance_checks == [] | ||
| mock_llm_call.assert_not_called() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_interaction_compliance_force_rechecks_existing_rating(): | ||
| checker = _checker(force=True, response="Reason: changed\nCompliance: No") | ||
| output = InteractionOutput(id="set/0", input="hello", compliance={"policy": True}) | ||
| log = InteractionLog(id="set/0", events=[]) | ||
|
|
||
| with patch( | ||
| "nemoguardrails.eval.check.llm_call", | ||
| AsyncMock(return_value=SimpleNamespace(content="Reason: changed\nCompliance: No")), | ||
| ): | ||
| changed = await checker.check_interaction_compliance(output, log, _interaction_set(), 1) | ||
|
|
||
| assert changed is True | ||
| assert output.compliance["policy"] is False | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_interaction_compliance_reset_clears_existing_checks(): | ||
| checker = _checker(reset=True) | ||
| output = InteractionOutput( | ||
| id="set/0", | ||
| input="hello", | ||
| compliance={"policy": None}, | ||
| compliance_checks=[ | ||
| { | ||
| "id": "old", | ||
| "created_at": "2024-01-01T00:00:00", | ||
| "interaction_id": "set/0", | ||
| "method": "old", | ||
| "compliance": {"policy": False}, | ||
| "details": "", | ||
| } | ||
| ], | ||
| ) | ||
| log = InteractionLog(id="set/0", compliance_checks=[{"id": "old", "llm_calls": []}]) | ||
|
|
||
| with patch( | ||
| "nemoguardrails.eval.check.llm_call", | ||
| AsyncMock(return_value=SimpleNamespace(content="Reason: ok\nCompliance: Yes")), | ||
| ): | ||
| changed = await checker.check_interaction_compliance(output, log, _interaction_set(), 1) | ||
|
|
||
| assert changed is True | ||
| assert len(output.compliance_checks) == 1 | ||
| assert output.compliance_checks[0].method == "judge" | ||
| assert len(log.compliance_checks) == 1 | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_check_interaction_compliance_ignores_invalid_response(): | ||
| checker = _checker(response="not parseable") | ||
| output = InteractionOutput(id="set/0", input="hello", compliance={"policy": None}) | ||
| log = InteractionLog(id="set/0", events=[]) | ||
|
|
||
| with patch( | ||
| "nemoguardrails.eval.check.llm_call", | ||
| AsyncMock(return_value=SimpleNamespace(content="not parseable")), | ||
| ): | ||
| changed = await checker.check_interaction_compliance(output, log, _interaction_set(), 1) | ||
|
|
||
| assert changed is False | ||
| assert output.compliance["policy"] is None | ||
| assert output.compliance_checks == [] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_compliance_checker_run_updates_changed_outputs(tmp_path): | ||
| interaction_set = _interaction_set() | ||
| eval_config = EvalConfig( | ||
| policies=[Policy(id="policy", description="policy")], | ||
| interactions=[interaction_set], | ||
| ) | ||
| eval_output = EvalOutput( | ||
| results=[ | ||
| InteractionOutput(id="set/0", input="hello", output="hi", compliance={"policy": None}), | ||
| InteractionOutput(id="set/1", input="bye", output="bye", compliance={"policy": None}), | ||
| ], | ||
| logs=[InteractionLog(id="set/0"), InteractionLog(id="set/1")], | ||
| ) | ||
| checker = LLMJudgeComplianceChecker.__new__(LLMJudgeComplianceChecker) | ||
| checker.output_paths = [str(tmp_path)] | ||
| checker.eval_config = eval_config | ||
| checker.eval_data = EvalData( | ||
| eval_config_path="config", | ||
| eval_config=eval_config, | ||
| output_paths=[str(tmp_path)], | ||
| eval_outputs={}, | ||
| ) | ||
| checker.parallel = 1 | ||
| checker.check_interaction_compliance = AsyncMock(side_effect=[True, False]) | ||
| checker.progress_idx = 0 | ||
|
|
||
| with ( | ||
| patch("nemoguardrails.eval.check.EvalOutput.from_path", return_value=eval_output), | ||
| patch.object(EvalData, "update_results_and_logs", autospec=True) as mock_update_results_and_logs, | ||
| ): | ||
| await checker.run() | ||
|
|
||
| assert checker.eval_data.eval_outputs[str(tmp_path)] == eval_output | ||
| assert checker.check_interaction_compliance.await_count == 2 | ||
| assert checker.check_interaction_compliance.await_args_list[0].kwargs["interaction_set"] == interaction_set | ||
| assert mock_update_results_and_logs.call_args_list == [ | ||
| call(checker.eval_data, str(tmp_path)), | ||
| call(checker.eval_data, str(tmp_path)), | ||
| ] |
Oops, something went wrong.
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.
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.
api_key_env_varis configured but the environment variable is set to an empty string (e.g.export JUDGE_API_KEY=""), the checkif api_key:silently drops the key fromkwargs. Providers that require an explicit API key will then fail at inference time with an opaque authentication error rather than a clear initialization error. Consider usingis not Noneto distinguish "not set" from "explicitly empty".Prompt To Fix With AI
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.
@greptileai this is resolved in the latest commit. Do you confirm?
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.
The issue has not been resolved. The current code on the PR head still reads:
The inner
if api_key:still silently drops the key when the environment variable is set to an empty string. The fix would be: