Skip to content

chenyu-dev25/rag_for_agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

rag_for_agent

基于 Python 的 RAG 学习与实现项目,覆盖从文档加载到答案生成的完整链路。

参考文章

本仓库实现思路主要参考微信公众号文章:

对应模块划分:

文章阶段 本仓库模块
文档加载 rag/load_file.py
语义切块(Meta-Chunking / PPL) rag/chunking/
索引构建(Embedding) rag/indexing/
检索(Query 改写、多查询、HyDE、ReRank 等) rag/retrieval/
生成答案 rag/generation/
端到端 rag/pipeline.py

流水线

文档 → 加载 → 切块 → 向量化入库 → 检索 → Kimi 生成答案

快速开始

# 1. 配置环境
cd rag
cp .env.example .env
# 编辑 .env:MOONSHOT_API_KEY、RAG_EMBEDDING_BACKEND=local 等

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

# 3. 回到项目根目录,跑 demo
cd ..
python -m rag.pipeline rag/demo.txt --no-ppl --query "推荐系统有哪些环节?"

首次使用本地 BGE 需从 Hugging Face 下载模型;若网络不通可先:

python -m rag.pipeline rag/demo.txt --embedding tfidf --no-ppl --query "你的问题"

目录结构

rag/
  load_file.py      # 多格式文档加载(PDF/Word/Markdown/HTML/…)
  chunking/         # Meta-Chunking 语义切块
  indexing/         # Embedding + 向量库(Chroma / NumPy)
  retrieval/        # Query 改写、多查询、HyDE、标签、ReRank
  generation/       # 基于检索上下文的答案生成
  pipeline.py       # 端到端入口
  config.py         # .env 配置加载

环境变量

rag/.env.example

用途 变量
Kimi 对话 MOONSHOT_API_KEYOPENAI_BASE_URLRAG_LLM_MODEL
本地向量 RAG_EMBEDDING_BACKEND=localRAG_EMBEDDING_MODEL
向量库路径 RAG_INDEX_PATH

Python 调用示例

from rag import RAGPipeline

pipe = RAGPipeline()
pipe.build_index("rag/demo.txt", use_ppl=False)
answer = pipe.ask("推荐系统有哪些环节?")
print(answer.answer)

License

学习交流用途;转载请注明出处。

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages