1. Go to this page and download the library: Download akira/laravel-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/ */
akira / laravel-rag example snippets
use Akira\Rag\Facades\Rag;
// Ingest a document
$result = Rag::ingest([
'title' => 'Labor Law 2024',
'source_type' => 'law',
'source_ref' => 'law:2024:001',
'content' => 'Full text of the labor law...',
'meta' => ['lang' => 'en', 'year' => 2024],
]);
// Returns: ['document_id' => '...', 'chunks' => 5]
// Ask a question
$answer = Rag::ask('What are the notice periods for termination?');
// Returns: [
// 'answer' => 'According to the law...',
// 'chunks' => [['id' => '...', 'score' => 0.95], ...],
// 'query_id' => '...'
// ]
namespace App\Rag;
use Akira\Rag\Tenant\TenantResolver;
class MyTenantResolver implements TenantResolver
{
public function resolve(): ?string
{
// Return current tenant ID
return auth()->user()?->tenant_id;
}
}
// Ingestion is automatically scoped
Rag::ingest([...]);
// Retrieval is automatically scoped
$answer = Rag::ask('...');
// Stats are tenant-specific
$stats = Rag::stats();
use Akira\Rag\Exceptions\InvalidPayload;
use Akira\Rag\Exceptions\InvalidQuestionException;
use Akira\Rag\Exceptions\DocumentNotFoundException;
try {
Rag::ingest([
'title' => 'Test',
// Missing n
}
use Akira\Rag\Facades\Rag;
it('can ingest and retrieve documents', function () {
$result = Rag::ingest([
'title' => 'Test Document',
'source_type' => 'test',
'source_ref' => 'test:001',
'content' => 'This is a test document about Laravel RAG.',
'meta' => ['category' => 'test'],
]);
expect($result)
->toHaveKeys(['document_id', 'chunks'])
->and($result['chunks'])->toBeGreaterThan(0);
$answer = Rag::ask('What is this test about?');
expect($answer)
->toHaveKeys(['answer', 'chunks', 'query_id'])
->and($answer['chunks'])->not->toBeEmpty();
});