Metadata-Version: 2.4
Name: ragforge-sdk
Version: 1.0.0
Summary: Modular, composable RAG retrieval library —— Build your retrieval pipeline like forging parts.
Author-email: jiangnanboy <2229029156@qq.com>
License:              Apache License
                                   Version 2.0, January 2004
                                https://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [2026] [jiangnanboy]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               https://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
Project-URL: GitHub, https://github.com/jiangnanboy/RAGForge
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: jieba>=0.42.1
Requires-Dist: numpy>=1.24.0
Requires-Dist: scikit-learn>=1.2.0
Requires-Dist: fastembed>=0.7.4
Requires-Dist: rank-bm25>=0.2.2
Requires-Dist: openai>=1.109.1
Requires-Dist: requests>=2.32.5
Dynamic: license-file

<div align="center">
  <img src="logo.png" alt="Image" width="80%" />
</div>

<h1 align="center">RAGForge</h1>

<div align="center">

<p align="center">
  <strong>模块化、可组合的 RAG 检索库 —— 像锻造零件一样构建你的检索管道</strong>
</p>

README：[中文](README.md) | [English](README_EN.md)

</div>

<p align="center">
  <strong>纯 CPU，零依赖，一个库搞定 RAG 检索管道。</strong>
</p>

<p align="center">
  Embedding 和 Rerank 全部 ONNX CPU 推理，无需 GPU。<br>
  查询理解可选接入 DeepSeek API，后续将支持本地小模型。<br>
  <strong>6 个 Protocol 接口 · 10+ 可组合组件 · X-Ray 级追踪</strong>
</p>

<p align="center">
  <a href="#为什么选-ragforge">为什么选</a> ·
  <a href="#30-秒上手">快速上手</a> ·
  <a href="#x-ray-追踪">X-Ray</a> ·
  <a href="#功能全景">功能</a> ·
  <a href="#architecture">架构</a> ·
  <a href="#installation">安装</a>
</p>

---

## 💡 为什么选 RAGForge

做一个 RAG 检索，你需要什么？

- 一个 Embedding 模型把文本变成向量
- 一个 Reranker 对候选文档精排
- BM25 + 向量的混合检索
- RRF 融合两路结果
- Query 改写、分解、HyDE 等查询理解
- 一套评估体系来调参

LangChain 和 LlamaIndex 都能做，但它们是**全栈框架**——你引入它们，就引入了几百个你可能永远用不到的依赖。更关键的是，它们都是**黑盒**：你不知道 BM25 给了每个文档多少分，不知道 RRF 融合后排名为什么变了，不知道 Rerank 的 logit 怎么映射到最终分数。

**RAGForge 的定位：只做检索管道这一段，做到极致，而且完全透明。**

| | LangChain | LlamaIndex | **RAGForge** |
|---|:---:|:---:|:---:|
| **定位** | 全栈 LLM 框架 | 全栈 RAG 框架 | **专注检索管道** |
| **CPU 推理** | 依赖外部库 | 依赖外部库 | ✅ **Embedding + Rerank 全 CPU** |
| **GPU 依赖** | 常需要 | 常需要 | ✅ **完全不需要** |
| **混合检索** | ✅ 但黑盒 | ✅ 但黑盒 | ✅ 每步可追踪 |
| **RRF 融合** | 简单实现 | 未内置 | ✅ Bonus + 自适应 |
| **X-Ray 调试** | ❌ | ❌ | ✅ **独有** |
| **Query 理解** | 基础 | 基础 | ✅ Multi-Query + HyDE |
| **LLM 评估** | 通用 | 通用 | ✅ 专为检索设计 |
| **组件可替换** | 受限 | 受限 | ✅ Protocol 接口 |
| **向量库绑定** | ❌ | ❌ | ✅ 不关心 |
| **零外部依赖** | ❌ | ❌ | ✅ BM25 零依赖即可用 |

> **一句话**：如果你只需要 BM25，用 3 行代码；如果你需要 Hybrid + Rerank + Multi-Query + X-Ray + A/B 评估，也能一行不漏。全看你组装什么。**而且全程 CPU，不需要一块 GPU。**

