Local_Pdf_Chat_RAG

weiwill88MIT活跃维护

用 Python 写的透明 RAG 参考实现,把 FAISS 向量检索和 BM25 关键词检索做混合召回再重排,配 Gradio 界面和 FastAPI 接口,方便本地搭文档问答。

Transparent Python RAG reference with FAISS + BM25 hybrid retrieval, reranking, Gradio UI, and FastAPI.

v2.1.0发布于 2026-08-12 15:03 UTC958star184fork同步于 2026-11-07 07:29 UTC

怎么上手

由 README 提炼,执行前请确认
git clone https://github.com/weiwill88/Local_Pdf_Chat_RAG.git
cd Local_Pdf_Chat_RAG
python3.10 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cp example.env .env
python rag_demo.py
硬件要求按 README 说明

README 未说明显存或 GPU 型号要求;可接本地 Ollama 或云端 API,嵌入与重排模型首次使用时会下载。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

可逐模块阅读的 Python RAG 参考实现,用 FAISS 加 BM25 混合检索把 PDF 问答流程跑通

用 Python 写的透明 RAG 参考实现,把 FAISS 向量检索和 BM25 关键词检索做混合召回再重排,配 Gradio 界面和 FastAPI 接口,方便本地搭文档问答。

核心能力

  • 按请求顺序拆分加载、切分、嵌入、检索、重排、生成模块
  • FAISS 稠密检索与 BM25 关键词检索结果融合
  • 可选 CrossEncoder 或 LLM 打分做结果重排
  • 支持 Ollama、SiliconFlow 及 OpenAI 兼容接口三类后端
  • 同时提供 Gradio 网页界面与 FastAPI REST 接口

容易踩的坑

必须先编辑 .env 配置至少一个模型后端,以 Your_ 开头的值只是占位符不能直接用;索引存在进程内存中,重启后需重建;PDF 只读文本层不做通用 OCR,扫描件无法直接解析。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

