Day 5 – AI Validation Rules: Flagging Missing or Risky Fields #LaravelGPT #SmartContracts #AIValidation #LegalAI #OpenAI #LaravelAI #DocumentAudit


Today we’ll create a GPT-driven validation system that flags documents with missing fields, invalid data (e.g. date mismatch), or risky clauses (e.g. penalty, termination terms) by asking GPT to “think like an auditor.”


🎯 Goal

Use GPT to analyze the full document (text + extracted fields) and return validation warnings or suggestions.


🧠 Step 1: Build the Validation Prompt

Create a reusable validation method in DocumentAnalysisService:

public function validateDocument(string $text, array $fields): string
{
    $combined = "Document content:\n{$text}\n\nExtracted fields:\n" . json_encode($fields);

    $response = OpenAI::chat()->create([
        'model' => 'gpt-4',
        'messages' => [
            ['role' => 'system', 'content' => 'You are a legal compliance assistant.'],
            ['role' => 'user', 'content' => "Review the following contract. Identify any missing fields, inconsistencies, or risky clauses. Be concise:\n\n{$combined}"]
        ],
    ]);

    return $response['choices'][0]['message']['content'];
}

🧩 Step 2: Add a validation_notes Column

php artisan make:migration add_validation_notes_to_documents_table

In the migration:

public function up()
{
    Schema::table('documents', function (Blueprint $table) {
        $table->text('validation_notes')->nullable();
    });
}

Then:

php artisan migrate

⚙️ Step 3: Call the Validation During Upload or Button Click

Update your DocumentController:

$notes = $ai->validateDocument($text, $fields);

Document::create([
    ...
    'validation_notes' => $notes,
]);

OR add a “Validate Now” button:

<form method="POST" action="{{ route('documents.validate', $document->id) }}">
    @csrf
    <button type="submit">Validate Now with AI</button>
</form>

Route and method:

Route::post('/documents/{document}/validate', [DocumentController::class, 'validate'])->name('documents.validate');

public function validate(Document $document, DocumentAnalysisService $ai)
{
    $notes = $ai->validateDocument($document->extracted_text, json_decode($document->extracted_fields, true));
    $document->update(['validation_notes' => $notes]);

    return back()->with('success', 'Validation completed.');
}

📄 Step 4: Display Validation Notes

<h2>Validation Notes</h2>
@if($document->validation_notes)
    <pre>{{ $document->validation_notes }}</pre>
@else
    <p>No validation notes yet.</p>
@endif

✅ Summary

✅ Today you:

  • Created GPT-powered validation logic
  • Flagged missing, inconsistent, or risky info
  • Displayed AI audit notes for review
See also  Day 7 – Improving GPT Recommendations Using Click Feedback #LaravelGPT #AIRecommendations #FeedbackLoop #Personalization #OpenAI #LaravelAI

✅ Up next (Day 6): We’ll highlight flagged fields visually in the interface and start building trust indicators like “Validated ✅”, “Action Needed ⚠️”, or “Review Required 🚨”.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.