---

## ⚡ 30 秒上手

安装：pip install ragforge-sdk

### 最简：纯关键词搜索（零依赖）

```python
from ragforge import SearchPipeline, BM25Retriever

pipeline = SearchPipeline(retriever=BM25Retriever())
results = pipeline.search("苹果手机", ["iPhone 15 价格", "华为手机"])
for doc, score in results:
    print(f"  {score:.4f} → {doc}")
```

不需要 Embedding，不需要向量库，不需要 GPU。`jieba` + `rank_bm25` 就够了。

### 完整：Hybrid + Rerank + Blend + X-Ray

```python
from ragforge import (
    SearchPipeline, FastembedEmbedder, FastembedReranker,
    ModelConfig, PipelineConfig,
)

model_cfg = ModelConfig(
    embedding_model_path="/path/to/multilingual-e5-small-onnx",
    rerank_model_path="/path/to/bge-reranker-v2-m3-ONNX",
)

pipeline = SearchPipeline(
    embedder=FastembedEmbedder(model_cfg),
    reranker=FastembedReranker(model_cfg),
)

query = "苹果手机多少钱"
documents = ["iPhone 15售价多少", "苹果手机官方定价", "华为手机报价"]

results, trace = pipeline.search(query, documents, trace=True)
print(trace.formatted)
```

> **全部 CPU 推理**。Embedding 模型 ~400MB，Reranker 模型 ~500MB（int8 量化），首次自动下载，之后缓存到 `~/.cache/RAGForge/`。不需要 CUDA，不需要 GPU 驱动。

### 组件独立使用

```python
from ragforge import BM25Retriever, VectorRetriever, FastembedEmbedder

# 独立的 BM25
bm25 = BM25Retriever()
results = bm25.retrieve("查询", ["文档1", "文档2"])

# 独立的向量检索
embedder = FastembedEmbedder(ModelConfig())
vector = VectorRetriever(embedder)
results = vector.retrieve("查询", ["文档1", "文档2"])
```

---

## 🔬 X-Ray 追踪

这是 RAGForge 的**杀手级功能**。

大多数 RAG 工具只告诉你最终结果。RAGForge 的 X-Ray 追踪展示**每一步的内部计算**——每个文档在每个阶段的精确分数。

```python
results, trace = pipeline.search(query, documents, trace=True)
print(trace.xray)
```

```
Step                      Time  Output
--------------------------------------------------------------------------------
Retrieval              1917.6ms  3 candidates
Rerank                 1834.1ms  3 docs reranked
Blend                     0.0ms  3 results blended

Final Results:
  Top1  0.3087  苹果手机官方定价
  Top2  0.2821  iPhone 15售价多少
  Top3  0.0640  华为手机报价

Query: 苹果手机多少钱
Total: 3751.8ms

┌─ BM25 Retrieval ─────────────────────────────────────────
│  "苹果手机官方定价                    " BM25=    0.6  rank=1
│  "iPhone 15售价多少               " BM25=    0.5  rank=2
│  "华为手机报价                      " BM25=    0.1  rank=3
└────────────────────────────────────────────────────────────

┌─ Vector Retrieval ─────────────────────────────────────────
│  "苹果手机官方定价                    " cos_sim=0.9609  rank=1
│  "iPhone 15售价多少               " cos_sim=0.9362  rank=2
│  "华为手机报价                      " cos_sim=0.9250  rank=3
└────────────────────────────────────────────────────────────

┌─ RRF Fusion ─────────────────────────────────────────
│  "苹果手机官方定价                    " rrf=0.0645  +bonus_rank1=0.05  → 0.1145
│  "iPhone 15售价多少               " rrf=0.0635  +bonus_rank2_3=0.02  → 0.0835
│  "华为手机报价                      " rrf=0.0625  +bonus_rank2_3=0.02  → 0.0825
└────────────────────────────────────────────────────────────

┌─ Rerank ────────────────────────────────────────────
│  "苹果手机官方定价                    " logit=   2.1  sigmoid=0.891  "Highly relevant"
│  "iPhone 15售价多少               " logit=   2.0  sigmoid=0.878  "Highly relevant"
│  "华为手机报价                      " logit=  -4.7  sigmoid=0.009  "Low relevance"
└────────────────────────────────────────────────────────────

┌─ Blend ─────────────────────────────────────────────
│  "苹果手机官方定价                    " 0.1145×0.75 + 0.891×0.25 = 0.3087
│  "iPhone 15售价多少               " 0.0835×0.75 + 0.878×0.25 = 0.2821
│  "华为手机报价                      " 0.0825×0.75 + 0.009×0.25 = 0.0640
└────────────────────────────────────────────────────────────

════════════════════════════════════════════════════════════
  FINAL RESULTS:
  #1   0.3087  苹果手机官方定价  ← FINAL #1
  #2   0.2821  iPhone 15售价多少  ← FINAL #2
  #3   0.0640  华为手机报价  ← FINAL #3

```

