Download the PHP package mohammed-abd-razaq/laravel-toxicity-filter without Composer
On this page you can find all versions of the php package mohammed-abd-razaq/laravel-toxicity-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mohammed-abd-razaq/laravel-toxicity-filter
More information about mohammed-abd-razaq/laravel-toxicity-filter
Files in mohammed-abd-razaq/laravel-toxicity-filter
Package laravel-toxicity-filter
Short Description A lightweight Laravel library that wraps AI-based toxicity detection engines to evaluate and moderate user-generated content within your application.
License MIT
Informations about the package laravel-toxicity-filter
Laravel AI Toxicity Filter Package
A professional Laravel library that integrates AI-based toxicity detection engines to automatically evaluate, moderate, and filter user-generated content such as comments, posts, messages, and reviews within your application.
Features
- 🤖 Multiple AI Providers: Support for OpenAI Moderation API, Google Perspective API, and extensible for more
- ⚡ Laravel Integration: Seamless integration with Laravel facades, service providers, and middleware
- 🛡️ Automatic Filtering: Middleware for automatic content moderation on routes
- 🎛️ Configurable Thresholds: Customizable toxicity thresholds for blocking, flagging, and warning
- 📊 Detailed Analytics: Comprehensive logging and database storage of toxicity detection results
- 🚀 Queue Support: Async processing for bulk or large content moderation
- 💾 Caching: Redis/database caching to reduce API calls and improve performance
- 🔧 Extensible: Easy to add new AI providers through clean interfaces
- 🔒 Privacy First: Content hashing for privacy protection
- 📈 Performance Optimized: Built-in rate limiting and content optimization
- 🛠️ Developer Friendly: Rich testing utilities and comprehensive error handling
- 🌐 Multi-language Support: Native support for Arabic and English content with automatic language detection
Requirements
- PHP 8.0 or higher
- Laravel 9.0, 10.0, 11.0, or 12.0
- OpenAI API key (for OpenAI provider)
- Google Perspective API key (for Perspective provider)
Installation
- Install via Composer:
Or if using the local package, update your root composer.json:
-
Run composer update:
-
Publish the configuration file:
-
Publish and run migrations:
- Clear configuration cache:
Configuration
Set up your AI provider API keys in .env:
Usage
Basic Usage with Facade
Using Specific Providers
Optional Middleware Usage
The package includes optional middleware for automatic content filtering. To use it, you need to manually register it first.
Register the Middleware
Add to your app/Http/Kernel.php:
Apply to Routes
The middleware will:
- Automatically block toxic content (returns 422 error)
- Flag moderately toxic content for review
- Add warnings to the request for mildly toxic content
- Log all detections to the database
Advanced Usage
Arabic Language Support
The package includes native support for Arabic content with automatic language detection and text normalization:
Automatic Language Detection
Arabic Text Normalization
The package automatically normalizes Arabic text for better analysis:
- Character Normalization: Converts different forms of Arabic characters (أ, إ, آ → ا)
- Diacritics Removal: Removes tashkeel (diacritics) for better matching
- Hamza Handling: Normalizes hamza variations
Language-Specific Thresholds
Configure different toxicity thresholds for Arabic and English content:
Manual Language Detection
You can also use the language detection service directly:
Queue Processing
For async processing, you can dispatch jobs:
Supported AI Providers
OpenAI Moderation API
- Pros: High accuracy, fast response, multiple toxicity categories
- Cons: Requires API key, has usage costs
- Content Limit: ~32,000 characters
Google Perspective API
- Pros: Free tier available, detailed attribute scoring
- Cons: Limited free quota, requires Google Cloud setup
- Content Limit: 3,000 characters
Configuration Options
The package offers extensive configuration options:
- Providers: Configure multiple AI providers with failover
- Thresholds: Set different toxicity thresholds for various actions
- Caching: Cache results to reduce API calls and costs
- Logging: Comprehensive logging with configurable storage
- Queue: Async processing for better performance
- Bypass Rules: Skip filtering for trusted users or content
Database Schema
The package creates a toxicity_detections table to log all analysis results:
Extending the Package
Adding New AI Providers
Implement the ToxicityProviderInterface:
Testing
Test Configuration
Create a .env.testing file for test environment:
Troubleshooting
Common Issues
1. Configuration not loaded
2. Provider API errors
- Verify API keys are correctly set in
.env - Check API rate limits and quotas
- Ensure network connectivity to provider endpoints
3. Migration issues
4. Cache issues
Debug Mode
Enable debug logging in your configuration:
Performance Considerations
- Caching: Enable caching to reduce API calls for duplicate content
- Queue: Use async processing for bulk content or non-blocking operations
- Rate Limiting: Configure rate limits to stay within API quotas
- Content Optimization: Pre-filter very short content or obvious spam
Security & Privacy
- Content Hashing: Store MD5 hashes instead of actual content for privacy
- API Key Management: Store API keys securely in environment variables
- User Bypass: Allow trusted users to bypass filtering when appropriate
- Audit Trail: Comprehensive logging for compliance and debugging
Changelog
Version 1.1.0
Arabic Language Support
- ✅ Native Arabic language detection and support
- ✅ Automatic Arabic text normalization (character and diacritics)
- ✅ Language-specific toxicity thresholds
- ✅ Multilingual content support
- ✅ Arabic language detection service
- ✅ Enhanced provider support for Arabic content
Version 1.0.0
Initial Release
- ✅ OpenAI Moderation API integration
- ✅ Google Perspective API integration
- ✅ Laravel facade and service provider
- ✅ Configurable toxicity thresholds
- ✅ Middleware for automatic filtering
- ✅ Database logging and analytics
- ✅ Caching support
- ✅ Queue processing
- ✅ Extensible provider system
- ✅ Comprehensive test suite
Contributing
We welcome contributions! Please see our Contributing Guide for details.
Development Setup
- Clone the repository
- Install dependencies:
composer install - Copy
.env.exampleto.envand configure - Run tests:
composer test
Support
- 📧 Email: [email protected]
- 🐛 Issues: GitHub Issues
- 📖 Documentation: GitHub Repository
License
This package is open-sourced software licensed under the MIT License.
Author
Mohammed Abd Razaq
- GitHub: @mohammed-abd-razaq
- Email: [email protected]
⭐ If you find this package helpful, please consider giving it a star on GitHub!
All versions of laravel-toxicity-filter with dependencies
laravel/framework Version ^9.0|^10.0|^11.0|^12.0
guzzlehttp/guzzle Version ^7.0
illuminate/support Version ^9.0|^10.0|^11.0|^12.0
illuminate/http Version ^9.0|^10.0|^11.0|^12.0