PHP code example of blue-hex / laravel-docling-rag
1. Go to this page and download the library: Download blue-hex/laravel-docling-rag 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/ */
blue-hex / laravel-docling-rag example snippets
use BlueHex\DoclingRag\Support\HasRagDocuments;
use Illuminate\Database\Eloquent\Model;
class DataSource extends Model
{
use HasRagDocuments;
}
use BlueHex\DoclingRag\Facades\Rag;
$document = Rag::ingest($request->file('document'), owner: $dataSource);
// or
$document = $dataSource->ingestDocument($request->file('document'));
// e.g. skip OCR for a document you know is already text, or force it for a scan
$dataSource->ingestDocument($file, options: ['convert_do_ocr' => false]);
Rag::ingest($file, owner: $dataSource, options: [
'convert_table_mode' => 'fast',
'convert_page_range' => [1, 10],
]);
public function search(Request $request, Project $project)
{
abort_if($project->user_id !== $request->user()->id, 404);
return Rag::search($request->string('q'), owner: $project);
}
use BlueHex\DoclingRag\Events\DocumentIngested;
use BlueHex\DoclingRag\Events\IngestionFailed;
use Illuminate\Support\Facades\Event;
Event::listen(function (DocumentIngested $event): void {
$document = $event->document; // status === DocumentStatus::Ready
$document->owner->notify(new DocumentReady($document));
});
Event::listen(function (IngestionFailed $event): void {
report(new RuntimeException($event->document->failure_reason));
});
// app/Listeners/BroadcastIngestionStatus.php
public function handle(DocumentIngested $event): void
{
IngestionStatusChanged::dispatch($event->document->id, 'ready');
}
use BlueHex\DoclingRag\Facades\Rag;
$results = Rag::search('What is the refund window?', owner: $dataSource);
foreach ($results as $chunk) {
$chunk->text; // the passage
$chunk->pageNo; // cite this
$chunk->headingPath; // e.g. ['Policies', 'Refunds']
$chunk->score;
}
use BlueHex\DoclingRag\Tools\SearchDocuments;
$agent->tools([
SearchDocuments::for($dataSource),
// optional: scope and cap it
// SearchDocuments::for($dataSource, filters: ['content_type' => ContentType::Table], k: 5),
]);
use BlueHex\DoclingRag\Retrieval\ChunkResult;
$fake = Rag::fake([
new ChunkResult(1, $doc->id, 'Refunds within 30 days.', pageNo: 4, headingPath: ['Refunds'], contentType: null, score: 0.9),
]);
// ... exercise your agent ...
expect($fake->searches)->toHaveCount(1);