Skip to content

Repository files navigation

CANDE-AI RAG System

🏗️ 企业级知识检索助手

基于 RAG (Retrieval-Augmented Generation) 技术的企业内部知识库解决方案

Python License

📋 项目简介

CANDE-AI 是一套面向企业内部的知识检索系统,基于 RAG 技术实现智能问答。系统已在上海灿德建材有限公司内部部署运行,服务于产品查询、技术支持等场景。

核心特性

  • 🔍 智能检索:基于向量语义搜索 + BM25 关键词搜索的混合检索
  • 🤖 AI 问答:集成大语言模型,自动生成专业回答
  • 📄 多格式支持:支持 PDF、PPTX、DOCX 等文档格式
  • 🔒 私有部署:数据完全私有,不依赖第三方云服务
  • 📊 可观测性:完整的查询追踪和性能监控

技术栈

组件 技术选型 说明
向量数据库 ChromaDB 本地部署,无需外部服务
Embedding DashScope / OpenAI 支持多种 Embedding 服务
LLM MiMo / OpenAI 支持多种大语言模型
精排模型 Cross-Encoder 本地部署,提升检索精度
前端框架 Streamlit 快速构建 Web 应用
内网穿透 Cloudflare Tunnel 免费公网访问方案

🚀 快速开始

环境要求

  • Python 3.10+
  • 4GB+ 内存(用于加载精排模型)
  • 稳定的网络连接(用于调用 Embedding/LLM API)

安装步骤

# 1. 克隆仓库
git clone https://github.com/your-username/cande-ai.git
cd cande-ai

# 2. 创建虚拟环境
python -m venv .venv
source .venv/bin/activate  # Linux/Mac
# 或 .venv\Scripts\activate  # Windows

# 3. 安装依赖
pip install -r requirements.txt

# 4. 配置环境变量
cp .env.example .env
# 编辑 .env 文件,填入你的 API 配置

# 5. 配置系统参数
cp config/settings.yaml.example config/settings.yaml
# 编辑 config/settings.yaml 文件

配置说明

环境变量 (.env)

# LLM API 配置
LLM_API_KEY=your-api-key
LLM_BASE_URL=https://api.openai.com/v1
LLM_MODEL=gpt-4

# Embedding API 配置
EMBEDDING_API_KEY=your-api-key
EMBEDDING_BASE_URL=https://api.openai.com/v1
EMBEDDING_MODEL=text-embedding-3-small

系统配置 (config/settings.yaml)

# 向量数据库配置
vector_store:
  provider: "chroma"
  persist_directory: "./data/db/chroma"
  collection_name: "knowledge_hub"

# 检索配置
retrieval:
  dense_top_k: 20      # 向量检索返回数量
  sparse_top_k: 20     # BM25 检索返回数量
  fusion_top_k: 10     # 融合后返回数量
  rrf_k: 60            # RRF 融合参数

# 精排配置
rerank:
  enabled: true
  provider: "cross_encoder"
  model: "cross-encoder/ms-marco-MiniLM-L-6-v2"
  top_k: 5

文档摄入

# 摄入单个文件
python scripts/ingest.py --path data/documents/report.pdf --collection my_docs

# 摄入整个目录
python scripts/ingest.py --path data/documents/ --collection my_docs

# 转换 PPTX/DOCX 并摄入
python scripts/convert_and_ingest.py --collection my_docs

启动服务

# 启动本地服务
streamlit run public_qa.py --server.port 8502 --server.address 0.0.0.0

# 启动公网服务(需要配置 Cloudflare Tunnel)
./start.sh

📁 项目结构

