Skip to content

rcerljenko/laravel-openai-moderation

Repository files navigation

Laravel OpenAI Moderation Rule

Laravel validation Rule using OpenAI Moderation API. Gives you a way to validate your form requests for inappropriate content.

Installation

Standard Composer package installation:

composer require rcerljenko/laravel-openai-moderation

Usage

  1. Publish config and translation files.
php artisan vendor:publish --provider="RCerljenko\LaravelOpenAIModeration\ServiceProvider"
  1. Set your OpenAI API key and enable package via newly created config file => config/openai.php

  2. Use provided rule with your validation rules.

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use RCerljenko\LaravelOpenAIModeration\Rules\OpenAIModeration;

class StoreText extends FormRequest
{
 /**
  * Determine if the user is authorized to make this request.
  */
 public function authorize(): bool
 {
  return true;
 }

 /**
  * Get the validation rules that apply to the request.
  */
 public function rules(): array
 {
  return [
   'text' => ['required', 'string', new OpenAIModeration],
  ];
 }
}