📚 RAGuify Docs API和MCP服务器
交互式文档开发工具
递归地抓取整个文档,并使用检索增强生成(RAG)提出基于人工智能的问题
______________________________________________________________________
🎯 概述
RAGify文档 是一个综合工具,通过结合网络抓取、矢量嵌入和人工智能问答,帮助开发人员快速浏览和理解文档。RAGify只需提供一个URL并提问,而不是手动阅读文档——RAGify将找到由实际文档内容支持的最相关答案。
✨ 主要特点
- 🕷️ 递归Web抓取 -自动遍历和提取整个文档网站的内容
- 🧠 矢量嵌入 -使用HuggingFace模型将文档转换为语义嵌入
- 🎯 智能检索 -使用最大边际相关性(MMR)来获取多样化和相关的上下文
- 🤖 AI驱动的答案 -利用Groq的快速语言模型进行准确响应
- ⚡ 智能高速缓存 -在同一文档的多个查询中重用嵌入
- 🔌 多个接口 -通过REST API、MCP服务器或直接Python模块访问
- 📍 来源归属 -获取用于回答您问题的确切文档页面的链接
- 🚀 生产就绪 -内置FastAPI和异步支持,可扩展部署
______________________________________________________________________
🏗️ 项目结构
RAGify-Docs-API/
├── main.py # Core RAG engine - documentation scraping & question answering
├── app.py # FastAPI REST API server
├── mcp_server.py # MCP (Model Context Protocol) server for Claude/AI integrations
├── pyproject.toml # Project metadata and dependencies
├── requirements.txt # Python package requirements
└── README.md # This file组件体系结构
┌─────────────────────────────────────────────────────────┐
│ RAGify Docs API │
├─────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────┐ ┌─────────────┐ │
│ │ FastAPI │ │ MCP Server │ │ Python │ │
│ │ (/ragify) │ │ (ask_docs) │ │ Module │ │
│ └──────┬───────┘ └──────┬───────┘ └──────┬──────┘ │
│ │ │ │ │
│ └─────────────────┼──────────────────┘ │
│ │ │
│ ┌──────▼───────┐ │
│ │ main.py │ │
│ │ (RAG Core) │ │
│ └──────┬───────┘ │
│ │ │
│ ┌─────────────────┼─────────────────┐ │
│ │ │ │ │
│ ┌────▼────┐ ┌─────▼──────┐ ┌────▼─────┐ │
│ │ Scraper │ │ Embeddings │ │ LLM │ │
│ │ (URL) │ │ (HF) │ │ (Groq) │ │
│ └────┬────┘ └─────┬──────┘ └────┬─────┘ │
│ │ │ │ │
│ └─────────────────┼────────────────┘ │
│ │ │
│ ┌──────▼────────┐ │
│ │ Cache Storage │ │
│ │ (In-Mem) │ │
│ └───────────────┘ │
│ │
└─────────────────────────────────────────────────────┘______________________________________________________________________
🚀 安装
先决条件
- Python 3.12+
- pip或uv包管理器
- Groq的API密钥(可选选项:本地使用Ollama)
设置步骤
- 克隆存储库
git clone
cd RAGify-Docs-API- 创建虚拟环境
python -m venv .venv
.venv\Scripts\activate # Windows
# or
source .venv/bin/activate # macOS/Linux- 安装依赖项
pip install -r requirements.txt
# or using uv
uv sync- 创建一个
.env文件 (可选-适用于API密钥)
GROQ_API_KEY=your_groq_api_key_here______________________________________________________________________
📖 用法
选项1:FastAPI REST API
启动服务器:
uvicorn app:app --reload --host 0.0.0.0 --port 8000提出请求:
curl -X POST "http://localhost:8000/ragify" \
-H "Content-Type: application/json" \
-d '{
"url": "https://docs.langchain.com/oss/python/langchain/overview",
"query": "What is LangChain?"
}'Python示例:
import requests
response = requests.post(
"http://localhost:8000/ragify",
json={
"url": "https://docs.python.org/3/",
"query": "How do I create a list?"
}
)
print(response.json())
# {
# "answer": "...",
# "sources": ["https://docs.python.org/3/..."]
# }API文件:
- 交互式文档:
http://localhost:8000/docs(Swagger用户界面) - 重新记录:
http://localhost:8000/redoc
______________________________________________________________________
选项2:MCP服务器
启动MCP服务器:
python mcp_server.py默认配置:
- 主持人:
0.0.0.0 - 端口:
8000(或从PORTenv变量) - 传输:HTTP流式传输
______________________________________________________________________
选项3:直接Python模块
在你自己的Python代码中使用RAGify:
from main import main
# Initialize RAG for a documentation URL
rag_chain = main("https://docs.langchain.com/oss/python/langchain/overview")
# Ask questions
response = rag_chain.invoke({
"input": "What is a retriever in LangChain?"
})
print(response["answer"])
print(response["context"]) # List of source documents______________________________________________________________________
🔑 配置
环境变量
# Groq API Configuration
GROQ_API_KEY=your_key_here
GROQ_MODEL=openai/gpt-oss-120b
# Or use Ollama instead of Groq (local inference)
# Uncomment in main.py: llm = ChatOllama(model="your-model")
# MCP Server Port
PORT=8000定制在 main.py
块大小和重叠:
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000, # Increase for longer contexts
chunk_overlap=200 # Increase for better continuity
)嵌入模型:
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
# Or use: "all-mpnet-base-v2" (larger, more accurate)
)检索参数:
retriever = vector_store.as_retriever(
search_type="mmr",
search_kwargs={
"k": 5, # Number of results to return
"fetch_k": 10, # Candidates to consider
"lambda_mult": 0.5 # Balances similarity vs diversity
}
)LLM选择:
# Use Groq (fast, requires API key)
llm = ChatGroq(model="openai/gpt-oss-120b", temperature=0.2)
# OR use Ollama locally (no API key needed)
# llm = ChatOllama(model="llama2", temperature=0.2)______________________________________________________________________
📋 API 参考
FastAPI端点
POST /ragify
问一个关于文档的问题。
请求:
{
"url": "https://docs.example.com",
"query": "How do I get started?"
}答复:
{
"answer": "To get started with Example...",
"sources": [
"https://docs.example.com/getting-started",
"https://docs.example.com/installation"
]
}状态代码:
200-成功500-RAG初始化或调用错误
______________________________________________________________________
GET /
健康检查和欢迎信息。
答复:
{
"message": "Welcome to the RAGify Docs API! Use the /ragify endpoint to ask questions about documentation."
}______________________________________________________________________
MCP工具: ask_docs
可通过MCP客户端(Claude等)访问
参数:
url(string):要抓取的文档URLquery(string):要问的问题
退货:
{
"answer": "...",
"sources": ["url1", "url2"]
}或者出错:
{
"error": "Error message"
}______________________________________________________________________
🚀 部署
Docker(可选)
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]构建并运行:
docker build -t ragify-docs-api .
docker run -p 8000:8000 -e GROQ_API_KEY=your_key ragify-docs-api______________________________________________________________________
内置于❤️ 适合喜欢优秀文档的开发人员
⭐ 如果你觉得这很有用,请在存储库中加星!
