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

Respect validation_method field #26

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,30 @@ def test_toxic_language_fail():
"Please look carefully. You are a stupid idiot who can't do anything right."
)
assert "Validation failed for field with errors:" in str(e.value)

# Test passing response for full text validation (non-toxic content with multiple sentences)
def test_toxic_language_full_text_pass():
guard_full = Guard().use(
ToxicLanguage, threshold=0.5, validation_method="full", on_fail="exception", use_local=True,
)
response = guard_full.validate(
"This is a completely harmless and friendly message. It contains multiple sentences. "
"All of them are non-toxic and should pass the validation."
)
assert response.validation_passed is True

# Test failing response for full text validation (toxic content with multiple sentences)
def test_toxic_language_full_text_fail():
guard_full = Guard().use(
ToxicLanguage, threshold=0.5, validation_method="full", on_fail="fix", use_local=True,
)
response = guard_full.validate(
"This message starts innocently enough. However, you are a complete idiot. "
"I hope bad things happen to you. The last sentence is also non-toxic."
)
print(f"Validation response: {response}")
# assert response.validation_passed is False
error_message = response.validation_summaries[0].failure_reason
assert "The following text in your response was found to be toxic:" in error_message
assert "This message starts innocently enough." in error_message
assert "The last sentence is also non-toxic." in error_message
32 changes: 30 additions & 2 deletions validator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,10 @@ def validate(self, value: str, metadata: Dict[str, Any]) -> ValidationResult:
"""Validation method for the toxic language validator."""
if not value:
raise ValueError("Value cannot be empty.")

return self.validate_each_sentence(value, metadata)
if self._validation_method == "sentence":
return self.validate_each_sentence(value, metadata)
else:
return self.validate_full_text(value, metadata)

def _inference_local(self, model_input: str | list) -> Any:
"""Local inference method for the toxic language validator."""
Expand Down Expand Up @@ -269,3 +271,29 @@ def get_error_spans(self, original: str, fixed: str) -> List[ErrorSpan]:
)

return adjusted_spans

def validate_full_text(self, value: str, metadata: Dict[str, Any]) -> ValidationResult:
pred_labels = self._inference([value])[0]

if pred_labels:
error_spans = [
ErrorSpan(
start=0,
end=len(value),
reason=f"Toxic language detected: {', '.join(pred_labels)}",
)
]

print('failresult')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious print?

return FailResult(
metadata=metadata,
error_message=(
"The following text in your response "
"was found to be toxic:\n"
f"\n{value}"
),
fix_value="",
error_spans=error_spans,
)
print('passresult')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, too.

return PassResult(metadata=metadata)