2 个版本v2.1.0
  1. v2.1.0v2.1.0 — OSS readiness and bilingual documentation最新正式版2026-08-12 · 2026-08-12 15:03 UTC

    Highlights

    This release makes Local PDF Chat RAG easier to evaluate, run, contribute to, and maintain as an open-source educational RAG reference.

    Added

    • OSI-approved MIT license recognized by GitHub
    • English README and reorganized Chinese README with setup first
    • Current Gradio interface screenshot captured from the running application
    • Credential-free automated tests for startup/status configuration, document loading, BM25 and hybrid retrieval, and missing-key behavior
    • GitHub Actions CI on Python 3.10 using current Node.js 24-compatible official Actions
    • Contribution guide, security policy, code of conduct, issue forms, and pull request template
    • Central project version metadata and changelog

    Changed

    • Clarified the project as an inspectable educational/reference implementation rather than a production-ready service
    • Declared the dependencies required for Excel parsing
    • Constrained Gradio to the supported 6.x line
    • Fixed theme initialization so the Gradio 6 UI renders without a JavaScript syntax error
    • Updated the repository homepage, description, and topics to point to the project itself

    Removed

    • Book, course, community, and store promotion from the repository

    Verification

    • 8 automated tests pass
    • Python source compilation passes
    • GitHub Actions passes on the release commit
    • Gradio 6.23.1 UI returns HTTP 200 and renders successfully in a real browser
    • GitHub community profile reports 100% health and identifies the MIT license

    See CHANGELOG.md for the version history.

  2. v2.0.0v2.0.0 模块化重构 + Bug修复2026-03-18 · 2026-03-18 03:11 UTC
    查看更新说明

    🎉 v2.0.0 重大更新

    🔧 架构重构

    • 模块化拆分:将 2775 行的单文件 rag_demo.py 拆分为 15 个独立模块
      • core/(8个模块):文档加载 → 文本分块 → 向量化 → FAISS索引 → BM25索引 → 混合检索 → 重排序 → 生成回答
      • features/(3个模块):联网搜索、矛盾检测、思维链处理
      • utils/:网络工具
      • config.py:配置中心
    • 消除全局变量:使用 VectorStore 类封装 FAISS 索引和数据
    • 代码精简 32%:总代码从 ~2775 行优化至 ~1891 行

    🐛 Bug 修复

    • 修复: 提问后看不到回复 — process_chat 从 generator 改为 return 模式
    • 修复: .env 加载不生效 — 优先加载 .env,回退到 example.env
    • 修复: Ollama 模型名格式错误 — 改为标准 Ollama 格式 deepseek-r1:8b

    ✨ 新功能

    • LLM 后端自动检测:启动时自动检测 SiliconFlow API / Ollama,无需手动选择
    • 亮/暗主题切换:右上角 🌓 按钮一键切换,支持 localStorage 持久化
    • AutoFaissIndex 实际启用:根据数据量自动选择最优 FAISS 索引类型
    • Gradio 6.x 完整适配:CSS/JS/Chatbot API 全面兼容

    📖 文档优化

    • README 新增 venv 分平台指南(Mac/Linux/Windows)
    • 新增 .env 配置步骤和 LLM 自动检测说明
    • 新增项目结构学习路线图

    📦 环境要求

    • Python 3.9+
    • 至少配置以下其中一项:
      • SILICONFLOW_API_KEY(云端,推荐)
      • 本地 Ollama 服务

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Local PDF Chat RAG

    A transparent, runnable Python implementation for learning and inspecting RAG

    English | 简体中文

    CI License: MIT Python Release Stars

    Local PDF Chat RAG is an educational and reference implementation for developers who want to inspect the complete retrieval-augmented generation pipeline. Document loading, chunking, embeddings, FAISS, BM25, hybrid retrieval, reranking, and answer generation are split into readable and replaceable modules. The repository includes both a Gradio UI and a FastAPI interface.

    This repository is intended for learning and experimentation. It is not a production-ready knowledge-base service. Add authentication, tenant isolation, persistence, evaluation, security controls, and deployment governance before using it with real business data.

    Current Local PDF Chat RAG interface

    Usage and maintenance

    Repository snapshot as of 2026-08-31:

    SignalVerifiable snapshot
    Adoption951 stars and 179 forks
    Recent GitHub traffic105 unique visitors and 78 unique cloners in the latest 14-day window, through 2026-08-30
    Release history2 published releases; current version: v2.1.0
    Maintenance activity5 commits in the last 90 days, with the latest main update on 2026-08-16
    Continuous integrationLatest main CI run passed; source compilation and credential-free tests run on pull requests
    Issue and PR handling38 issues closed and 0 open; 9 pull requests merged and 1 under review

    Recent maintenance includes reviewing and merging an external retrieval fix, publishing bilingual documentation, adding credential-free tests and GitHub Actions CI, adopting the MIT license, and documenting a private security-reporting process.

    Stars, forks, and traffic are dated GitHub snapshots. Traffic and clone counts indicate repository interest, not verified installations or production deployments. See the repository, Actions, and Releases for the current state.

    Why this project

    • Inspectable pipeline: core modules follow the order in which a RAG request is processed.
    • Hybrid retrieval: combines FAISS dense retrieval with BM25 keyword retrieval.
    • Optional reranking: supports a CrossEncoder or model-based relevance scoring.
    • Multiple model backends: local Ollama, SiliconFlow, and OpenAI-compatible APIs.
    • Document support: PDF, TXT, Markdown, DOCX, XLS/XLSX, and PPTX.
    • Two interfaces: a Gradio web application and a FastAPI REST API.
    • Verifiable maintenance: automated tests, GitHub Actions CI, contribution guidance, and a security-reporting process.

    RAG pipeline

    flowchart LR
        A[Documents] --> B[Parsing]
        B --> C[Chunking]
        C --> D[Embeddings]
        D --> E[FAISS]
        C --> F[BM25]
        E --> G[Hybrid retrieval]
        F --> G
        G --> H[Reranking]
        H --> I[Context building]
        I --> J[LLM generation]
        J --> K[Answer and sources]
    

    Quick start

    1. Create an environment

    git clone https://github.com/weiwill88/Local_Pdf_Chat_RAG.git
    cd Local_Pdf_Chat_RAG
    
    python3.10 -m venv .venv
    source .venv/bin/activate  # Windows: .venv\Scripts\activate
    python -m pip install --upgrade pip
    pip install -r requirements.txt
    

    2. Configure one model backend

    cp example.env .env
    

    Edit .env and choose at least one option:

    • set SILICONFLOW_API_KEY;
    • set MAGICK_API_KEY, its endpoint, and model name; or
    • start Ollama locally and pull the model configured in .env.

    Keep real credentials in your local .env file. Values beginning with Your_ are treated as placeholders and are not valid credentials.

    3. Start the web UI

    python rag_demo.py
    

    The application first tries http://127.0.0.1:17995, then ports 17996–17999 if needed.

    4. Start the REST API

    python api_router.py
    

    Main endpoints:

    • GET /api/status: runtime and provider configuration status;
    • POST /api/upload: upload and process a document;
    • POST /api/ask: ask a question against processed documents.

    Repository layout

    ├── config.py                  # Environment, model, and RAG settings
    ├── rag_demo.py                # Gradio web UI
    ├── api_router.py              # FastAPI interface
    ├── core/
    │   ├── document_loader.py     # Document extraction
    │   ├── text_splitter.py       # Text chunking
    │   ├── embeddings.py          # Embeddings
    │   ├── vector_store.py        # FAISS index
    │   ├── bm25_index.py          # BM25 index
    │   ├── retriever.py           # Hybrid and recursive retrieval
    │   ├── reranker.py            # Result reranking
    │   └── generator.py           # Context and answer generation
    ├── features/                  # Web search and optional extensions
    ├── tests/                     # Tests that require no external credentials
    └── .github/                   # CI, issue forms, and pull request template
    

    Tests

    pip install -r requirements-dev.txt
    PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest
    

    The current suite covers:

    • configuration and default backend selection;
    • TXT, Markdown, and unsupported-file loading behavior;
    • BM25 and hybrid-result merging;
    • clean, network-free failure when an API key is missing.

    GitHub Actions compiles the Python sources and runs the test suite for every pull request.

    Configuration

    See example.env for the complete example. Common variables include:

    VariablePurpose
    SILICONFLOW_API_KEYSiliconFlow API credential
    SILICONFLOW_MODEL_NAMESiliconFlow model ID
    MAGICK_API_KEYOpenAI-compatible provider credential
    MAGICK_API_URLProvider base URL or full Chat Completions URL
    MAGICK_MODEL_NAMEProvider model ID
    OLLAMA_MODEL_NAMELocal Ollama model name
    SERPAPI_KEYOptional web-search credential
    RERANK_METHODcross_encoder or llm

    Known limitations

    • PDF extraction reads the text layer and does not provide general-purpose OCR.
    • Excel and PowerPoint extraction focuses on text rather than visual layout.
    • The index is currently in process memory and must be rebuilt after restart.
    • Embedding and reranking models may be downloaded on first use.
    • Cloud model and web-search requests send the relevant query to third-party services; review your data boundary first.

    Contributing

    Reproducible bug reports, documentation improvements, and focused pull requests are welcome. Read CONTRIBUTING.md and CODE_OF_CONDUCT.md before contributing.

    Do not open a public issue for a vulnerability. Follow SECURITY.md instead.

    Releases and maintenance

    License

    Released under the MIT License.