← blog 架构与系统 · 2026-04-20

My Knowledge RAG

企业知识库 RAG 系统的使用说明:环境配置、索引构建、项目结构、命令参考与检索 API 概览。

4 min read

Enterprise KB RAG

企业知识库 RAG 系统 — 提供标准化 REST API,通过混合检索(BM25 + 向量 + RRF + 重排)查询企业内部最佳实践和技术规范。

快速开始

1. 安装依赖

uv sync

2. 配置环境变量

必需的环境变量在 config.toml 中配置,无需复制 .env:

[database]
url = "postgresql+asyncpg://postgres:YOUR_PASSWORD@localhost:5432/AI-Interview"

[dashscope]
api_key = "YOUR_DASHSCOPE_API_KEY"

3. 初始化数据库

确保 PostgreSQL 已安装并启用 pgvector 扩展(PostgreSQL 15+):

CREATE EXTENSION IF NOT EXISTS vector;
CREATE DATABASE AI-Interview;

4. 构建索引

# 全量重建(会清空现有数据)
uv run python scripts/build_index.py --source ./enterprise-kb/

# 增量更新(暂未实现)
uv run python scripts/build_index.py --source ./enterprise-kb/ --incremental

5. 启动服务

uv run python -m enterprise_kb.api

项目结构

enterprise-kb/
├── enterprise-kb/              # Markdown 源文档(知识库内容)
├── src/enterprise_kb/
│   ├── api.py                 # FastAPI 服务与路由
│   ├── cache.py               # L1 查询结果缓存(TTL 5min)
│   ├── config.py              # 配置加载(config.toml)
│   ├── models.py             # 数据模型(Chunk、RetrievedDocument)
│   ├── markdown_parser.py     # Markdown 解析与分块
│   ├── storage.py             # PostgreSQL + pgvector 存储层
│   ├── bm25_retriever.py      # BM25 稀疏检索(内存索引)
│   ├── embedding_service.py   # DashScope text-embedding-v3 向量化
│   ├── reranker.py           # DashScope qwen3-rerank 重排序
│   ├── fusion.py             # RRF(Reciprocal Rank Fusion)融合
│   └── retriever.py          # HybridRetriever 编排层
├── scripts/
│   └── build_index.py         # CLI 索引构建工具
├── tests/                      # 测试套件
├── config.toml                 # 配置文件
└── pyproject.toml

命令参考

命令说明
uv sync安装依赖
uv run pytest tests/ -v运行全部测试
uv run python scripts/build_index.py --source ./enterprise-kb/全量重建索引
uv run python -m enterprise_kb.api启动 API 服务(默认 0.0.0.0:8080)

知识库文档格式

知识库文档放在 enterprise-kb/ 目录下,以 Markdown + YAML front-matter 编写:

---
skill_points:
  - Redis数据类型
  - 缓存穿透
module: Redis缓存
score_points:
  - "基础: 能否正确选择 String/Hash 类型并说明适用场景"
  - "进阶: 能否设计缓存穿透防御方案"
---
# Redis缓存模块

## 功能规范

### 数据类型与适用场景

#### String
最基础类型,最大 512MB...

#### Hash
适合存储对象结构...

front-matter 字段:

字段必填说明
skill_points是技能点列表,供 /retrieve/by-skill 检索
module是模块名称,供 /retrieve/by-module 检索
score_points是评分要点,格式 "级别: 描述"

正文标题层级:

层级行为
#文档标题,跳过
##父级上下文,不单独成块
###上下文前缀,每个 #### chunk 的内容前缀
####最小分块单位,每个 #### 及下内容合成一个 chunk

检索流程

by-skill 流程(按技能点检索):

graph TD
    REQ["请求: POST /retrieve/by-skill"] --> S1["1. 元数据预过滤 → 从 DB 筛选 skill_points 匹配的 chunk IDs"]
    S1 --> S2["2. BM25 稀疏检索 → 在过滤出的 IDs 中用 rank_bm25 计算"]
    S2 --> S3["3. 向量检索 → 在过滤出的 IDs 中用 pgvector 余弦距离"]
    S3 --> S4["4. RRF 融合 → k=60,混合稀疏+密集排序"]
    S4 --> S5["5. 重排序 → DashScope qwen3-rerank 输出最终顺序"]

by-module 流程(按模块检索):

graph TD
    REQ["请求: POST /retrieve/by-module"] --> M1["1. 元数据预过滤 → 从 DB 筛选 module 匹配的 chunk IDs"]
    M1 --> M2["2. 向量检索 → 在过滤出的 IDs 中用 pgvector"]
    M2 --> M3["3. 重排序 → DashScope qwen3-rerank"]

缓存:L1 缓存(QueryResultCache,TTL 5min),按 {query}:{top_k} 缓存检索结果。

API 端点

  • GET /health — 健康检查,返回索引统计
  • POST /retrieve/by-skill — 按技能点检索
  • POST /retrieve/by-module — 按模块检索

详见 docs/api.md

架构说明

详见 docs/architecture.md

Sources

No external sources for this entry.

Related