PHP code example of subhashladumor1 / laravel-ai-docs
1. Go to this page and download the library: Download subhashladumor1/laravel-ai-docs library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
subhashladumor1 / laravel-ai-docs example snippets
// Extract, summarize and convert an invoice to JSON in one chain
$data = AIDocs::pdf($invoice)->toJson();
// Ask a natural-language question about a contract
$answer = AIDocs::pdf($contract)->ask('What is the payment due date?');
// Transcribe a meeting audio and summarize it
$summary = AIDocs::audio($recording)->summarize();
// OCR a handwritten receipt into structured text
$text = AIDocs::image($receipt)->text();
use Subhashladumor1\LaravelAiDocs\Facades\AIDocs;
class InvoiceProcessor
{
public function process(string $pdfPath): array
{
// Extract ALL structured data from the invoice in one call
$data = AIDocs::model('gpt-5.2')->pdf($pdfPath)->toJson();
// $data contains:
// [
// 'title' => 'Invoice #INV-2024-00892',
// 'document_type' => 'invoice',
// 'date' => '2024-03-15',
// 'author' => 'ACME Supplies Ltd.',
// 'key_values' => [
// 'invoice_number' => 'INV-2024-00892',
// 'subtotal' => '$4,200.00',
// 'tax' => '$420.00',
// 'total' => '$4,620.00',
// 'due_date' => '2024-04-15',
// ],
// 'key_entities' => ['ACME Supplies Ltd.', 'John Doe', 'USD'],
// 'summary' => 'Invoice for 12 units of server hardware...',
// ]
Invoice::create([
'vendor' => $data['author'] ?? 'Unknown',
'total' => $data['key_values']['total'] ?? '0',
'due_date' => $data['key_values']['due_date'] ?? null,
'raw_data' => $data,
]);
return $data;
}
}
class ContractChatbot
{
public function chat(string $contractPath, string $question): string
{
// The package automatically chunks the contract into overlapping
// windows, scores each chunk for relevance, then sends only the
// relevant context to the AI (RAG pipeline).
return AIDocs::model('claude-sonnet-4-6')
->pdf($contractPath)
->ask($question);
}
}
// Usage in a controller:
$bot = new ContractChatbot();
$bot->chat($contract, 'What is the termination notice period?');
// → "Either party may terminate with 30 days written notice per Section 12.3."
$bot->chat($contract, 'What are the payment terms?');
// → "Payment is due net-30 from invoice date. Late payments accrue 1.5% monthly interest."
$bot->chat($contract, 'Is there an exclusivity clause?');
// → "Yes, Section 8 grants Client exclusivity in the APAC region for 24 months."
class MedicalFormDigitizer
{
public function digitize(string $imagePath): array
{
// Works with scanned photos, JPG, PNG — any image format
$text = AIDocs::image($imagePath)
->language('en')
->text('Extract all medical data: patient name, DOB, diagnosis codes, medications, allergies, and physician name.');
// Parse fields from the extracted text
return $this->parseFields($text);
}
public function digitizeArabicReport(string $imagePath): string
{
// Full Arabic OCR — auto-detected language
return AIDocs::image($imagePath)->text();
// Language detected automatically as 'ar'
}
}
class MeetingAutomator
{
public function process(string $audioPath): void
{
$builder = AIDocs::audio($audioPath)->language('en');
// Step 1: Get full transcription
$transcript = $builder->transcribe();
// Step 2: Summarize with action items
$summary = $builder->summarize(
'List all action items, decisions made, and owners. Use bullet points.'
);
// Step 3: Store and notify
Meeting::create([
'transcript' => $transcript,
'summary' => $summary,
'duration' => now(),
]);
Notification::send(
User::managers()->get(),
new MeetingSummaryNotification($summary)
);
}
}
class DocumentPortal
{
public function summarize(string $filePath): string
{
// Language is auto-detected from content (Unicode script analysis)
// Summary is returned in the detected language automatically
return AIDocs::pdf($filePath)->summarize()->text();
// Arabic PDF → Arabic summary
// Chinese PDF → Chinese summary
// French PDF → French summary
}
public function summarizeInEnglish(string $filePath): string
{
// Force English output regardless of source language
return AIDocs::pdf($filePath)
->language('en')
->summarize()
->text();
}
public function bulkProcess(array $files): array
{
return array_map(fn($file) => [
'file' => basename($file),
'summary' => AIDocs::pdf($file)->summarize()->text(),
'tables' => AIDocs::pdf($file)->tables()->result()->tables,
'json' => AIDocs::pdf($file)->toJson(),
], $files);
}
}
class FinancialReportParser
{
public function parse(string $reportPath): array
{
// Full pipeline: extract text → find tables → summarize → markdown
$result = AIDocs::model('gpt-5.2')
->pdf($reportPath)
->enhance() // pre-process PDF
->tables() // extract all data tables
->summarize() // executive summary
->result(); // get DocumentResultDTO
// Access structured data
foreach ($result->tables as $table) {
echo $table->title . "\n"; // "Revenue by Quarter"
echo $table->toMarkdown() . "\n"; // markdown table
// Headers + rows as arrays for charting
$chartData = [
'labels' => $table->headers,
'rows' => $table->rows,
];
}
return [
'summary' => $result->summary,
'tables' => count($result->tables),
'provider' => $result->provider, // 'openai'
'model' => $result->model, // 'gpt-5.2'
'time' => round($result->processingTimeSeconds, 2) . 's',
];
}
}
// Use the cheapest model for simple summaries
$quickSummary = AIDocs::model('gemini-3-flash-preview')->pdf($file)->summarize()->text();
// Use the most capable model for complex legal analysis
$legalAnalysis = AIDocs::model('claude-sonnet-4-6')->pdf($contract)->ask(
'Identify all clauses that could create liability for the vendor.'
);
// Use GPT-5-mini for vision-heavy scanned documents
$scannedText = AIDocs::model('gpt-5-mini')->image($scan)->text();
// Chain with explicit provider name
$result = AIDocs::provider('gemini')->pdf($file)->toJson();
$dto = AIDocs::model('gpt-5.2')
->pdf('/path/to/report.pdf')
->enhance()
->tables()
->summarize()
->result();
// Check what's available
if ($dto->hasText()) { /* raw text extracted */ }
if ($dto->hasSummary()) { /* AI summary generated */ }
if ($dto->hasTables()) { /* tables found */ }
if ($dto->hasJson()) { /* structured JSON extracted */ }
// Read properties
$dto->rawText; // Full extracted text
$dto->summary; // AI-generated summary
$dto->tables; // TableDTO[] — each with headers, rows, title
$dto->markdown; // Markdown-formatted document
$dto->json; // Structured JSON array
$dto->language; // Detected language: 'en', 'ar', etc.
$dto->provider; // 'openai' | 'claude' | 'gemini'
$dto->model; // 'gpt-5.2' | 'claude-sonnet-4-6' | etc.
$dto->processingTimeSeconds; // Wall-clock time used
// Convert for API responses
return response()->json($dto->toArray());
// Immutable wither — create a modified copy
$modified = $dto->with(['language' => 'fr']);
// Switch model (auto-detects provider from alias)
AIDocs::model('gpt-5-mini')->pdf($file)->text();
AIDocs::model('claude-sonnet-4-6')->pdf($file)->summarize()->text();
AIDocs::model('gemini-3-flash-preview')->pdf($file)->toJson();
// Switch provider explicitly (uses that provider's default model)
AIDocs::provider('claude')->pdf($file)->ask('Who signed this?');
AIDocs::provider('gemini')->image($scan)->text();
// Force language for all operations in the chain
AIDocs::language('ar')->pdf($file)->summarize()->text();
AIDocs::language('fr')->model('claude-sonnet-4-6')->document($docx)->toMarkdown();
// Multiple overrides — order doesn't matter
AIDocs::model('gpt-5-mini')->language('zh')->image($scan)->text();
AIDocs::language('de')->provider('gemini')->pdf($file)->toJson();
// 1. Just extract raw text
$text = AIDocs::pdf($file)->text();
// 2. Get page count
$pages = AIDocs::pdf($file)->pages();
// 3. Summarize
$summary = AIDocs::pdf($file)->summarize()->text();
// 4. Summarize with a custom instruction
$bullets = AIDocs::pdf($file)->summarize('Return 5 bullet points only.')->text();
// 5. Ask a question (RAG)
$answer = AIDocs::pdf($file)->ask('What is the contract value?');
// 6. Extract structured JSON
$data = AIDocs::pdf($file)->toJson();
// 7. JSON with custom schema instruction
$invoice = AIDocs::pdf($file)->toJson('Extract: vendor, total, due_date, line_items as JSON.');
// 8. Structured Extraction
$data = AIDocs::pdf($file)->structured();
// 9. Extract tables only
$result = AIDocs::pdf($file)->tables()->result();
$tables = $result->tables; // TableDTO[]
// 9. Convert to Markdown (text only)
$md = AIDocs::pdf($file)->toMarkdown();
// 10. Summarize then convert full result to Markdown
$md = AIDocs::pdf($file)->summarize()->toMarkdown();
// 11. Tables + summary → Markdown (richest document output)
$md = AIDocs::pdf($file)->enhance()->tables()->summarize()->toMarkdown();
// 12. Full pipeline → DocumentResultDTO
$result = AIDocs::pdf($file)->enhance()->tables()->summarize()->result();
// 13. Override language for Arabic PDFs
$summary = AIDocs::language('ar')->pdf($file)->summarize()->text();
// 14. Multi-provider same file
$fast = AIDocs::model('gemini-3-flash-preview')->pdf($file)->summarize()->text();
$precise = AIDocs::model('claude-sonnet-4-6')->pdf($file)->ask('What are the risks?');
// 15. Page count before processing
if (AIDocs::pdf($file)->pages() > 100) {
$summary = AIDocs::model('claude-sonnet-4-6')->pdf($file)->summarize()->text();
} else {
$summary = AIDocs::pdf($file)->toJson();
}
// 1. Simple OCR - extract all text
$text = AIDocs::image($scan)->text();
// 2. OCR with a focused extraction prompt
$numbers = AIDocs::image($receipt)->text('Extract only monetary amounts and totals.');
// 3. OCR with language hint
$arabic = AIDocs::language('ar')->image($scan)->text();
$french = AIDocs::language('fr')->image($scan)->text();
// 4. Summarize image content
$summary = AIDocs::image($scan)->summarize();
// 5. Summarize with custom style
$summary = AIDocs::image($scan)->summarize('One sentence summary only.');
// 6. Extract tables from a screenshot of a spreadsheet
$tables = AIDocs::image($spreadsheetPhoto)->tables();
foreach ($tables as $table) {
echo $table->toMarkdown();
}
// 7. Ask a question about an image
$answer = AIDocs::image($photo)->ask('What is the name on this ID card?');
$answer = AIDocs::image($menu)->ask('Does this menu have any vegetarian options?');
// 8. Convert image content to structured JSON
$data = AIDocs::image($businessCard)->toJson();
// Returns: ['title' => 'John Doe', 'key_entities' => ['Acme Corp'], ...]
// 9. Get full DocumentResultDTO
$result = AIDocs::image($scan)->result();
echo $result->rawText;
echo $result->language; // auto-detected
// 10. Use GPT-5-mini for best OCR accuracy
$text = AIDocs::model('gpt-5-mini')->image($scan)->text();
// 11. Medical form: focused extraction prompt + JSON
$fields = AIDocs::model('gpt-5-mini')
->language('en')
->image($medicalForm)
->toJson();
// 1. Simple transcription
$text = AIDocs::audio($mp3)->transcribe();
// 2. Transcription with language hint (improves accuracy)
$text = AIDocs::language('es')->audio($file)->transcribe();
$text = AIDocs::language('ar')->audio($file)->transcribe();
// 3. Transcribe then summarize
$summary = AIDocs::audio($meeting)->summarize();
// 4. Summarize with custom instructions
$actions = AIDocs::audio($meeting)->summarize(
'List action items, owners, and deadlines. Format as numbered list.'
);
// 5. Get both transcript + summary via result()
$result = AIDocs::audio($meeting)->result();
$transcript = $result->transcript; // or $result->rawText — same value
$provider = $result->provider; // 'openai'
// 6. Step-by-step: transcribe first, then summarize separately
$builder = AIDocs::audio($recording)->language('en');
$transcript = $builder->transcribe();
$summary = $builder->summarize('Bullet points only.');
// 7. Store both in DB
Recording::create([
'transcript' => AIDocs::audio($file)->transcribe(),
'summary' => AIDocs::audio($file)->summarize(),
'duration' => $audioDurationSeconds,
]);
// 1. Extract text from DOCX
$text = AIDocs::document($docx)->text();
// 2. Extract text from plain text file
$text = AIDocs::document('/notes/meeting.txt')->text();
// 3. Summarize a Word document
$summary = AIDocs::document($docx)->summarize()->text();
// 4. Ask a question about a DOCX contract
$answer = AIDocs::document($docx)->ask('What is the governing law?');
// 5. Extract structured JSON from a DOCX report
$data = AIDocs::document($docx)->toJson();
// 6. Extract tables from a DOCX with data tables
$result = AIDocs::document($docx)->tables()->result();
// 7. Full pipeline on a Word document
$result = AIDocs::document($docx)
->enhance()
->tables()
->summarize()
->result();
// 8. Convert any document to Markdown
$md = AIDocs::document($docx)->summarize()->toMarkdown();
$md = AIDocs::document($txtFile)->toMarkdown();
// 9. Multi-language DOCX
$summary = AIDocs::language('de')->document($germanDocx)->summarize()->text();
// 10. Ask a question using Claude (long context = better for big docs)
$answer = AIDocs::model('claude-sonnet-4-6')->document($docx)->ask(
'Summarize all obligations of Party B.'
);
$dto->hasText(); // true if rawText is not empty
$dto->hasSummary(); // true if summary was generated
$dto->hasTables(); // true if at least one table was found
$dto->hasJson(); // true if structured JSON was extracted
$dto->toArray(); // Convert everything to a plain array (great for API responses)
$dto->toJson(); // Return only the json property as array (or [] if null)
$dto->with([...]); // Return a modified copy (immutable wither)
$result = AIDocs::pdf($file)->enhance()->tables()->summarize()->result();
// Conditional logic based on what was found
if ($result->hasTables()) {
foreach ($result->tables as $table) {
echo $table->title . "\n";
echo $table->toMarkdown() . "\n";
}
}
if ($result->hasSummary()) {
Slack::send('#docs', $result->summary);
}
// API response
return response()->json($result->toArray());
// Immutable wither — create a modified copy without changing original
$translated = $result->with(['summary' => translateToFrench($result->summary)]);
// Metadata for logging
Log::info('AI processed document', [
'provider' => $result->provider,
'model' => $result->model,
'language' => $result->language,
'time' => $result->processingTimeSeconds,
'tables' => count($result->tables),
]);
$table->title; // string|null — e.g. "Revenue by Region"
$table->headers; // string[] — e.g. ['Region', 'Q1', 'Q2']
$table->rows; // string[][] — e.g. [['APAC', '$2M', '$3M'], ...]
$table->pageNumber; // int — estimated page number (1-based)
$table->toMarkdown(); // Renders as a GitHub-flavoured markdown table
$table->toArray(); // Converts to plain array for JSON serialization
use Subhashladumor1\LaravelAiDocs\Tests\Fakes\FakeAIProvider;
use Subhashladumor1\LaravelAiDocs\Services\SummarizerService;
use Subhashladumor1\LaravelAiDocs\Services\AskPDFService;
use Subhashladumor1\LaravelAiDocs\Processors\TextChunker;
// Test summarization — no OpenAI call made
it('summarizes a PDF', function () {
$provider = (new FakeAIProvider())
->withTextResponse('This report covers Q4 earnings growth of 23%.');
$service = new SummarizerService();
$result = $service->summarize($provider, 'Long earnings report text...');
expect($result)->toBe('This report covers Q4 earnings growth of 23%.');
});
// Test Ask PDF / RAG — no API call made
it('answers questions about a document', function () {
$provider = (new FakeAIProvider())
->withTextResponse('The payment is due on April 15, 2024.');
$service = new AskPDFService(new TextChunker(500, 50));
$answer = $service->ask($provider, 'Invoice text...', 'When is payment due?');
expect($answer)->toContain('April 15');
});
// Test error simulation
it('handles provider failure gracefully', function () {
$provider = (new FakeAIProvider())->shouldThrow('Rate limit exceeded');
expect(fn () => (new SummarizerService())->summarize($provider, 'text'))
->toThrow(RuntimeException::class, 'Rate limit exceeded');
});