generated from guardrails-ai/validator-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
538343b
commit 3548c1d
Showing
2 changed files
with
19 additions
and
43 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -14,4 +14,4 @@ type: | |
qa: | ||
make lint | ||
make type | ||
make tests | ||
make test |
This file contains 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,43 +1,19 @@ | ||
# to run these, run | ||
# pytest test/test-validator.py | ||
|
||
import pytest | ||
from guardrails import Guard | ||
from validator import CompetitorCheck | ||
|
||
# We use 'refrain' as the validator's fail action, | ||
# so we expect failures to always result in a guarded output of None | ||
# Learn more about corrective actions here: | ||
# https://www.guardrailsai.com/docs/concepts/output/#%EF%B8%8F-specifying-corrective-actions | ||
competitors_list = [ | ||
"Acorns", | ||
"Citigroup", | ||
"Citi", | ||
"Fidelity Investments", | ||
"Fidelity", | ||
"JP Morgan Chase and company", | ||
"JP Morgan", | ||
"JP Morgan Chase", | ||
"JPMorgan Chase", | ||
"Chase" "M1 Finance", | ||
"Stash Financial Incorporated", | ||
"Stash", | ||
"Tastytrade Incorporated", | ||
"Tastytrade", | ||
"ZacksTrade", | ||
"Zacks Trade", | ||
] | ||
|
||
guard = Guard().use(CompetitorCheck(competitors=competitors_list, use_local=True, on_fail="refrain")) | ||
|
||
|
||
def test_pass(): | ||
test_output = "HomeDepot is not a competitor" | ||
res = guard.parse(test_output) | ||
|
||
assert(res.validated_output == test_output) | ||
|
||
def test_fail(): | ||
test_output = "Acorns, with its extensive global network, has become a powerhouse in the banking sector, catering to the needs of millions across different countries, like Citi. I also like my RothIRA account with Fidelity Investments." | ||
res = guard.parse(test_output) | ||
|
||
assert(not res.validation_passed) | ||
from guardrails.hub import CompetitorCheck | ||
|
||
# Setup Guard with CompetitorCheck validator | ||
guard = Guard().use( | ||
CompetitorCheck, ["Apple", "Samsung"], "exception" | ||
) | ||
|
||
# Test passing response (no competitor mentioned) | ||
def test_competitor_check_pass(): | ||
response = guard.validate("The apple doesn't fall far from the tree.") | ||
assert response.validation_passed is True | ||
|
||
# Test failing response (competitor mentioned) | ||
def test_competitor_check_fail(): | ||
with pytest.raises(Exception) as e: | ||
guard.validate("Apple just released a new iPhone.") | ||
assert "Validation failed for field with errors:" in str(e.value) |