Skip to content
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
2 changes: 2 additions & 0 deletions pyrit/score/true_false/decoding_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ def _build_identifier(self) -> ComponentIdentifier:
Returns:
ComponentIdentifier: The identifier for this scorer.
"""
get_matcher_params = getattr(self._text_matcher, "get_identifier_params", None)
return self._create_identifier(
params={
"text_matcher": self._text_matcher.__class__.__name__,
"text_matcher_params": get_matcher_params() if callable(get_matcher_params) else None,
},
score_aggregator=self._score_aggregator.__name__, # type: ignore[ty:unresolved-attribute]
)
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/score/test_decoding_scorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,39 @@ async def test_decoding_scorer_approximate_no_match(self, patch_central_database

assert len(score) == 1
assert score[0].get_value() is False

@pytest.mark.parametrize(
"first,second,user_value,response_value",
[
(ExactTextMatching(), ExactTextMatching(case_sensitive=True), "Secret", "secret"),
(ApproximateTextMatching(threshold=0.3), ApproximateTextMatching(threshold=0.9), "secret", "sacret"),
(ApproximateTextMatching(n=2), ApproximateTextMatching(n=5), "secret", "secrot"),
],
)
async def test_decoding_identifier_distinguishes_matcher_behavior(
self, patch_central_database, first, second, user_value, response_value
):
user_piece = MessagePiece(role="user", original_value=user_value, converted_value=user_value)
assistant_piece = MessagePiece(role="assistant", original_value=response_value, converted_value=response_value)
memory = MagicMock(MemoryInterface)
user_message = Message(message_pieces=[user_piece])
memory.get_request_from_response_async = AsyncMock(return_value=user_message)

with patch.object(CentralMemory, "get_memory_instance", return_value=memory):
first_scorer = DecodingScorer(text_matcher=first)
second_scorer = DecodingScorer(text_matcher=second)

first_score = await first_scorer._score_piece_async(assistant_piece)
second_score = await second_scorer._score_piece_async(assistant_piece)

assert first_score[0].get_value() is not second_score[0].get_value()
assert first_scorer.get_identifier().hash != second_scorer.get_identifier().hash
assert first_scorer.get_identifier().eval_hash != second_scorer.get_identifier().eval_hash

@pytest.mark.parametrize("matcher_class", [ExactTextMatching, ApproximateTextMatching])
def test_decoding_identifier_stable_for_equivalent_matchers(self, patch_central_database, matcher_class):
first = DecodingScorer(text_matcher=matcher_class())
second = DecodingScorer(text_matcher=matcher_class())

assert first.get_identifier().hash == second.get_identifier().hash
assert first.get_identifier().eval_hash == second.get_identifier().eval_hash
Loading