|
| 1 | +## 🛡️ Overview |
| 2 | + |
| 3 | +This PR introduces a comprehensive guardrails system for Openlayer's tracing functionality, enabling automatic content filtering and protection for AI/LLM applications. |
| 4 | + |
| 5 | +## ✨ Key Features |
| 6 | + |
| 7 | +### 🏗️ **Flexible Architecture** |
| 8 | +- **Base guardrail system** with extensible `BaseGuardrail` abstract class |
| 9 | +- **Multiple action types**: `ALLOW`, `BLOCK`, `MODIFY` with configurable strategies |
| 10 | +- **Block strategies**: Graceful handling (return empty/error) vs exceptions |
| 11 | +- **Rich metadata** for monitoring, filtering, and analysis |
| 12 | + |
| 13 | +### 🔒 **PII Protection** |
| 14 | +- **PIIGuardrail** implementation using Microsoft Presidio |
| 15 | +- **Configurable entities**: Block SSNs, redact phone numbers, etc. |
| 16 | +- **Confidence thresholds** for fine-tuned detection |
| 17 | +- **Multiple handling strategies** for different use cases |
| 18 | + |
| 19 | +### 🎯 **Comprehensive Integration** |
| 20 | + |
| 21 | +#### **@trace() Decorator Support** |
| 22 | +```python |
| 23 | +@tracer.trace(guardrails=[pii_guardrail]) |
| 24 | +def process_user_input(user_data: str) -> str: |
| 25 | + return f"Processed: {user_data}" |
| 26 | +``` |
| 27 | + |
| 28 | +#### **Helper Functions Support** |
| 29 | +```python |
| 30 | +# Global configuration - applies to ALL helper functions |
| 31 | +tracer.configure(guardrails=[pii_guardrail]) |
| 32 | +traced_client = tracer.trace_openai(openai.OpenAI()) |
| 33 | +``` |
| 34 | + |
| 35 | +#### **Per-Call Overrides** |
| 36 | +```python |
| 37 | +tracer.add_chat_completion_step_to_trace( |
| 38 | + guardrails=[custom_guardrail], # Override global settings |
| 39 | + inputs={"prompt": "sensitive data"}, |
| 40 | + output="protected response" |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +## 📊 **Metadata & Analytics** |
| 45 | + |
| 46 | +Each trace step includes comprehensive guardrail metadata: |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "guardrails": { |
| 51 | + "input_pii_protection": { |
| 52 | + "action": "redacted", |
| 53 | + "reason": "Redacted PII entities: PHONE_NUMBER", |
| 54 | + "block_strategy": "return_error_message" |
| 55 | + } |
| 56 | + }, |
| 57 | + "has_guardrails": true, |
| 58 | + "guardrail_blocked": false, |
| 59 | + "guardrail_modified": true, |
| 60 | + "guardrail_allowed": false |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## 🔧 **Usage Examples** |
| 65 | + |
| 66 | +### **@trace() Decorator Example** |
| 67 | +See: [`examples/tracing/trace_decorator_with_guardrails.py`](examples/tracing/trace_decorator_with_guardrails.py) |
| 68 | +- Function-level PII protection |
| 69 | +- Multiple guardrails with different strategies |
| 70 | +- Custom guardrail implementations |
| 71 | +- Role-based conditional protection |
| 72 | + |
| 73 | +### **trace_openai() Helper Example** |
| 74 | +See: [`examples/tracing/trace_openai_with_guardrails.py`](examples/tracing/trace_openai_with_guardrails.py) |
| 75 | +- Global guardrails for all LLM calls |
| 76 | +- RAG pipeline protection |
| 77 | +- Application-specific configurations |
| 78 | +- Multi-model setups with different protection levels |
| 79 | + |
| 80 | +## ⚡ **Backward Compatibility** |
| 81 | + |
| 82 | +- **100% backward compatible** - existing code works unchanged |
| 83 | +- **Optional guardrails** - only applied when explicitly configured |
| 84 | +- **Graceful degradation** - missing dependencies don't break functionality |
| 85 | + |
| 86 | +## 🧪 **Testing** |
| 87 | + |
| 88 | +Comprehensive test suite included: |
| 89 | +- Unit tests for all guardrail components |
| 90 | +- Integration tests for decorator and helper functions |
| 91 | +- Block strategy validation |
| 92 | +- Metadata structure verification |
| 93 | + |
| 94 | +## 📦 **Dependencies** |
| 95 | + |
| 96 | +**Optional** (for PII guardrails): |
| 97 | +```bash |
| 98 | +pip install presidio-analyzer presidio-anonymizer |
| 99 | +``` |
| 100 | + |
| 101 | +**Future extensibility** ready for: |
| 102 | +- LLM-Guard integration |
| 103 | +- Custom content filters |
| 104 | +- Third-party security tools |
| 105 | + |
| 106 | +## 🎯 **Use Cases** |
| 107 | + |
| 108 | +- **PII Protection**: Automatic detection and redaction of sensitive data |
| 109 | +- **Content Filtering**: Block inappropriate or harmful content |
| 110 | +- **Compliance**: Meet regulatory requirements (GDPR, HIPAA, etc.) |
| 111 | +- **Security**: Prevent data leaks in AI applications |
| 112 | +- **Monitoring**: Track and analyze content filtering actions |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +**Impact**: This system provides enterprise-grade content protection for AI applications with minimal integration overhead and maximum flexibility. |
0 commit comments