> 一眼看到每个文档为什么排在这个位置——是 BM25 拉上去的？还是 Rerank？权重怎么分配的？调参再也不用猜。

---

## 🗺️ 功能全景

### 🔎 检索核心

| 功能 | 说明 | 推理 |
|------|------|------|
| **Hybrid Retrieval** | BM25 + Dense Vector 并行检索 | CPU |
| **RRF Fusion** | Reciprocal Rank Fusion + Top-Position Bonus | CPU |
| **Cross-Encoder Reranking** | ONNX Reranker (int8 量化) + Sigmoid 归一化 | CPU |
| **Position-Aware Blending** | 按排名区间动态分配检索分 / 重排分权重 | CPU |
| **Adaptive Fusion** | 从反馈中自动学习最优 RRF 参数 | CPU |

### 🧠 查询理解

| 功能 | 说明 | 推理 |
|------|------|------|
| **Query Rewrite** | 口语化查询 → 检索友好查询 | DeepSeek API / 未来本地小模型 |
| **Query Decomposition** | 多意图查询 → 2-4 个子查询 | DeepSeek API / 未来本地小模型 |
| **HyDE** | 生成假设答案文档提升向量召回 | DeepSeek API / 未来本地小模型 |
| **Query Expansion** | 同义词 / 相关词扩展 | DeepSeek API / 未来本地小模型 |
| **Multi-Query Retrieval** | 原始 + 改写查询分别检索 → RRF 融合 | CPU + API |

> **设计哲学**：检索管道的核心计算（Embedding、BM25、Rerank、Fusion）全部本地 CPU 完成，**不依赖任何外部 API**。查询理解（Query Rewrite 等）目前通过 DeepSeek API 实现，后续将支持接入本地小模型（如 Qwen2.5-1.5B、DeepSeek-R1-Distill），实现完全离线部署。

### 🔭 可观测性

| 功能 | 说明 |
|------|------|
| **X-Ray Trace** | 每个文档在每一步的精确分数（**独有**） |
| **Pipeline Trace** | 步骤级计时 + 摘要 |
| **Latency Profiler** | 每步耗时 + 占比报告 |

### 📊 评估

| 功能 | 说明 |
|------|------|
| **LLM-as-Judge** | DeepSeek 自动判断相关性 |
| **Metrics** | NDCG / Recall / Precision / MRR |
| **A/B Comparison** | 两个管道配置并排对比 |

### 🛠 工程能力

| 功能 | 说明 |
|------|------|
| **Semantic Cache** | 基于向量相似度的结果缓存 |
| **Document Dedup** | 基于向量相似度的文档去重 |
| **Protocol-Based** | 6 个接口，任何组件都可替换 |
| **Zero Global State** | 所有依赖显式注入 |
| **Lazy Loading** | 模型首次使用时加载，自动缓存 |

---

## 📖 详细用法
详见demo.py

<details>
<summary><b>🔧 查询理解（DeepSeek API）</b></summary>

