PHP code example of omniglies / laravel-rag

1. Go to this page and download the library: Download omniglies/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/ */

    

omniglies / laravel-rag example snippets


'ai_provider' => env('RAG_AI_PROVIDER', 'openai'),

'providers' => [
    'openai' => [
        'api_key' => env('OPENAI_API_KEY'),
        'model' => env('RAG_OPENAI_MODEL', 'gpt-3.5-turbo'),
        'max_tokens' => env('RAG_OPENAI_MAX_TOKENS', 1000),
    ],
    'anthropic' => [
        'api_key' => env('ANTHROPIC_API_KEY'),
        'model' => env('RAG_ANTHROPIC_MODEL', 'claude-3-sonnet-20240229'),
    ],
],

'vector_database' => [
    'provider' => env('RAG_VECTOR_PROVIDER', 'pinecone'),
    'providers' => [
        'pinecone' => [
            'api_key' => env('PINECONE_API_KEY'),
            'environment' => env('PINECONE_ENVIRONMENT'),
            'index_name' => env('PINECONE_INDEX_NAME', 'rag-knowledge'),
        ],
        'weaviate' => [
            'url' => env('WEAVIATE_URL'),
            'api_key' => env('WEAVIATE_API_KEY'),
        ],
        'qdrant' => [
            'url' => env('QDRANT_URL'),
            'api_key' => env('QDRANT_API_KEY'),
        ],
    ],
],

'search' => [
    'default_limit' => env('RAG_SEARCH_LIMIT', 3),
    'similarity_threshold' => env('RAG_SIMILARITY_THRESHOLD', 0.7),
    'hybrid_search' => [
        'enabled' => env('RAG_HYBRID_SEARCH_ENABLED', true),
        'vector_weight' => env('RAG_VECTOR_WEIGHT', 0.8),
        'keyword_weight' => env('RAG_KEYWORD_WEIGHT', 0.2),
    ],
],

use Omniglies\LaravelRag\Facades\Rag;

// Ingest a document
$document = Rag::ingestDocument('Document Title', $uploadedFile, [
    'author' => 'John Doe',
    'category' => 'Research'
]);

// Ask a question
$response = Rag::askWithContext('What is machine learning?');
echo $response['answer'];

// Search for similar content
$chunks = Rag::searchRelevantChunks('artificial intelligence', 5);

use Omniglies\LaravelRag\Services\RagService;

class MyController extends Controller
{
    public function __construct(private RagService $ragService) {}

    public function search(Request $request)
    {
        $results = $this->ragService->searchRelevantChunks(
            $request->query,
            $request->limit ?? 10
        );
        
        return response()->json($results);
    }
}

use Omniglies\LaravelRag\Services\VectorSearchService;
use Omniglies\LaravelRag\Services\EmbeddingService;

// Direct vector operations
$vectorService = app(VectorSearchService::class);
$embeddingService = app(EmbeddingService::class);

// Generate embeddings
$embedding = $embeddingService->generateEmbedding('sample text');

// Perform vector search
$results = $vectorService->searchSimilar('query text', [
    'limit' => 10,
    'threshold' => 0.8,
    'namespace' => 'documents'
]);

// Configure external processing
'external_processing' => [
    'enabled' => env('RAG_EXTERNAL_PROCESSING_ENABLED', true),
    'api_url' => env('RAG_PROCESSING_API_URL'),
    'api_key' => env('RAG_PROCESSING_API_KEY'),
    'webhook_secret' => env('RAG_PROCESSING_WEBHOOK_SECRET'),
],

'channels' => [
    'rag' => [
        'driver' => 'daily',
        'path' => storage_path('logs/rag.log'),
        'level' => 'debug',
    ],
],
bash
php artisan rag:install
bash
# Start queue workers for document processing
php artisan queue:work --queue=rag
bash
# Check usage statistics
php artisan rag:status --detailed

# Optimize chunk sizes
# Edit config/rag.php chunking settings