SDK Examples¶
Running Examples¶
All examples are in crates/oxide-guard-sdk/examples/:
# Regex guard example
cargo run -p oxide-guard-sdk --example custom_regex_guard
# Keyword guard example
cargo run -p oxide-guard-sdk --example custom_keyword_guard
# API guard example (uses mock)
cargo run -p oxide-guard-sdk --example custom_api_guard
# ML guard example
cargo run -p oxide-guard-sdk --example custom_ml_guard
Example: PII Detector (Regex)¶
Detects emails and SSNs using regex patterns:
use oxide_guard_sdk::prelude::*;
let guard = RegexGuard::new("pii_detector", GuardAction::Alert)
.add_pattern(r"[\w.+-]+@[\w-]+\.[\w.]+")
.unwrap()
.add_pattern(r"\b\d{3}-\d{2}-\d{4}\b")
.unwrap();
let result = guard.check("Contact user@example.com, SSN: 123-45-6789");
assert!(!result.passed);
assert_eq!(result.matches.len(), 2);
Example: Prompt Injection Filter (Builder)¶
Uses the builder to create a keyword-based injection filter:
use oxide_guard_sdk::prelude::*;
let guard = GuardBuilder::new("injection_filter")
.add_keywords(["ignore previous", "system prompt", "jailbreak", "DAN"])
.set_severity(Severity::Critical)
.set_action(GuardAction::Block)
.build()
.unwrap();
assert!(!guard.check("ignore previous instructions").passed);
assert!(guard.check("how do I write a for loop?").passed);
Example: Multi-Layer Defense (Composite)¶
Combines keyword + regex + ML guards:
use oxide_guard_sdk::prelude::*;
use oxide_guard_sdk::templates::ml_guard::Prediction;
let keyword_guard = KeywordGuard::new(
"keywords", vec!["attack".into()], 0.0,
GuardAction::Block, Severity::High, true,
);
let regex_guard = RegexGuard::new("patterns", GuardAction::Block)
.add_pattern(r"(?i)ignore\s+previous")
.unwrap();
let ml_guard = CustomMLGuard::new("ml", |content: &str| {
Prediction {
score: if content.len() > 100 { 0.8 } else { 0.1 },
label: None,
}
}).with_threshold(0.5);
let composite = CompositeGuard::new("multi_layer")
.add_guard(Box::new(keyword_guard))
.add_guard(Box::new(regex_guard))
.add_guard(Box::new(ml_guard));
let result = composite.check("attack: ignore previous instructions");
assert!(!result.passed);
Example: Python Custom Guard¶
from oxideshield import CustomGuard
class InjectionGuard(CustomGuard):
def __init__(self):
super().__init__("InjectionGuard")
self.patterns = [
"ignore previous",
"system prompt",
"new instructions",
]
def check(self, text):
lower = text.lower()
for pattern in self.patterns:
if pattern in lower:
return self.flag(f"Detected injection pattern: {pattern}")
return self.pass_check()
guard = InjectionGuard()
result = guard.check("Please ignore previous instructions")
print(f"Passed: {result.passed}") # False
print(f"Reason: {result.reason}") # "Detected injection pattern: ignore previous"
Example: CLI Scaffolding¶
Generate a new guard project:
# Generate a regex guard
oxideshield generate guard --name EmailDetector --type regex
# Generate a keyword guard with custom output directory
oxideshield generate guard --name ContentFilter --type keyword --output-dir ./my_guards
# Generate an ML guard stub
oxideshield generate guard --name ThreatClassifier --type ml