nomanur/laravel-markdown-rag 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

nomanur/laravel-markdown-rag

Composer 安装命令:

composer require nomanur/laravel-markdown-rag

包简介

Transform Markdown files into AI-ready context with smart chunking and RAG capabilities for Laravel

README 文档

README

Latest Version on Packagist Total Downloads

Laravel Markdown RAG is a package that allows you to build a Retrieval-Augmented Generation (RAG) system using Markdown files as your knowledge base, powered by Gemini AI.

Installation

You can install the package via composer:

composer require nomanur/laravel-markdown-rag

Publish the package assets and configuration:

php artisan vendor:publish --provider="Nomanur\LaravelMarkdownRAGServiceProvider"
php artisan vendor:publish --provider="Laravel\Ai\AiServiceProvider"

Run the migrations:

php artisan migrate

Configuration

Add the following environment variables to your .env file:

GEMINI_API_KEY=
GEMINI_MODEL=gemini-2.5-flash
AI_EMBEDDING_PROVIDER=gemini
AI_DEFAULT_PROVIDER=gemini
MARKDOWN_INFO_PATH=knowledge-base
MARKDOWN_EMBEDDING_BATCH_SIZE=50
MARKDOWN_AI_RETRY_MAX_ATTEMPTS=3
MARKDOWN_QUERY_REWRITE=false
MARKDOWN_RERANKING=false
  • MARKDOWN_CHAT_RATE_LIMIT: 5 (optional) if you want to rate the limit for the chat.
  • MARKDOWN_INFO_PATH: (optional) the path to the markdown files in the public directory.
  • MARKDOWN_EMBEDDING_BATCH_SIZE: (optional) the number of chunks to process in a single embedding request (default: 50).
  • MARKDOWN_AI_RETRY_MAX_ATTEMPTS: (optional) the maximum number of retry attempts for AI requests (default: 3).
  • MARKDOWN_QUERY_REWRITE: (optional) whether to enable query rewriting for better retrieval (default: false).
  • MARKDOWN_RERANKING: (optional) whether to enable reranking of search results (default: false).

To use the configuration, update the code in config/ai.php with:

'default' => env('AI_DEFAULT_PROVIDER', 'ollama'),
'default_for_images' => env('AI_IMAGE_PROVIDER', 'gemini'),
'default_for_audio' => env('AI_AUDIO_PROVIDER', 'openai'),
'default_for_transcription' => env('AI_TRANSCRIPTION_PROVIDER', 'openai'),
'default_for_embeddings' => env('AI_EMBEDDING_PROVIDER', 'openai'),
'default_for_reranking' => env('AI_RERANKING_PROVIDER', 'cohere'),

'providers' => [
    'gemini' => [
        'driver' => 'gemini',
        'key' => env('GEMINI_API_KEY'),
        'models' => [
            'text' => [
                'default' => env('GEMINI_MODEL', 'gemini-1.5-flash'),
                'cheapest' => env('GEMINI_MODEL_CHEAPEST', 'gemini-1.5-flash'),
                'smartest' => env('GEMINI_MODEL_SMARTEST', 'gemini-2.0-pro-exp-02-05'),
            ],
        ],
    ],
],

Usage

1. Register Routes

Run the following command to register the necessary routes:

php artisan markdownrag:route

2. Setup Knowledge Base

Create a folder for your markdown files within the public directory. By default, this is public/knowledge-base, but you can change it using the MARKDOWN_INFO_PATH environment variable. Add your .md files there.

Example:

public/
└── knowledge-base/
    ├── company/
    │   ├── file1.md
    │   ├── file2.md
    │   └── file3.md
    └── product/
        ├── file1.md
        ├── file2.md
        └── file3.md

3. Indexing

Index your markdown files to make them searchable:

php artisan markdownrag:index

4. Start Queue Worker

Start the queue worker to process background jobs:

php artisan queue:work

5. Accessing the Interface

You can access the chat interface at the following URL: your-domain.com/markdownrag

6. Customizing Message History

By default, KnowledgeAgent retrieves messages from the History model. You can customize how messages are resolved using one of the following options:

Option 1: Global override in AppServiceProvider

Use the resolveMessagesUsing static method to customize message resolution globally:

use Nomanur\Ai\Agents\KnowledgeAgent;
use Nomanur\Models\History;
use Laravel\Ai\Messages\Message;

KnowledgeAgent::resolveMessagesUsing(function ($agent) {
    return History::where('user_id', $agent->user->id)
        ->where('agent', 'knowledge')
        ->latest()
        ->skip(1) // Your custom skip logic
        ->when(config('laravel-markdown-rag.markdown_chat_rate_limit'), fn($query, $limit) => $query->limit($limit))
        ->get()
        ->reverse()
        ->map(fn($message) => new Message($message->role, $message->content))
        ->all();
});

Option 2: Inheritance in another project

If you are extending the agent in another project, you can override the messages() method directly:

use Nomanur\Ai\Agents\KnowledgeAgent;
use Nomanur\Models\History;
use Laravel\Ai\Messages\Message;

class ExtendedKnowledgeAgent extends KnowledgeAgent
{
    public function messages(): iterable
    {
        // Custom implementation with skip(1)
        return History::where('user_id', $this->user->id)
            ->where('agent', 'knowledge')
            ->latest()
            ->skip(1)
            ->when(config('laravel-markdown-rag.markdown_chat_rate_limit'), fn($query, $limit) => $query->limit($limit))
            ->get()
            ->reverse()
            ->map(fn($message) => new Message($message->role, $message->content))
            ->all();
    }
}

7. Customizing Instructions

By default, KnowledgeAgent retrieves its instructions from the associated document's system_prompt or falls back to a default prompt. You can customize the agent's instructions using one of the following options:

Option 1: Global override in AppServiceProvider

Use the resolveInstructionsUsing static method to customize instructions globally:

use Nomanur\Ai\Agents\KnowledgeAgent;

KnowledgeAgent::resolveInstructionsUsing(function (KnowledgeAgent $agent) {
    if ($agent->document) {
        return "You are an expert on {$agent->document->name}. Answer questions strictly based on it.";
    }
    
    return "You are a helpful AI assistant.";
});

Option 2: Inheritance in another project

You can also override the instructions() method directly by extending the agent:

use Nomanur\Ai\Agents\KnowledgeAgent;
use Stringable;

class ExtendedKnowledgeAgent extends KnowledgeAgent
{
    public function instructions(): Stringable|string
    {
        return "You are a highly capable and friendly assistant.";
    }
}

8. Customizing Tool Descriptions

You can also override the description of the KnowledgeSearchTool globally. This is useful if you want to give the AI more specific instructions about when to use the search tool:

use Nomanur\Ai\Tools\KnowledgeSearchTool;

KnowledgeSearchTool::resolveDescriptionUsing(function (KnowledgeSearchTool $tool) {
    return "Search the internal knowledge base for company policies and HR documents only.";
});

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email nomanurrahman@gmail.com instead of using the issue tracker.

Credits

License

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

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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