```python
from ragforge import QueryPlanner, LLMConfig

llm_cfg = LLMConfig(api_key="sk-your-deepseek-key")
planner = QueryPlanner(llm_cfg)

# 改写：口语 → 检索友好
rewritten = planner.rewrite("苹果手机多少钱")
# → "苹果 iPhone 系列手机 价格 报价 官方售价"

# 分解：多意图 → 子查询
sub_queries = planner.decompose("对比iPhone和华为的拍照效果")
# → ["iPhone 拍照效果评测", "华为手机 拍照效果评测", "iPhone vs 华为 拍照对比"]

# HyDE：生成假设答案
hypothetical = planner.hyde("什么是RAG")
# → "RAG（Retrieval-Augmented Generation）是一种结合了检索..."

# 扩展：同义词
expansions = planner.expand("深度学习")
# → ["neural network", "machine learning", ...]
```

</details>

<details>
<summary><b>🔄 Multi-Query Retrieval（原始 + 改写分别检索）</b></summary>

```python
from ragforge import (
    SearchPipeline, QueryPlanner, RRFFusion, PositionAwareBlend,
    FastembedEmbedder, FastembedReranker,
    ModelConfig, PipelineConfig, LLMConfig, QueryTransformStrategy,
)

pipe_cfg = PipelineConfig(
    query_transform_strategy=QueryTransformStrategy.RETRIEVE_AND_FUSE,
)

pipeline = SearchPipeline(
    embedder=FastembedEmbedder(model_cfg),
    reranker=FastembedReranker(model_cfg),
    fusion=RRFFusion(pipe_cfg),
    blend=PositionAwareBlend(pipe_cfg),
    query_transform=QueryPlanner(LLMConfig(api_key="sk-...")),
    config=pipe_cfg,
)

# 原始查询 + 改写查询 → 并行检索 → RRF 融合
results, trace = pipeline.search("苹果手机多少钱", documents, trace=True)
```

流程：
```
query ──► [改写] ─┬──► [检索: 原始查询]   ──┐
                   └──► [检索: 改写查询]   ──┼──► [RRF 融合] ──► [Rerank] ──► 结果
```

> 💡 也支持 `decompose()`——原始 + N 个子查询 → (N+1) 路并行检索，全部融合。

</details>

<details>
<summary><b>📊 评估 & A/B 对比</b></summary>

```python
from ragforge import Evaluator, LLMJudge, LLMConfig

judge = LLMJudge(LLMConfig(api_key="sk-..."))
evaluator = Evaluator(judge=judge)

# 评估管道质量
metrics = evaluator.evaluate(pipeline, queries, ground_truth, top_k=5)
print(f"NDCG@5: {metrics.ndcg:.3f}, Recall@5: {metrics.recall:.3f}, MRR: {metrics.mrr:.3f}")

# A/B 对比两个管道
comparison = evaluator.compare(
    pipeline_a=hybrid_pipeline,
    pipeline_b=bm25_only_pipeline,
    queries=queries,
    ground_truth=ground_truth,
)
print(comparison["report"])
```

```
Pipeline Comparison Report
======================================================================
Metric                Pipeline A  Pipeline B
--------------------------------------------
NDCG@5                    0.8200      0.6100
Recall@5                  0.9000      0.7000
Precision@5               0.7200      0.5200
MRR                       0.9500      0.7500
Avg Latency (ms)         320.0       12.0
```

</details>

<details>
<summary><b>⚡ 自适应融合（从反馈中学习）</b></summary>

```python
from ragforge import AdaptiveFusion

feedback = [
    ("苹果手机价格", {"iPhone 15 官方售价 5999元", "苹果官网定价"}),
    ("Python教程", {"Python入门指南", "Python最佳实践"}),
]

fusion = AdaptiveFusion.from_feedback(feedback)
print(f"Best: k={fusion.best_config.rrf_k}, weight={fusion.best_config.query_weight}")

pipeline = SearchPipeline(fusion=fusion)
```

</details>

<details>
<summary><b>📦 语义缓存 & 文档去重</b></summary>

