🚀 DeepRepo - 本地RAG引擎
DeepRepo 是一個生產級的 Python 庫,支持在本地代碼庫上執行 RAG(檢索增強生成),並支持多種 AI 供應商。
🚀 快速開始
安裝
cd deeprepo_core
pip install -e .
詳細的各供應商設置說明請參閱 INSTALLATION.md。
基本用法
from deeprepo import DeepRepoClient
client = DeepRepoClient(provider_name="ollama")
client = DeepRepoClient(
embedding_provider_name="openai",
llm_provider_name="anthropic"
)
result = client.ingest("/path/to/your/code")
print(f"已攝入 {result['chunks_processed']} 個塊")
response = client.query("身份驗證是如何工作的?")
print(response['answer'])
print(f"來源: {response['sources']}")
✨ 主要特性
- 純 Python 實現:無需使用大型框架(如 LangChain/LlamaIndex),也無需外部向量數據庫。
- 多 AI 供應商支持:支持 Ollama(本地)、HuggingFace、OpenAI、Anthropic 和 Gemini。
- MCP 服務器支持:可與 Cursor、Claude Desktop、Antigravity 等 MCP 客戶端集成。
- 基於裝飾器的插件系統:便於供應商註冊和擴展。
- 向量存儲:使用 NumPy 實現餘弦相似度,並以 JSON 格式持久化。
- RESTful API:提供 FastAPI 服務,便於集成。
- 支持 Docker:可進行完整的容器化部署。
📦 安裝指南
安裝 MCP 依賴
pip install deeprepo[mcp]
啟動 MCP 服務器
deeprepo-mcp
python -m deeprepo.mcp.server
配置 Cursor
創建或編輯 ~/.cursor/mcp.json:
{
"mcpServers": {
"deeprepo": {
"command": "python",
"args": ["-m", "deeprepo.mcp.server"],
"env": {
"LLM_PROVIDER": "ollama"
}
}
}
}
使用不同的供應商:
{
"mcpServers": {
"deeprepo": {
"command": "python",
"args": ["-m", "deeprepo.mcp.server"],
"env": {
"EMBEDDING_PROVIDER": "openai",
"LLM_PROVIDER": "anthropic",
"OPENAI_API_KEY": "sk-...",
"ANTHROPIC_API_KEY": "sk-ant-..."
}
}
}
}
啟動 FastAPI 服務器
export OPENAI_API_KEY=your-key
uvicorn web_app.main:app --reload
Docker 部署
docker-compose up --build
服務將在 http://localhost:8000 上可用。
💻 使用示例
基礎用法
client = DeepRepoClient(provider_name="ollama")
result = client.ingest("/path/to/your/code")
print(f"已攝入 {result['chunks_processed']} 個塊")
response = client.query("身份驗證是如何工作的?")
print(response['answer'])
print(f"來源: {response['sources']}")
高級用法
client = DeepRepoClient(
embedding_provider_name="openai",
llm_provider_name="anthropic"
)
result = client.ingest("/path/to/your/code")
print(f"已攝入 {result['chunks_processed']} 個塊")
response = client.query("身份驗證是如何工作的?")
print(response['answer'])
print(f"來源: {response['sources']}")
📚 詳細文檔
支持的 AI 供應商
| 供應商 |
成本 |
速度 |
適用場景 |
| Ollama |
免費 |
快速 |
本地開發、注重隱私、離線工作 |
| HuggingFace |
免費* |
中等 |
基於雲的應用,無需本地設置 |
| OpenAI |
付費 |
非常快 |
生產環境,追求最佳質量 |
| Anthropic |
付費 |
非常快 |
生產環境,推理能力出色 |
| Gemini |
免費* |
中等 |
測試,適用於 Google 生態系統 |
*免費套餐有使用限制
供應商示例
client = DeepRepoClient(provider_name="ollama")
client = DeepRepoClient(provider_name="huggingface")
client = DeepRepoClient(provider_name="openai")
client = DeepRepoClient(
embedding_provider_name="openai",
llm_provider_name="anthropic"
)
client = DeepRepoClient(provider_name="gemini")
client = DeepRepoClient(
embedding_provider_name="huggingface",
llm_provider_name="openai"
)
API 端點
| 方法 |
端點 |
描述 |
| GET |
/ |
健康檢查 |
| GET |
/stats |
獲取向量存儲統計信息 |
| POST |
/ingest |
從目錄中攝入文檔 |
| POST |
/chat |
使用 RAG 進行查詢 |
| POST |
/clear-history |
清除對話歷史 |
API 示例
curl -X POST http://localhost:8000/ingest \
-H "Content-Type: application/json" \
-d '{"path": "/path/to/code"}'
curl -X POST http://localhost:8000/chat \
-H "Content-Type: application/json" \
-d '{"query": "這段代碼是做什麼的?"}'
可用的 MCP 工具
| 工具 |
描述 |
ingest_codebase |
將目錄攝入到向量存儲中 |
query_codebase |
使用 RAG 查詢知識庫 |
search_similar |
查找相似代碼,無需大語言模型 |
get_stats |
獲取向量存儲統計信息 |
clear_history |
清除對話歷史 |
詳細的 MCP 配置請參閱 deeprepo_core/src/deeprepo/mcp/README.md。
配置
環境變量
| 變量 |
描述 |
必需的供應商 |
HUGGINGFACE_API_KEY 或 HF_TOKEN |
HuggingFace API 令牌 |
HuggingFace 供應商 |
OPENAI_API_KEY |
OpenAI API 密鑰 |
OpenAI 供應商 |
ANTHROPIC_API_KEY |
Anthropic API 密鑰 |
Anthropic 供應商 |
GEMINI_API_KEY |
Google Gemini API 密鑰 |
Gemini 供應商 |
切換供應商
client = DeepRepoClient(
provider_name="ollama",
storage_path="vectors.json"
)
client = DeepRepoClient(
embedding_provider_name="openai",
llm_provider_name="anthropic",
storage_path="vectors.json"
)
或者使用環境變量:
export LLM_PROVIDER=ollama
python your_script.py
export EMBEDDING_PROVIDER=openai
export LLM_PROVIDER=anthropic
python your_script.py
常見用例:
- 使用 Anthropic 進行大語言模型:由於 Anthropic 沒有嵌入 API,建議與 OpenAI 或 HuggingFace 配合使用。
- 成本優化:使用免費的 HuggingFace 進行嵌入,付費的 OpenAI 進行大語言模型。
- 性能優化:使用快速的 OpenAI 進行嵌入,強大的 Anthropic 進行大語言模型。
測試
快速開始
pytest tests/unit/ -v
pytest tests/unit/ --cov=deeprepo --cov-report=html
pytest tests/ -v
測試結構
手動測試供應商
python tests/integration/test_all_providers.py ollama
python tests/integration/test_all_providers.py huggingface openai
詳細的測試文檔請參閱 tests/README.md。
開發
添加新的供應商
- 在
src/deeprepo/providers/ 中創建一個新文件。
- 實現
EmbeddingProvider 和 LLMProvider 接口。
- 使用
@register_embedding 和 @register_llm 裝飾器。
- 供應商將自動被發現!
示例:
from deeprepo.interfaces import EmbeddingProvider, LLMProvider
from deeprepo.registry import register_embedding, register_llm
@register_embedding("my_provider")
class MyEmbeddingProvider(EmbeddingProvider):
def embed(self, text: str) -> list[float]:
pass
@register_llm("my_provider")
class MyLLM(LLMProvider):
def generate(self, prompt: str, context: str = None) -> str:
pass
🔧 技術細節
架構
deeprepo_core/
├── src/deeprepo/
│ ├── client.py # 主門面
│ ├── storage.py # 向量存儲 (JSON + NumPy)
│ ├── ingestion.py # 文件掃描和分塊
│ ├── interfaces.py # 抽象基類
│ ├── registry.py # 基於裝飾器的註冊表
│ ├── mcp/ # 用於 AI 助手的 MCP 服務器
│ │ ├── server.py # FastMCP 服務器
│ │ └── README.md # MCP 文檔
│ └── providers/
│ ├── ollama_v.py # Ollama (本地,免費)
│ ├── huggingface_v.py # HuggingFace (雲,免費)
│ ├── openai_v.py # OpenAI (付費)
│ ├── anthropic_v.py # Anthropic (付費)
│ └── gemini_v.py # Gemini (免費套餐)
設計模式
- 倉庫模式:
VectorStore 將存儲與應用邏輯解耦。
- 策略模式:
LLMProvider 和 EmbeddingProvider 抽象接口。
- 註冊表模式:
@register_llm 裝飾器用於動態發現供應商。
- 單例模式:FastAPI 在啟動時加載一次客戶端。
📄 許可證
本項目採用 MIT 許可證,詳細信息請參閱 LICENSE 文件。
貢獻
歡迎貢獻代碼!請隨時提交拉取請求。
支持
如有問題、疑問或功能請求,請在 GitHub 上提交問題。
為希望完全控制其 RAG 管道的開發者打造