本项目提供企业级 RAG 的可运行示例与评估脚本,涵盖:
- 基础 RAG(LangChain + Chroma + BGE + Ollama)
- 基础 RAG(LlamaIndex + Chroma + BGE + Ollama)
- 多查询 + RRF 融合检索(Multi-Query + Reciprocal Rank Fusion)
- RAG 评估(RAGAs)
- RAG 评估(TruLens)
- 知识图谱示例(LlamaIndex + Neo4j)
说明:示例默认优先使用本地
Ollama模型(如qwen2:7b-instruct-q4_0)。如需使用 OpenAI/通义千问,请在.env中配置对应 API Key,并按 README 指引切换。
企业RAG技术实战项目/
├─ README.md
├─ requirements.txt
├─ .env.example
└─ src/
├─ langchain_naive_rag.py
├─ llamaindex_basic_rag.py
├─ multi_query_rrf_rag.py
├─ ragas_evaluation.py
├─ trulens_evaluation.py
└─ kg_neo4j_example.py
- Python 3.10(推荐)
- Windows 可直接使用本项目(无需 WSL 与 Docker);如需 RAGFlow,请参考下方可选组件
步骤:
- 创建并激活虚拟环境(任选其一)
- Conda:
conda create -n llmrag python=3.10
conda activate llmrag
- venv:
python -m venv .venv
.\.venv\Scripts\activate
- 安装依赖
pip install -r requirements.txt
- 可选:准备本地 LLM(Ollama)
- 安装 Ollama:
https://ollama.com - 拉取模型(示例):
ollama run qwen2:7b-instruct-q4_0
默认服务地址:http://127.0.0.1:11434(Docker 场景可用 http://host.docker.internal:11434)
- 可选:配置 API Key
- 复制
env.example(或.env.example,若系统隐藏文件受限请用env.example)为.env,按需填写:OPENAI_API_KEYDASHSCOPE_API_KEYNEO4J_URL/NEO4J_USERNAME/NEO4J_PASSWORD
运行前建议先启动 Ollama 服务或在 .env 中配置云端模型。
- 基础 RAG(LangChain):
python src/langchain_naive_rag.py
- 基础 RAG(LlamaIndex):
python src/llamaindex_basic_rag.py
- 多查询 + RRF 融合:
python src/multi_query_rrf_rag.py
- RAGAs 评估:
python src/ragas_evaluation.py
- TruLens 评估(启动后会打开本地 Dashboard):
python src/trulens_evaluation.py
- 知识图谱(需要可用的 Neo4j 实例与凭据):
python src/kg_neo4j_example.py
- 数据来源:示例默认抓取百度百科 AIGC 页面做演示用途。
- 向量数据库:优先使用 Chroma,便于 Windows 本地运行与持久化。
- 中文向量模型:使用
BAAI/bge-small-zh-v1.5,具备良好中英双语能力。 - LLM:本地优先
Ollama;如需云端,可切换至 OpenAI/通义千问(示例已注释可切换段落)。 - 多查询融合:将 LLM 生成的多路查询进行检索与分数归并,再通过 RRF 进行融合排序,提升召回覆盖与鲁棒性。
- 评估:提供 RAGAs 与 TruLens 两种评估路径,分别侧重指标自动评测与交互式可视化分析。
如需体验 RAGFlow:
- 安装 Docker(参考官方文档)
- 克隆
ragflow:https://github.com/infiniflow/ragflow - 在其
docker/目录执行:
docker compose -f docker-compose-CN.yml up -d
- 首次运行镜像体积较大(约 9GB),请耐心等待。服务启动后访问
http://127.0.0.1:80。 - Windows Docker 场景中,如需调用本机 Ollama,请在 RAGFlow 中配置
http://host.docker.internal:11434。
- 评估时若遇到
Failed to parse output. Returning None,可先使用本 README 指定的ragas==0.1.12,或按 Ragas 最新版本说明进行调整。 - 网络环境不佳导致依赖/模型下载缓慢,可切换国内镜像源或预先离线下载。
- 如需 GPU 加速,请根据本机 CUDA/驱动版本安装对应的
torch与相关依赖。
- LangChain 文档(v0.2):
https://python.langchain.com/v0.2/docs/introduction/ - Chroma:
https://www.trychroma.com/ - LlamaIndex:
https://docs.llamaindex.ai/en/stable/api_reference/ - RAG 调研综述:
https://arxiv.org/abs/2312.10997 - RAGFlow:
https://github.com/infiniflow/ragflow