```python
# 语义缓存：相似查询直接返回缓存结果
from ragforge import SemanticCache
cache = SemanticCache(embedder=embedder, similarity_threshold=0.95)
result1 = cache.get_or_search("苹果手机价格", pipeline.search, documents)
result2 = cache.get_or_search("苹果手机多少钱", pipeline.search, documents)  # 缓存命中
print(cache.stats)  # {'entries': 1, 'hits': 1, 'misses': 0, 'hit_rate': 1.0}

# 文档去重：基于向量相似度
from ragforge import Deduplicator
dedup = Deduplicator(embedder=embedder, threshold=0.95)
unique = dedup.deduplicate(documents)
print(f"{len(documents)} → {len(unique)} unique documents")
```

</details>

<details>
<summary><b>🎨 自定义组件（Protocol 接口）</b></summary>

```python
# 只需实现接口方法，无需继承任何类
class MyRetriever:
    def retrieve(self, query: str, documents: list[str]) -> list[tuple[str, int]]:
        # 你的检索逻辑
        return [(doc, rank) for rank, doc in enumerate(sorted_docs)]

# 换用 OpenAI Embedding
class OpenAIEmbedder:
    def __init__(self, api_key): ...
    def embed(self, text: str) -> np.ndarray: ...
    def embed_batch(self, texts: list[str]) -> list[np.ndarray]: ...

# 直接插入管道
pipeline = SearchPipeline(retriever=MyRetriever())
```

</details>

---

## 🏛 Architecture

### Pipeline Flow

```
query ──► [Query Transform] ──► [Retriever] ──► [Fusion] ──► [Rerank] ──► [Blend] ──► results
              optional           always       if hybrid    optional    optional
```

Multi-Query 模式：

```
query ──► [Transform] ─┬──► [检索: 原始查询]  ──┐
                      ├──► [检索: 改写查询]  ──┼──► [RRF 融合] ──► [Rerank] ──► [Blend] ──► results
                      └──► [检索: 子查询3]   ──┘
```

### Protocol Interfaces

```python
class Embedder(Protocol):
    def embed(self, text: str) -> np.ndarray: ...
    def embed_batch(self, texts: list[str]) -> list[np.ndarray]: ...

class Retriever(Protocol):
    def retrieve(self, query: str, documents: list[str]) -> list[tuple[str, int]]: ...

class Reranker(Protocol):
    def rerank(self, query: str, documents: list[str]) -> list[float]: ...

class FusionStrategy(Protocol):
    def fuse(self, ranked_lists: list[list[tuple[str, int]]]) -> list[tuple[str, float]]: ...

class QueryTransform(Protocol):
    def transform(self, query: str) -> str | list[str]: ...

class Judge(Protocol):
    def judge(self, query: str, documents: list[str]) -> list[dict]: ...
```

### Project Structure

```
ragforge/
├── protocols.py                 # 6 个 Protocol 接口定义
├── type_utils.py                # 共享数据类型 + X-Ray 格式化
├── pipeline.py                  # SearchPipeline（组装所有组件）
│
├── config/
│   ├── model_config.py          # 模型路径配置
│   ├── pipeline_config.py       # 算法超参数 + QueryTransformStrategy
│   └── llm_config.py            # DeepSeek LLM 配置
│
├── models/                      # ONNX CPU 推理模型
│   ├── embedding.py             # FastembedEmbedder (ONNX)
│   └── reranker.py              # FastembedReranker (ONNX, int8)
│
├── retrieval/
│   ├── bm25.py                  # BM25Retriever (jieba)
│   ├── vector.py                # VectorRetriever (cosine)
│   └── hybrid.py                # HybridRetriever (parallel)
│
├── fusion/
│   ├── rrf.py                   # RRFFusion (bonus + adaptive)
│   ├── blend.py                 # PositionAwareBlend
│   └── adaptive.py              # AdaptiveFusion (auto-tuned)
│
├── llm/
│   └── llm_client.py              # LLMClient (OpenAI-compatible)
│
├── query/
│   └── planner.py               # QueryPlanner (rewrite/decompose/hyde/expand)
│
├── evaluation/
│   ├── judge.py                 # LLMJudge (LLM-as-Judge)
│   └── evaluator.py             # Evaluator (NDCG/Recall/MRR + A/B)
│
├── cache/                       # SemanticCache
├── dedup/                       # Deduplicator
├── tracing/                     # Tracer
└── profiler.py                  # PipelineProfiler
```

---

## ⚙️ Configuration

### ModelConfig

| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `embedding_model_path` | `str \| None` | `None` | Embedding 模型路径（None 则自动下载） |
| `embedding_dim` | `int` | `384` | 向量维度 |
| `rerank_model_path` | `str \| None` | `None` | Reranker 模型路径（None 则自动下载） |

### PipelineConfig

| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `rrf_k` | `int` | `60` | RRF 常数 |
| `top_k_recall` | `int` | `30` | 融合后保留的候选数 |
| `query_weight` | `float` | `2.0` | RRF 分数权重 |
| `bonus_rank1` | `float` | `0.05` | 第 1 名加分 |
| `bonus_rank2_3` | `float` | `0.02` | 第 2-3 名加分 |
| `query_transform_strategy` | `str` | `"replace"` | `"replace"` 或 `"retrieve_and_fuse"` |
| `blend_weights` | `dict` | 见源码 | 按排名区间的融合权重 |

### LLMConfig

| 参数 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `api_key` | `str` | `""` | DeepSeek API Key |
| `base_url` | `str` | `"https://api.deepseek.com"` | API 地址 |
| `model` | `str` | `"deepseek-chat"` | 模型名称 |

---

## API Reference

### 检索组件

| 类 | 接口 | 说明 | 推理 |
|---|------|------|------|
| `FastembedEmbedder` | `Embedder` | ONNX Embedding (multilingual-e5-small) | CPU |
| `FastembedReranker` | `Reranker` | ONNX Cross-Encoder (bge-reranker-v2-m3, int8) | CPU |
| `BM25Retriever` | `Retriever` | BM25 全文检索 (jieba 分词) | CPU |
| `VectorRetriever` | `Retriever` | 余弦相似度向量检索 | CPU |
| `HybridRetriever` | `Retriever` | BM25 + Vector 并行 | CPU |
| `RRFFusion` | `FusionStrategy` | RRF + Top-Position Bonus | CPU |
| `PositionAwareBlend` | `FusionStrategy` | 位置感知权重融合 | CPU |
| `AdaptiveFusion` | `FusionStrategy` | 反馈驱动的参数自学习 | CPU |
| `SearchPipeline` | — | 可组合管道 | CPU |

### 查询 & 评估

| 类 | 接口 | 说明 | 推理 |
|---|------|------|------|
| `QueryPlanner` | `QueryTransform` | 改写 / 分解 / HyDE / 扩展 | API (DeepSeek) |
| `LLMJudge` | `Judge` | LLM 相关性判断 | API (DeepSeek) |
| `Evaluator` | — | NDCG / Recall / MRR + A/B | API + CPU |

### 工程组件

| 类 | 说明 |
|---|------|
| `SemanticCache` | 向量相似度缓存 |
| `Deduplicator` | 近似文档去重 |
| `Tracer` | 步骤级追踪 |
| `PipelineProfiler` | 延迟分析 |

---

## 📦 Installation

### 核心依赖（检索功能，全 CPU）

```bash
pip install fastembed rank_bm25 jieba scikit-learn numpy requests
```

### LLM 功能（Query Understanding、Evaluation）——可选

```bash
pip install openai  # DeepSeek 兼容 OpenAI API
```

> **不装 openai 也完全没问题。** BM25、Embedding、Rerank、RRF、Blend 全部独立运行。只有当你需要 Query Rewrite、HyDE、LLM 评估时才需要接入 LLM。

### 默认模型

| 组件 | 模型 | 大小     | 量化 | 推理 |
|------|------|--------|------|------|
| Embedding | multilingual-e5-small-onnx | ~400MB | FP16 | **CPU** |
| Reranker | bge-reranker-v2-m3-ONNX-int8 | ~500MB | int8 | **CPU** |

来源：[ModelScope](https://modelscope.cn/profile/jiangnanboy)

自动下载到 `~/.cache/RAGForge/`，首次使用时懒加载。

---

## License

Apache2.0 License - see the [LICENSE](LICENSE) file for details.

