承接 thaolaptrinh/laravel-rag 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thaolaptrinh/laravel-rag

Composer 安装命令:

composer require thaolaptrinh/laravel-rag

包简介

Production-grade RAG engine for Laravel — provider-agnostic, HTTP-based, extensible

README 文档

README

Tests PHPStan License

Production-grade RAG (Retrieval-Augmented Generation) engine for Laravel — provider-agnostic, HTTP-based, extensible.

Features

  • Document Ingestion: Split documents into chunks, generate embeddings, store in pgvector
  • Semantic Search: Cosine similarity search with metadata filtering
  • LLM Integration: OpenAI-compatible API support (OpenAI, FPT Cloud, Ollama, vLLM, Supabase)
  • Idempotent Re-ingestion: Skip unchanged documents using content hash
  • Queue Support: Process large document sets asynchronously
  • Testing Helpers: Fake mode for unit testing

Requirements

  • PHP 8.4+
  • Laravel 11.0, 12.0, 13.0
  • PostgreSQL 13+ with pgvector extension

Installation

composer require thaolaptrinh/laravel-rag

Configuration

  1. Add the RAG database connection to config/database.php:
'rag' => [
    'driver' => 'pgsql',
    'host' => env('RAG_DB_HOST', '127.0.0.1'),
    'port' => env('RAG_DB_PORT', '5432'),
    'database' => env('RAG_DB_DATABASE', 'laravel'),
    'username' => env('RAG_DB_USERNAME', 'postgres'),
    'password' => env('RAG_DB_PASSWORD', ''),
    'prefix' => '',
    'search_path' => env('RAG_DB_SCHEMA', 'public'),
],
  1. Publish and run migrations:
php artisan rag:install

Or manually:

php artisan vendor:publish --provider="Thaolaptrinh\Rag\RagServiceProvider" --tag="rag-config"
php artisan vendor:publish --provider="Thaolaptrinh\Rag\RagServiceProvider" --tag="rag-migrations"
php artisan migrate

Post-Installation: Create HNSW Index

After ingesting your first documents, create the HNSW vector index for optimal search performance:

php artisan rag:ingest "Your document content" --id="doc-1"
php artisan rag:index

Note: The HNSW index cannot be created on an empty table. Run rag:index after your first ingestion.

Environment Variables

# Database (separate from app DB)
RAG_DB_CONNECTION=rag
RAG_DB_HOST=127.0.0.1
RAG_DB_PORT=5432
RAG_DB_DATABASE=laravel
RAG_DB_USERNAME=postgres
RAG_DB_PASSWORD=

# Embedding (OpenAI-compatible HTTP)
RAG_EMBEDDING_API_URL=https://api.openai.com/v1/embeddings
RAG_EMBEDDING_API_KEY=sk-...
RAG_EMBEDDING_MODEL=text-embedding-3-small
RAG_EMBEDDING_DIMENSIONS=1536
RAG_EMBEDDING_BATCH_SIZE=100
RAG_EMBEDDING_TIMEOUT=120

# LLM (OpenAI-compatible HTTP)
RAG_LLM_API_URL=https://api.openai.com/v1/chat/completions
RAG_LLM_API_KEY=sk-...
RAG_LLM_MODEL=gpt-4o-mini
RAG_LLM_MAX_OUTPUT_TOKENS=4096
RAG_LLM_CONTEXT_WINDOW=128000
RAG_LLM_TEMPERATURE=0.7
RAG_LLM_TIMEOUT=120

# Chunking
RAG_CHUNK_SIZE=1000
RAG_CHUNK_OVERLAP=200

# Document
RAG_DOCUMENT_MAX_CONTENT_LENGTH=100000

# Ingestion
RAG_INGESTION_SUB_BATCH_SIZE=10
RAG_INGESTION_PIPELINE_TIMEOUT=600

# Retrieval
RAG_RETRIEVAL_TOP_K=20
RAG_RETRIEVAL_MIN_SCORE=0.0
RAG_HNSW_EF_SEARCH=100

# Prompt
RAG_PROMPT_SYSTEM=You are a helpful assistant...
RAG_PROMPT_TOKENS_PER_CHAR=0.25

# Contextual Retrieval (Optional)
RAG_CONTEXTUAL_ENABLED=false

Usage

Ingest Documents

use Thaolaptrinh\Rag\Data\Document;
use Thaolaptrinh\Rag\Rag;

// Single document
$result = Rag::ingest(Document::create('Your document content here', [
    'source' => 'manual',
    'file_id' => 123,
]));

// Multiple documents
$result = Rag::ingestMany([
    Document::create('Content 1', ['source' => 'pdf']),
    Document::create('Content 2', ['source' => 'pdf']),
]);

Query

// Simple query
$answer = Rag::query('What is this document about?');

// With options
$answer = Rag::query('What is this about?', [
    'top_k' => 10,
    'filters' => ['source' => 'pdf'],
    'system' => 'Custom system prompt',
]);

echo $answer->text;
echo $answer->traceId;

// Access sources
foreach ($answer->sources as $source) {
    echo $source->content;
    echo $source->score;
}

Streaming

Rag::queryStream('Tell me about...', function (string $token): void {
    echo $token;
});

Delete

// Delete single document
Rag::delete('document-id');

// Delete multiple
$count = Rag::deleteMany(['id-1', 'id-2']);

// Delete all
Rag::truncate();

Queue Support

For large document sets, use queued ingestion:

// Queue single document
Rag::ingestQueued($document);
Rag::ingestQueued($document, 'rag-embeddings');

// Queue multiple (one job per document)
Rag::ingestManyQueued($documents);

Testing

use Thaolaptrinh\Rag\Rag;
use Thaolaptrinh\Rag\Data\Document;

beforeEach(function () {
    Rag::fake();
});

it('ingests documents', function () {
    $result = Rag::ingest(Document::create('test content'));
    
    expect($result->ingested)->toBe(1);
    
    Rag::assertIngested('document-id');
});

it('queries the store', function () {
    $answer = Rag::query('What is Laravel?');
    
    expect($answer->text)->toBe('Fake response');
    
    Rag::assertQueried('What is Laravel?');
});

Artisan Commands

# Ingest a document
php artisan rag:ingest "Document content" --id="doc-1" --metadata='{"source": "manual"}'

# Query
php artisan rag:query "What is this about?"

# Delete
php artisan rag:delete doc-1
php artisan rag:delete --all

# Install (publish config + run migrations)
php artisan rag:install
php artisan rag:install --without-migration

# Create HNSW index (after ingesting data)
php artisan rag:index

Architecture

See docs/ARCHITECTURE.md for detailed architecture documentation.

Key Design Decisions

  • Provider-agnostic: Core never mentions specific providers (OpenAI, pgvector, etc.)
  • Batch operations: Use embedBatch(), storeMany() for production performance
  • Idempotent ingestion: Content hash check skips unchanged documents
  • Separate database: Uses RAG_DB_* connection, not app's default
  • Hard delete: No soft delete to avoid HNSW index bloat

Testing

# Run tests
composer test

# Run PHPStan
composer analyse

License

MIT License. See LICENSE.md.

thaolaptrinh/laravel-rag 适用场景与选型建议

thaolaptrinh/laravel-rag 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「llm」 「embeddings」 「rag」 「pgvector」 「retrieval-augmented-generation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 thaolaptrinh/laravel-rag 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thaolaptrinh/laravel-rag 我们能提供哪些服务?
定制开发 / 二次开发

基于 thaolaptrinh/laravel-rag 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-29