684 words
3 minutes
enterprise RAG architecture

Architecture#

System Overview#

┌─────────────────────────────────────────────────────────┐
│ API Client │
└─────────────────────┬───────────────────────────────────┘
│ HTTP
┌─────────────────────────────────────────────────────────┐
│ FastAPI Server │
│ (uvicorn, port 8080) │
│ │
│ ┌─────────────┐ ┌──────────────────────────────┐ │
│ │ /health │ │ /retrieve/by-skill │ │
│ │ /retrieve │───▶│ /retrieve/by-module │ │
│ └─────────────┘ └──────────┬───────────────────┘ │
│ │ │
│ ┌──────────▼───────────────────┐ │
│ │ HybridRetriever │ │
│ │ (L1 QueryResultCache) │ │
│ └──────────┬───────────────────┘ │
└───────────────────────────────┬┼──────────────────────────┘
┌───────────────────────┼───────────────────────┐
│ │ │
▼ ▼ ▼
┌───────────────┐ ┌────────────────────┐ ┌──────────────┐
│ BM25Retriever│ │ DashScope Embedding │ │DashScope │
│ (in-memory) │ │ + Vector Search │ │ Reranker │
│ L3 Cache │ │ (pgvector) │ │ │
└───────┬───────┘ └──────────┬─────────┘ └──────┬──────┘
│ │ │
└───────────────────────┼─────────────────────┘
┌───────────────────────┐
│ PostgreSQL + pgvector │
│ L2 Storage │
└───────────────────────┘

Retrieval Pipeline#

by-skill 流程#

Query: "Token管理"
┌─────────────────────────────┐
│ 1. L1 Cache Check │ ──hit──▶ return cached result
│ (query + top_k → result) │
└─────────────┬───────────────┘
│ miss
┌─────────────────────────────┐
│ 2. Query Expansion │ ──▶ ["Token管理", "Token", "JWT", "AccessToken", ...]
│ (SKILL_POINT_SYNONYMS) │
└─────────────┬───────────────┘
┌────────┴────────┐
▼ ▼
┌────────────┐ ┌─────────────────────┐
│ BM25 │ │ Dense Vector Search │
│ (in-memory)│ │ search_similar_chunks│
│ L3 Cache │ │ (pgvector) │
└─────┬──────┘ └──────────┬──────────┘
│ │
└─────────┬──────────┘
┌─────────────────────────────┐
│ 3. RRF Fusion (k=60) │
│ score = Σ 1/(k + rank) │
└─────────────┬───────────────┘
┌─────────────────────────────┐
│ 4. DashScope Rerank │
│ qwen-rerank model │
└─────────────┬───────────────┘
┌─────────────────────────────┐
│ 5. Cache result (TTL 5min) │
│ return top_k results │
└─────────────────────────────┘

by-module 流程#

Query: "用户认证"
┌─────────────────────────────┐
│ 1. L1 Cache Check │
└─────────────┬───────────────┘
│ miss
┌─────────────────────────────┐
│ 2. search_by_module() │ ──▶ PostgreSQL WHERE module = ?
│ (pgvector) │
└─────────────┬───────────────┘
┌─────────────────────────────┐
│ 3. DashScope Rerank │
└─────────────┬───────────────┘
┌─────────────────────────────┐
│ 4. Cache + return │
└─────────────────────────────┘

Data Models#

Chunk (storage)#

数据库存储的文档块。

字段类型说明
contentstring块文本内容
chunk_indexint块序号
levelint标题层级 (2=##, 3=###)
parent_headingstring父标题
heading_pathstring完整路径
sourcestring来源文档名
modulestring模块名
skill_pointslist[string]技能点列表
score_pointslist[string]评分要点
embeddingvector(1536)块级向量
bm25_scorefloatBM25 分数

RetrievedDocument (API response)#

API 返回的检索结果。

content: str # 块文本内容
metadata: dict # 元数据
score: float # 相似度分数

Caching Strategy#

层级组件缓存内容TTL说明
L1QueryResultCache完整检索结果5min相同 query 直接返回
L2PostgreSQL + pgvector向量 + BM25 数据持久化数据库层
L3BM25Retriever (in-memory)BM25 倒排索引进程内API 启动时从 DB 加载

Configuration#

所有参数集中在 config.toml

[retrieval]
top_k = 5 # 默认返回文档数
rerank_pool_size = 15 # Reranking 候选池大小
[matching]
module_match_threshold = 0.7 # 模块识别相似度阈值
[rrf]
k = 60 # RRF 融合参数
[dashscope]
api_key = "${DASHSCOPE_API_KEY}" # 环境变量
embedding_model = "text-embedding-v3"
reranker_model = "qwen-rerank"
[database]
url = "${ENTERPRISE_KB_DATABASE_URL}" # 环境变量

Document Format#

Markdown 文档需包含 YAML front-matter:

---
skill_points:
- 用户登录
- Token管理
module: 用户认证
score_points:
- 基本: 是否理解登录流程原理
---
# 用户认证模块
## 功能规范
### 1. 登录流程
...

Front-matter 字段#

字段类型说明
skill_pointslist[string]关联的技能点列表
modulestring所属模块名(唯一)
score_pointslist[string]评分要点

CLI Index Builder#

Terminal window
# 全量重建
uv run python scripts/build_index.py --source ./enterprise-kb/
# 增量更新(目前为提示模式)
uv run python scripts/build_index.py --source ./enterprise-kb/ --incremental

Git Hook#

hooks/post-commit 复制到 .git/hooks/post-commit 即可在每次提交后自动增量更新索引。

enterprise RAG architecture
https://sgjki547.top/posts/rag-architecture/
Author
SGJki
Published at
2026-04-21
License
CC BY-NC-SA 4.0