-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding tests for rulenodeprocessor. move fallback logic for rule repl…
…ace to initializaiotn
- Loading branch information
Showing
2 changed files
with
51 additions
and
10 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
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
from pydantic import BaseModel | ||
|
||
from langsmith import Client, traceable, tracing_context | ||
from langsmith.anonymizer import StringNodeRule, create_anonymizer | ||
from langsmith.anonymizer import StringNodeRule, create_anonymizer, RuleNodeProcessor | ||
|
||
EMAIL_REGEX = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}") | ||
UUID_REGEX = re.compile( | ||
|
@@ -139,3 +139,49 @@ def my_func(body: str, from_: MyInput) -> MyOutput: | |
if "inputs" in patched_data: | ||
assert patched_data["inputs"] == expected_inputs | ||
assert patched_data["outputs"] == expected_outputs | ||
|
||
def test_rule_node_processor_scrub_sensitive_info(): | ||
rules = [ | ||
StringNodeRule(pattern=re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), replace="[ssn]"), | ||
StringNodeRule( | ||
pattern=re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"), | ||
replace="[email]", | ||
), | ||
StringNodeRule( | ||
pattern=re.compile(r"\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b"), replace="[phone]" | ||
), | ||
] | ||
processor = RuleNodeProcessor(rules) | ||
|
||
nodes = [ | ||
{"value": "My SSN is 123-45-6789.", "path": ["field1"]}, | ||
{"value": "Contact me at [email protected].", "path": ["field2"]}, | ||
{"value": "Call me on 123-456-7890.", "path": ["field3"]}, | ||
] | ||
|
||
expected = [ | ||
{"value": "My SSN is [ssn].", "path": ["field1"]}, | ||
{"value": "Contact me at [email].", "path": ["field2"]}, | ||
{"value": "Call me on [phone].", "path": ["field3"]}, | ||
] | ||
|
||
result = processor.mask_nodes(nodes) | ||
|
||
assert result == expected | ||
|
||
def test_rule_node_processor_default_replace(): | ||
rules = [ | ||
StringNodeRule(pattern=re.compile(r"sensitive")), | ||
] | ||
processor = RuleNodeProcessor(rules) | ||
|
||
nodes = [ | ||
{"value": "This contains sensitive data", "path": ["field1"]}, | ||
] | ||
|
||
expected = [ | ||
{"value": "This contains [redacted] data", "path": ["field1"]}, | ||
] | ||
|
||
result = processor.mask_nodes(nodes) | ||
assert result == expected |