cande-ai/
├── config/                    # 配置文件
│   ├── settings.yaml.example  # 系统配置模板
│   └── prompts/              # Prompt 模板
├── data/                     # 数据目录
│   ├── documents/            # 原始文档
│   ├── db/                   # 向量数据库
│   └── images/               # 提取的图片
├── scripts/                  # 工具脚本
│   ├── ingest.py             # 文档摄入脚本
│   ├── convert_and_ingest.py # 格式转换+摄入
│   ├── query.py              # 查询脚本
│   └── evaluate.py           # 评估脚本
├── src/                      # 源代码
│   ├── core/                 # 核心模块
│   │   ├── query_engine/     # 查询引擎
│   │   ├── trace/            # 追踪系统
│   │   └── response/         # 响应构建
│   ├── ingestion/            # 摄入管道
│   │   ├── chunking/         # 文档分块
│   │   ├── embedding/        # 向量化
│   │   ├── storage/          # 存储层
│   │   └── transform/        # 数据转换
│   ├── libs/                 # 第三方集成
│   │   ├── llm/              # LLM 接口
│   │   ├── embedding/        # Embedding 接口
│   │   ├── vector_store/     # 向量数据库接口
│   │   └── reranker/         # 精排模型
│   └── observability/        # 可观测性
│       └── dashboard/        # 管理后台
├── public_qa.py              # 公网问答入口
├── start.sh                  # 启动脚本
├── .env.example              # 环境变量模板
└── README.md                 # 项目说明

🔧 核心功能

1. 混合检索

系统采用向量语义搜索 + BM25 关键词搜索的混合检索策略:

# 向量检索:语义相似度
dense_results = vector_store.query(embedding, top_k=20)

# BM25 检索:关键词匹配
sparse_results = bm25_indexer.query(keywords, top_k=20)

# RRF 融合:结合两种检索结果
fused_results = rrf_fusion.fuse([dense_results, sparse_results])

2. 智能精排

使用 Cross-Encoder 模型对检索结果进行精排:

# 加载精排模型
reranker = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")

# 计算相关性分数
scores = reranker.predict([(query, doc) for doc in documents])

# 按分数排序
ranked_results = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True)

3. 多格式文档处理

支持 PDF、PPTX、DOCX 等格式的自动转换和摄入:

# 自动检测文件格式
if file_path.suffix == '.pptx':
    pdf_path = convert_pptx_to_pdf(file_path)
elif file_path.suffix == '.docx':
    pdf_path = convert_docx_to_pdf(file_path)

# 摄入到向量数据库
ingest_document(pdf_path, collection)

📊 性能指标

指标 数值 说明
检索延迟 <500ms 向量检索 + BM25 检索
精排延迟 <200ms Cross-Encoder 精排
端到端延迟 <3s 包含 LLM 生成回答
准确率 >85% 基于内部测试集

🔒 安全说明

  • 所有 API Key 通过环境变量管理,不硬编码在代码中
  • 支持本地部署,数据不离开企业网络
  • 可配置访问控制和审计日志

🚀 部署方案

方案 1:本地部署(推荐)

适合小型团队(5-20 人),成本低,数据完全私有。

# 本地启动
streamlit run public_qa.py --server.port 8502 --server.address 0.0.0.0

# 配置内网穿透(可选)
./start.sh

方案 2:云服务器部署

适合中大型团队(20-100 人),稳定性高。

# 使用 Docker 部署
docker build -t cande-ai .
docker run -p 8502:8502 -v ./data:/app/data cande-ai

# 或使用 docker-compose
docker-compose up -d

方案 3:混合部署

核心数据本地存储,计算资源云端扩展。

📈 监控与运维

查询追踪

系统自动记录每次查询的详细信息:

{
  "trace_id": "xxx",
  "query": "淋浴花洒有哪些型号?",
  "collection": "sanitary-ware",
  "retrieval_time_ms": 450,
  "rerank_time_ms": 180,
  "llm_time_ms": 2100,
  "total_time_ms": 2730,
  "results_count": 5
}

性能监控

# 查看查询日志
tail -f logs/traces.jsonl

# 统计查询性能
python scripts/analyze_traces.py

🤝 贡献指南

欢迎贡献代码、报告问题或提出改进建议。

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/xxx)
  3. 提交更改 (git commit -m 'Add feature xxx')
  4. 推送到分支 (git push origin feature/xxx)
  5. 创建 Pull Request

📄 许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件

🙏 致谢

📞 联系方式


CANDE-AI ©2024 灿德建材. All Rights Reserved.

About

企业级知识检索助手 - 基于 RAG 技术的企业内部知识库解决方案

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages