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

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

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

akira/laravel-rag

Composer 安装命令:

composer require akira/laravel-rag

包简介

Akira RAG for Laravel 12 with PostgreSQL + pgvector

README 文档

README

Latest Version on Packagist Total Downloads

A production-ready Retrieval-Augmented Generation (RAG) system for Laravel 12+ with PostgreSQL + pgvector. Built with clean architecture, type safety, and deterministic behavior.

Features

  • Clean Architecture - Facade → Service → Manager pattern
  • Type Safety - Full type hints and custom exceptions
  • Multi-tenant Support - Optional tenant isolation with custom resolvers
  • Performance - HNSW indexing, caching, and optimized queries
  • Idempotent Ingestion - Hash-based deduplication
  • Audit Logging - Built-in query tracking
  • Deterministic Chunking - Configurable token-based splitting
  • CLI Tools - Rich interactive commands for management

Requirements

  • PHP: 8.4+
  • Laravel: 12+
  • Database: PostgreSQL 14+ (with pgvector extension)
  • Optional: SQLite for testing (in-memory)

Installation

Quick Install (Recommended)

composer require akira/laravel-rag
php artisan rag:install

The installer will guide you through:

  • Publishing configuration files
  • Publishing migrations
  • Running migrations
  • Enabling multi-tenant mode (optional)

Non-Interactive Install

Perfect for CI/CD pipelines:

php artisan rag:install \
  --force \
  --with-tenancy \
  --run-migrate \
  --star

Manual Install

composer require akira/laravel-rag
php artisan vendor:publish --tag="laravel-rag-config"
php artisan vendor:publish --tag="laravel-rag-migrations"
php artisan migrate

PostgreSQL Setup

Ensure pgvector extension is available:

CREATE
EXTENSION IF NOT EXISTS vector;

Quick Start

Basic Usage

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' => '...'
// ]

Advanced Usage

use Akira\Rag\Facades\Rag;

// Filtered retrieval
$answer = Rag::ask(
    question: 'How many vacation days?',
    filters: ['document_id' => '123e4567-e89b-12d3-a456-426614174000']
);

// With custom metadata
Rag::ingest([
    'title' => 'Employee Handbook',
    'source_type' => 'handbook',
    'source_ref' => 'handbook:2024:v2',
    'content' => $content,
    'meta' => [
        'department' => 'HR',
        'version' => 2,
        'effective_date' => '2024-01-01',
        'tags' => ['benefits', 'policies']
    ],
]);

Using Data Transfer Objects

use Akira\Rag\Data\IngestPayload;
use Akira\Rag\Data\AskPayload;

// Type-safe ingestion
$payload = new IngestPayload(
    title: 'Safety Guidelines',
    source_type: 'manual',
    source_ref: 'safety:2024',
    content: $text,
    meta: ['category' => 'safety']
);

Rag::ingest($payload->toArray());

// Type-safe asking
$askPayload = new AskPayload(
    question: 'What are the fire safety procedures?',
    filters: ['category' => 'safety'],
    meta: ['user_id' => auth()->id()]
);

Rag::ask($askPayload->question, $askPayload->filters);

CLI Commands

Interactive Commands

All commands support rich interactive prompts:

# Install and configure
php artisan rag:install

# Ingest content
php artisan rag:ingest

# View statistics
php artisan rag:stats

# Export knowledge base
php artisan rag:export

# Import from backup
php artisan rag:restore backup.json.gz

# Create backups
php artisan rag:backup

# Re-embed documents
php artisan rag:reembed --all

# Import PDF files
php artisan rag:import:pdf documents/

Non-Interactive Mode

Perfect for automation:

# Ingest with options
php artisan rag:ingest \
  --title="My Document" \
  --source_type=manual \
  --source_ref=doc:001 \
  --text="Content here..."

# Export with encryption
php artisan rag:export \
  --output=backup.json \
  --include-embeddings \
  --compress \
  --encrypt

# Stats as JSON
php artisan rag:stats --json

Configuration

The config/rag.php file provides extensive configuration options:

return [
    // AI Models
    'ai' => [
        'embedding_model' => env('RAG_EMBEDDING_MODEL', 'text-embedding-3-small'),
        'chat_model' => env('RAG_CHAT_MODEL', 'gpt-4'),
    ],

    // Chunking Strategy
    'chunking' => [
        'target_tokens' => 800,
        'overlap_tokens' => 120,
    ],

    // Retrieval Settings
    'retrieval' => [
        'top_k' => 5,
        'hybrid' => [
            'enabled' => true,
            'semantic_weight' => 0.7,
            'keyword_weight' => 0.3,
        ],
    ],

    // Caching
    'cache' => [
        'enabled' => true,
        'prefix' => 'rag:v1',
        'ttl_seconds' => 1209600, // 14 days
    ],

    // Audit Logging
    'audit' => [
        'enabled' => true,
    ],

    // Multi-tenancy
    'tenancy' => [
        'enabled' => false,
        'resolver' => \Akira\Rag\Tenant\NullTenantResolver::class,
        'tenant_column' => 'tenant_id',
    ],
];

Multi-Tenancy

Setup

  1. Enable tenancy in config:
'tenancy' => [
    'enabled' => true,
    'resolver' => \App\Rag\MyTenantResolver::class,
    'tenant_column' => 'tenant_id',
],
  1. Create a tenant resolver:
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;
    }
}

Usage

Once configured, all operations are automatically scoped to the current tenant:

// Ingestion is automatically scoped
Rag::ingest([...]);

// Retrieval is automatically scoped
$answer = Rag::ask('...');

// Stats are tenant-specific
$stats = Rag::stats();

Exception Handling

The package provides specific exceptions for different scenarios:

use Akira\Rag\Exceptions\InvalidPayload;
use Akira\Rag\Exceptions\InvalidQuestionException;
use Akira\Rag\Exceptions\DocumentNotFoundException;

try {
    Rag::ingest([
        'title' => 'Test',
        // Missing required fields
    ]);
} catch (InvalidPayload $e) {
    // Handle validation errors
    // $e->getMessage() returns descriptive error
}

try {
    Rag::ask('');
} catch (InvalidQuestionException $e) {
    // Handle empty question
}

Available Exceptions

  • Validation: InvalidPayload, InvalidQuestionException
  • Resources: DocumentNotFoundException, ChunkNotFoundException
  • Processing: ChunkingException, EmbeddingException, RetrievalException
  • Import/Export: ExportException, ImportException
  • Infrastructure: CacheException, DatabaseException, ConfigurationException
  • Tenancy: TenantResolverException

See Exception Documentation for complete details.

Testing

composer test

Writing Tests

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();
});

Architecture

┌─────────────────────────────────────────┐
│           Facade (Rag)                  │
│    Public API Entry Point               │
└─────────────┬───────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────┐
│         RagService                      │
│    Business Logic Layer                 │
└─────────────┬───────────────────────────┘
              │
              ▼
┌─────────────────────────────────────────┐
│         RagManager                      │
│   Core Operations & DB Access           │
│   - Ingestion & Chunking                │
│   - Retrieval & Caching                 │
│   - Tenant Scoping                      │
└─────────────┬───────────────────────────┘
              │
        ┌─────┴─────┬──────────────┐
        ▼           ▼              ▼
   ┌────────┐  ┌────────┐    ┌──────────┐
   │ Models │  │ Cache  │    │ Database │
   └────────┘  └────────┘    └──────────┘

Documentation

For detailed documentation, visit:

Changelog

Please see CHANGELOG for recent changes.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security vulnerabilities, please review our security policy.

Credits

License

The MIT License (MIT). Please see License File for more information.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-18