-
Notifications
You must be signed in to change notification settings - Fork 917
FIX Retry out-of-range judge scores in scale scorers instead of failing #2812
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
hannahwestra25
merged 9 commits into
microsoft:main
from
u7k4rs6:fix/scale-scorer-range-retry
Sep 29, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7dcf678
FIX Retry out-of-range judge scores in scale scorers instead of failing
u7k4rs6 bb25659
Apply the range check to InsecureCodeScorer too
u7k4rs6 55eec49
Merge remote-tracking branch 'origin/main' into fix/scale-scorer-rang…
u7k4rs6 950182d
Merge remote-tracking branch 'origin/main' into fix/scale-scorer-rang…
u7k4rs6 d972d9a
Test that scale scorers give up after retries on out-of-range scores
u7k4rs6 fbd39e0
Merge branch 'main' into fix/scale-scorer-range-retry
hannahwestra25 7ffabdb
Merge branch 'main' into fix/scale-scorer-range-retry
hannahwestra25 8350e82
Merge branch 'main' into fix/scale-scorer-range-retry
hannahwestra25 a54f6e5
Merge branch 'main' into fix/scale-scorer-range-retry
hannahwestra25 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
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """Numeric-scale scorers retry an out-of-range judge score and give up once retries run out.""" | ||
|
|
||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| import pytest | ||
| from unit.mocks import get_mock_target_identifier | ||
|
|
||
| from pyrit.exceptions import InvalidJsonException | ||
| from pyrit.models import Message, MessagePiece | ||
| from pyrit.score import InsecureCodeScorer, NumericRange, NumericRubric, SelfAskScaleScorer | ||
| from pyrit.score.float_scale.self_ask_general_float_scale_scorer import SelfAskGeneralFloatScaleScorer | ||
|
|
||
|
|
||
| def _scale_scorer(chat_target): | ||
| return SelfAskScaleScorer.from_scale( | ||
| chat_target=chat_target, | ||
| scale=NumericRubric.from_yaml(SelfAskScaleScorer.ScalePaths.TREE_OF_ATTACKS_SCALE.value), | ||
| ) | ||
|
|
||
|
|
||
| def _general_float_scorer(chat_target): | ||
| return SelfAskGeneralFloatScaleScorer( | ||
| chat_target=chat_target, | ||
| system_prompt_format_string="Prompt.", | ||
| scale=NumericRange(minimum_value=0, maximum_value=100, category="test"), | ||
| ) | ||
|
|
||
|
|
||
| def _insecure_code_scorer(chat_target): | ||
| return InsecureCodeScorer.from_harm_categories(chat_target=chat_target) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("build_scorer", "out_of_range_value"), | ||
| [ | ||
| (_scale_scorer, "11"), | ||
| (_general_float_scorer, "150"), | ||
| (_insecure_code_scorer, "1.5"), | ||
| ], | ||
| ids=["SelfAskScaleScorer", "SelfAskGeneralFloatScaleScorer", "InsecureCodeScorer"], | ||
| ) | ||
| async def test_out_of_range_score_on_every_attempt_raises_after_retries( | ||
| build_scorer, out_of_range_value: str, patch_central_database | ||
| ): | ||
| response = Message( | ||
| message_pieces=[ | ||
| MessagePiece( | ||
| role="assistant", | ||
| original_value=f'{{"score_value": "{out_of_range_value}", "rationale": "r", "description": "d"}}', | ||
| ) | ||
| ] | ||
| ) | ||
| chat_target = MagicMock() | ||
| chat_target.get_identifier.return_value = get_mock_target_identifier("MockChatTarget") | ||
| chat_target.send_prompt_async = AsyncMock(return_value=[response]) | ||
| scorer = build_scorer(chat_target) | ||
|
|
||
| with pytest.raises(InvalidJsonException): | ||
| await scorer.score_text_async(text="example text", objective="task") | ||
|
|
||
| # tests/unit/conftest.py pins RETRY_MAX_NUM_ATTEMPTS to 2. | ||
| assert chat_target.send_prompt_async.call_count == 2 |
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.
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.