AI Guardrails Framework - Comprehensive LLM safety toolkit (Alpha)
Project description
๐ Stinger - AI Guardrails Framework
A powerful, easy-to-use Python framework for safeguarding LLM applications with comprehensive content filtering and moderation capabilities.
Alpha Release Available! Install with
pip install stinger-guardrails-alpha
โจ Features
- ๐ก๏ธ Comprehensive Guardrails: Toxicity detection, PII protection, code generation prevention, and more
- ๐ Security Audit Trail: Complete logging of all security decisions for compliance and forensics
- ๐ฏ Simple API: Get started in 3 lines of code
- โก High Performance: Async-ready with synchronous convenience wrapper
- ๐ง Configurable: YAML-based configuration with runtime updates
- ๐งช Production Ready: Comprehensive testing and error handling
- ๐ Well Documented: Complete API reference and examples
๐ Quick Start
Installation
# Install the alpha release
pip install stinger-guardrails-alpha
# Or install from source for development
pip install .
Basic Usage
from stinger import GuardrailPipeline
from stinger.core import audit
# Enable security audit trail (zero-config)
audit.enable() # Tracks all security decisions
# Create a pipeline from preset
pipeline = GuardrailPipeline.from_preset("customer_service")
# Check input content
result = pipeline.check_input("My credit card is 4532-1234-5678-9012")
if result['blocked']:
print(f"Input blocked: {result['reasons']}")
# Audit trail automatically logs: user input, guardrail decision, reasons
# Check output content
result = pipeline.check_output("Here's the code: import os; os.system('rm -rf /')")
if result['blocked']:
print(f"Output blocked: {result['reasons']}")
๐ฅ๏ธ Command Line Interface (CLI)
After installing Stinger, you can use the CLI:
stinger demo
stinger check-prompt "My SSN is 123-45-6789."
stinger check-response "Here is your password: hunter2"
๐ Interactive Demos
Web Demo - Real-Time Guardrail Visualization
Experience Stinger's power through our interactive web interface that shows guardrails in action:
# Start the web demo (simplest method)
cd demos/web_demo
python start_demo.py
# Or run in background mode (useful for tools with timeouts)
python start_demo.py --detached
# Open http://127.0.0.1:8000 in your browser (use HTTP, not HTTPS)
Features:
- ๐ฌ Interactive chat interface with real-time guardrail feedback
- ๐ฆ Visual indicators showing which guardrails triggered
- ๐ Live audit trail visualization
- ๐ Switch between presets (customer service, medical, financial)
- โก See guardrails activate as you type
Management Console - System Monitoring Dashboard
Monitor your Stinger deployment with our real-time management console:
# Start the management console (simplest method)
cd management-console
./start_console.sh
# Or run in background mode (useful for tools with timeouts)
./start_console.sh --detached
# Open http://localhost:3001 in your browser
Features:
- ๐ Real-time metrics and performance monitoring
- ๐ Active conversation tracking
- ๐ Guardrail trigger statistics
- ๐ฅ System health monitoring
- ๐ Historical data visualization
๐ก๏ธ Available Guardrails
Stinger offers multiple levels of protection with both fast regex-based and sophisticated AI-powered guardrails:
๐ Simple/Fast Guardrails (No API Key Required)
- Simple PII Detection: Regex-based detection of SSNs, credit cards, emails, phone numbers
- Simple Toxicity Detection: Keyword-based profanity and hate speech filtering
- Simple Code Generation: Pattern-based code snippet detection
- Keyword Blocking: Block specific words or phrases
- URL Filtering: Block or allow specific domains
- Length Limiting: Control input/output length
- Regex Filtering: Custom pattern matching
๐ค AI-Powered Guardrails (Requires OpenAI API Key)
- AI PII Detection: Context-aware PII detection using language models
- AI Toxicity Detection: Nuanced understanding of harmful content
- AI Code Generation: Sophisticated code pattern recognition
- Content Moderation: General inappropriate content detection
- Topic Filtering: AI-based allowed/blocked topic enforcement
๐ Prompt Injection Protection (Three Levels)
- Quick/Local Detection: Fast pattern matching for common injection attempts
- AI-Powered Detection: Single-turn analysis using language models
- Conversation-Aware AI: Multi-turn context analysis for sophisticated attacks
- Configurable strategies: 'recent', 'suspicious', or 'mixed' context
- Risk levels: low, medium, high, critical
๐ฏ Usage Examples
# Use simple guardrails for speed (no API key needed)
pipeline = GuardrailPipeline.from_preset("customer_service") # Uses simple versions
# Enable AI guardrails for better accuracy (requires API key)
export OPENAI_API_KEY="sk-..."
pipeline = GuardrailPipeline.from_preset("medical") # Uses AI versions
# Mix and match as needed
config = {
"input": [
{"type": "simple_pii_detection"}, # Fast PII check
{"type": "ai_toxicity_detection"}, # AI toxicity check
{"type": "prompt_injection", # Multi-turn injection detection
"config": {"conversation_aware": True}}
]
}
๐ Security Audit Trail
Stinger provides comprehensive security audit logging for compliance and forensic analysis:
Zero-Config Audit Trail
from stinger.core import audit
# Enable with smart defaults (just works!)
audit.enable()
# Or specify destination
audit.enable("./logs/security.log")
# With PII redaction for compliance
audit.enable("./logs/audit.log", redact_pii=True)
Complete Security Tracking
- User Prompts: All user inputs logged with attribution
- LLM Responses: All AI responses tracked with context
- Guardrail Decisions: Every security decision with full reasoning
- Conversation Flow: Complete conversation reconstruction
- User Attribution: IP, session, user ID tracking for forensics
Compliance-Standard Ready
- PII Redaction Capabilities: Automatically redact sensitive data while preserving audit value (useful for GDPR, HIPAA, and other privacy regulations)
- Complete Audit Trail: Comprehensive logging suitable for enterprise security reviews
- Data Retention Controls: Configurable retention policies for different data types
- Forensic Analysis: Full incident reconstruction capabilities
- Export Formats: Generate compliance-ready reports in standard formats
Audit Trail Features
- Smart Environment Detection: Auto-configures for dev/prod/docker
- Async Buffering: Background processing with <10ms latency impact
- Query Tools: Easy audit trail searching and analysis
- Export Capabilities: CSV/JSON export for compliance reporting
- Cannot be disabled in production: Ensures audit trail integrity
๐ Configuration
Stinger uses YAML configuration files:
version: "1.0"
pipeline:
input:
- name: toxicity_check
type: simple_toxicity_detection
enabled: true
confidence_threshold: 0.7
categories: [hate_speech, harassment, threats]
- name: pii_check
type: simple_pii_detection
enabled: true
confidence_threshold: 0.8
categories: [credit_card, ssn, email]
output:
- name: code_generation_check
type: simple_code_generation
enabled: true
confidence_threshold: 0.6
categories: [programming_keywords, code_blocks]
๐ฏ API Reference
GuardrailPipeline
The main class for using Stinger guardrails.
# Initialize
pipeline = GuardrailPipeline("config.yaml")
# Check content
result = pipeline.check_input(content)
result = pipeline.check_output(content)
# Get status
status = pipeline.get_guardrail_status()
# Dynamic configuration
pipeline.enable_guardrail("toxicity_check")
pipeline.disable_guardrail("pii_check")
pipeline.update_guardrail_config("toxicity_check", {"confidence_threshold": 0.9})
Result Format
{
'blocked': bool, # Whether content was blocked
'warnings': List[str], # List of warning messages
'reasons': List[str], # List of blocking reasons
'details': Dict[str, Any], # Detailed results from each guardrail
'pipeline_type': str # Type of pipeline ("input" or "output")
}
Audit Trail API
from stinger.core import audit
# Enable audit trail
audit.enable("./logs/audit.log")
# Query audit trail
records = audit.query(
conversation_id="conv_123",
user_id="user_456",
last_hour=True
)
# Export for compliance
audit.export_csv("./logs/audit.log", "compliance_report.csv")
# Get performance stats
stats = audit.get_stats()
print(f"Queued: {stats['queued']}, Written: {stats['written']}")
๐ Examples
Basic Usage
from stinger import GuardrailPipeline
pipeline = GuardrailPipeline("config.yaml")
# Simple content checking
result = pipeline.check_input("User input here")
if result['blocked']:
print(f"Blocked: {result['reasons']}")
elif result['warnings']:
print(f"Warnings: {result['warnings']}")
else:
print("Content approved")
Advanced Usage
from stinger import GuardrailPipeline, audit
# Enable security audit trail
audit.enable("./logs/audit.log", redact_pii=True)
# Initialize with custom config
pipeline = GuardrailPipeline("my_config.yaml")
# Get pipeline status
status = pipeline.get_guardrail_status()
print(f"Pipeline has {status['total_enabled']} enabled guardrails")
# Dynamically configure guardrails
pipeline.disable_guardrail("pii_check")
pipeline.enable_guardrail("toxicity_check")
# Update configuration
pipeline.update_guardrail_config("toxicity_check", {
'confidence_threshold': 0.9
})
# Process content with detailed results
result = pipeline.check_input("Test content")
print(f"Blocked: {result['blocked']}")
print(f"Reasons: {result['reasons']}")
print(f"Warnings: {result['warnings']}")
print(f"Details: {result['details']}")
# All security decisions automatically logged to audit trail
๐งช Testing
Run the demo to see Stinger in action:
# Run the tech support demo
cd demos/tech_support
python3 demo.py
# Run the simple example
python3 examples/getting_started/01_basic_installation.py
๐ Learning Resources
Examples (/examples)
Start here - Minimal, focused code examples that mirror the Getting Started guide:
01_basic_installation.py- Installation and basic setup02_simple_guardrail.py- Simple guardrail usage03_global_rate_limiting.py- Rate limiting configuration04_conversation_api.py- Conversation-based filtering05_conversation_rate_limiting.py- Conversation rate limiting06_health_monitoring.py- Health monitoring and status07_cli_and_yaml_config.py- CLI and YAML configuration08_security_audit_trail.py- Security audit trail setup and usage09_troubleshooting_and_testing.py- Testing and debugging
Run examples:
cd examples/getting_started
python 01_basic_installation.py
Demos (/demos)
Advanced demonstrations showcasing specific features and scenarios:
conversation_aware_prompt_injection_demo.py- Advanced prompt injection detectionglobal_rate_limiting_demo.py- Rate limiting demonstrationstopic_filter_demo.py- Topic-based filteringtech_support/- Complete tech support scenario with audit trail
Run demos:
cd demos
python conversation_aware_prompt_injection_demo.py
Learning Path
- Start with examples - Run through the numbered examples in order
- Explore demos - Try the advanced demonstrations
- Check the tech support scenario - See a complete real-world implementation
- Review API docs - Deep dive into the complete API reference
๐ง Development
Note: Stinger uses a modern
src/layout. All package code is undersrc/stinger/.
Installation from Source
git clone https://github.com/virtualsteve-star/stinger.git
cd stinger
pip install -e .
Running Tests
pytest tests/
Project Structure
src/
โโโ stinger/
โโโ core/ # Core components and high-level API
โโโ guardrails/ # Guardrail implementations
โโโ data/ # Keyword lists and data files
โโโ scenarios/ # Pre-configured scenarios
โโโ utils/ # Utilities and exceptions
โโโ adapters/ # Model adapters
โโโ cli.py # CLI entry point
โโโ ...
๐ค Contributing
We welcome contributions! Please see our Contributing Guide for details.
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Support
- ๐ Documentation
- ๐ Issues
Made with โค๏ธ for safer AI applications
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stinger_guardrails_alpha-0.1.0a4.tar.gz.
File metadata
- Download URL: stinger_guardrails_alpha-0.1.0a4.tar.gz
- Upload date:
- Size: 96.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5c3d84271a5b5257bfda4b727ca192a22576e27f616d0f88a414a74713dc2ec3
|
|
| MD5 |
39635e0974fa074fede58e93d7973671
|
|
| BLAKE2b-256 |
394894f00a68eff91be78acd320a8f56bb02eb1e9be92efb93880087bfb0275c
|
File details
Details for the file stinger_guardrails_alpha-0.1.0a4-py3-none-any.whl.
File metadata
- Download URL: stinger_guardrails_alpha-0.1.0a4-py3-none-any.whl
- Upload date:
- Size: 119.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ddfd0d2480879e544f4155422ceb8d242cf46e29bf8ab5edd2a8159b7428d49
|
|
| MD5 |
9584568bd7fba4569f044feb0e0e596b
|
|
| BLAKE2b-256 |
c1b4777de82a3c5e18201d7a9d0ae9c9bfa3be005d1faae6f449c443e307f301
|