Repository navigation
fix: redact toxicity as well as profanity #2002
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
Open
feiiiiii5
wants to merge
5
commits into
openlit:main
Choose a base branch
from
feiiiiii5:feiiiiii5/issue-contract-openlit-moderation
base: main
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b2e842b
fix: redact toxicity as well as profanity
feiiiiii5 88a7029
style: remove trailing blank line in test file
feiiiiii5 b6b4e1f
fix(guard): redact overlapping moderation spans in one pass
feiiiiii5 e7120fb
test(guard): document the recording guard class
feiiiiii5 05555e7
fix(guard): drop redundant scan-tail overrides after upstream base ch…
feiiiiii5 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 |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| """Tests for the Pipeline guard engine.""" | ||
|
|
||
| from typing import List | ||
|
|
||
| import pytest | ||
|
|
||
| from openlit.guard._base import ( | ||
| Guard, | ||
| GuardAction, | ||
| GuardPhase, | ||
| GuardResult, | ||
| ) | ||
| from openlit.guard._pipeline import Pipeline | ||
| from openlit.guard.pii import PII | ||
|
|
@@ -14,6 +17,21 @@ | |
| from openlit.guard.schema import Schema | ||
|
|
||
|
|
||
| class RecordingGuard(Guard): | ||
| """Records each evaluated string so tests can inspect downstream input.""" | ||
|
|
||
| name = "recorder" | ||
| phases = (GuardPhase.PREFLIGHT,) | ||
|
|
||
| def __init__(self, seen: List[str]) -> None: | ||
| super().__init__(action="allow") | ||
| self._seen = seen | ||
|
|
||
| def evaluate(self, text: str) -> GuardResult: | ||
| self._seen.append(text) | ||
| return GuardResult(guard_name=self.name) | ||
|
|
||
|
|
||
| class TestPipelineBasics: | ||
| """Core pipeline behaviour: ordering, allow/deny, redaction chaining.""" | ||
|
|
||
|
|
@@ -197,3 +215,71 @@ def test_latency_recorded(self): | |
| pipeline = Pipeline(guards=[PII(action="deny")]) | ||
| result = pipeline.evaluate("[email protected]", phase="preflight") | ||
| assert result.results[0].latency_ms >= 0 | ||
|
|
||
|
|
||
| class TestModerationRedaction: | ||
| """Redact must replace every flagged span, not just profanity.""" | ||
|
|
||
| def test_toxicity_is_redacted(self): | ||
| """Toxicity-only text is redacted and the toxic phrase is removed.""" | ||
| result = Moderation(action="redact").evaluate("kill yourself") | ||
|
|
||
| assert result.action == GuardAction.REDACT | ||
| assert result.classification == "toxicity" | ||
| assert result.score == 0.95 | ||
| assert result.transformed_text is not None | ||
| assert "[REDACTED:toxicity]" in result.transformed_text | ||
| assert "kill yourself" not in result.transformed_text.lower() | ||
|
|
||
| def test_profanity_redaction_still_works(self): | ||
| """Profanity-only redaction keeps its existing placeholder.""" | ||
| result = Moderation(action="redact").evaluate("what the damn hell") | ||
|
|
||
| assert result.action == GuardAction.REDACT | ||
| assert result.transformed_text is not None | ||
| assert "[REDACTED:profanity]" in result.transformed_text | ||
| assert "damn" not in result.transformed_text.lower() | ||
|
|
||
| def test_clean_text_unchanged(self): | ||
| """Clean text is allowed with no transformation.""" | ||
| result = Moderation(action="redact").evaluate("Hello world") | ||
|
|
||
| assert result.action == GuardAction.ALLOW | ||
| assert result.transformed_text is None | ||
| assert result.classification == "" | ||
|
|
||
| def test_pipeline_forwards_redacted_toxicity(self): | ||
| """The pipeline no longer forwards toxic text to later guards.""" | ||
| pipeline = Pipeline(guards=[Moderation(action="redact")]) | ||
| result = pipeline.evaluate("kill yourself", phase="preflight") | ||
|
|
||
| assert result.action == GuardAction.REDACT | ||
| assert result.transformed_text is not None | ||
| assert "[REDACTED:toxicity]" in result.transformed_text | ||
| assert "kill yourself" not in result.transformed_text.lower() | ||
|
|
||
| def test_overlapping_custom_word_does_not_break_toxicity_redaction(self): | ||
| """A custom word inside a toxic phrase cannot expose the rest of it.""" | ||
| result = Moderation(action="redact", custom_words=["I'll"]).evaluate("I'll kill you") | ||
|
|
||
| assert result.classification == "profanity, toxicity" | ||
| assert result.transformed_text == "[REDACTED:toxicity]" | ||
| assert "kill you" not in result.transformed_text | ||
|
|
||
| def test_long_input_keeps_unscanned_suffix(self): | ||
| """A capped scan does not truncate the text returned to callers.""" | ||
| text = "kill yourself and more text" | ||
| result = Moderation(action="redact", max_scan_length=20).run(text, GuardPhase.PREFLIGHT) | ||
|
|
||
| assert result.transformed_text == "[REDACTED:toxicity] and more text" | ||
|
|
||
| def test_later_guard_receives_redacted_toxicity(self): | ||
| """Downstream guards see the placeholder instead of the original phrase.""" | ||
| seen: List[str] = [] | ||
| pipeline = Pipeline( | ||
| guards=[Moderation(action="redact"), RecordingGuard(seen)], | ||
| fail_open=False, | ||
| ) | ||
| pipeline.evaluate("kill yourself", phase="preflight") | ||
|
|
||
| assert seen == ["[REDACTED:toxicity]"] | ||
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
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.