A Laravel package for Retrieval-Augmented Generation (RAG) systems with support for multiple drivers
mohaphez/laravel-ragkit is a Laravel package for a laravel package for retrieval-augmented generation (rag) systems with support for multiple drivers.
It currently has 12 GitHub stars and 5 downloads on Packagist (latest version v1.0.0).
Install it with composer require mohaphez/laravel-ragkit.
Discover more Laravel packages by mohaphez
or browse all Laravel packages to compare alternatives.
Last updated
RagKit is a Laravel package that provides a clean, reusable implementation of Retrieval-Augmented Generation (RAG) systems with support for multiple drivers.
You can install the package via composer:
composer require mohaphez/laravel-ragkit
After installing, publish the configuration and migrations:
php artisan vendor:publish --provider="RagKit\RagKitServiceProvider" --tag="ragkit-config"
php artisan vendor:publish --provider="RagKit\RagKitServiceProvider" --tag="ragkit-migrations"
Then run the migrations:
php artisan migrate
Configure your RAG service provider credentials in your .env file:
RAGKIT_DEFAULT_PROVIDER=chatbees
RAGKIT_CHATBEES_API_KEY=your-api-key
RAGKIT_CHATBEES_ACCOUNT_ID=your-account-id
'storage' => [
'disk' => env('RAGKIT_STORAGE_DISK', 'local'),
'path' => env('RAGKIT_STORAGE_PATH', 'rag'),
],
Configure upload processing in config/ragkit.php:
'upload' => [
'enable_upload_listener' => true,
'upload_handler_class' => \RagKit\Handlers\DefaultUploadHandler::class,
'queue' => 'default',
'max_retry_attempts' => 3,
'retry_backoff' => [10, 60, 180], // seconds between retries
],
'routes' => [
'prefix' => 'rag',
'middleware' => ['web', 'auth'],
],
RagKit uses a hierarchical data structure:
This structure allows for better organization and separation of concerns.
Add the HasRagAccounts trait to your User model:
use RagKit\Traits\HasRagAccounts;
class User extends Authenticatable
{
use HasRagAccounts;
// ...rest of your User model
}
This will add the following relationships to your User model:
ragAccounts() - A relationship to get all the user's RAG accountsragCollections() - A relationship to get all collections across accountsallRagDocuments() - A relationship to get all documents across collectionsuse RagKit\Facades\RagKit;
// Create a RAG account for a user
$account = RagKit::createUserAccount(
$user,
'My Research Account',
'chatbees',
[
'description' => 'Account for research papers',
]
);
// Create a collection in the account
$collection = RagKit::createCollection(
$account,
'Research Papers',
[
'namespace_name' => 'public',
'description' => 'Academic research papers on machine learning',
]
);
// Upload a document to a collection (triggers background processing)
$document = RagKit::uploadDocument(
$collection,
$filePath,
$fileName,
[
'source' => 'web_upload',
'category' => 'knowledge_base',
]
);
// Check document status
$status = $document->status; // created, queued, uploading, processing, completed, failed, retry
$message = $document->status_message;
DocumentUploaded event is firedHandleDocumentUpload listener processes the eventDefaultUploadHandler queues the document for processingProcessRagDocumentUpload job:
created: Initial state when document is stored locallyqueued: Document is queued for processinguploading: Document is being uploaded to providerprocessing: Document is being processed by providercompleted: Document processing is completefailed: Document processing failedretry: Document processing failed and will be retried// Ask a question using a specific collection
$result = RagKit::ask(
$collection,
'What is retrieval-augmented generation?',
null, // Optional document ID to filter by
[] // Chat history
);
// Access the answer and sources
$answer = $result['answer'];
$references = $result['references'];
// Start or continue a chat conversation within a collection
$history = [
['role' => 'user', 'content' => 'What is RAG?'],
['role' => 'assistant', 'content' => 'RAG stands for Retrieval-Augmented Generation...'],
];
$result = RagKit::ask(
$collection,
'Can you provide an example?',
null,
$history
);
You can import multiple documents at once using the ragkit:import command:
php artisan ragkit:import /path/to/documents 1 --recursive --extensions=pdf,docx
The command accepts the following arguments and options:
directory: Path to the directory containing files to importcollection_id: ID of the collection to import documents into--recursive: (Optional) Import files recursively from subdirectories--extensions: (Optional) Comma-separated list of file extensions to import (defaults to pdf,docx,doc,txt)List all RAG collections with their details:
php artisan ragkit:collections
The command accepts the following options:
--account_id: (Optional) Filter collections by account ID--provider: (Optional) Filter collections by provider (e.g., 'chatbees')--user_id: (Optional) Filter collections by user IDAll routes are prefixed with /rag and protected by web and auth middleware.
/rag/accounts
/rag/collections
account_id: RequiredGET /rag/documents
collection_id: RequiredPOST /rag/documents/upload
Content-Type: multipart/form-datafile: Document filecollection_id: Collection IDmetadata: Optional JSON metadataGET /rag/documents/{uuid}
DELETE /rag/documents/{uuid}
GET /rag/documents/{uuid}/outline-faq
/rag/chat
collection_id: Collection IDmessage: User messagehistory: Optional chat historyYou can create and register custom RAG service drivers by implementing the RagServiceAdapterInterface and registering it with the service:
use RagKit\Contracts\RagServiceAdapterInterface;
use RagKit\Facades\RagKit;
class CustomRagAdapter implements RagServiceAdapterInterface
{
// Implement interface methods
}
// Register adapter in AppServiceProvider
RagKit::registerAdapter('custom_provider', new CustomRagAdapter());
Run the package tests with PHPUnit:
composer test
Unit Tests:
Feature Tests:
feature/your-feature-namecomposer install
.env.example to .envphp artisan migrate
composer testmain: Production-ready codedevelop: Development branchfeature/*fix/*This package is open-sourced software licensed under the MIT license.