LEANN 配置指南 本指南可帮助您针对不同用例优化 LEANN,并了解各种配置选项之间的权衡。 开始使用:简单更好 初次尝试 LEANN 时,从一个小数据集入手,快速验证您的方法: 对于文档 RAG:默认的 目录非常合适——包含 2 篇 AI 研究论文、《傲慢与偏见》文学作品以及一份技术报告 对于其他数据源:为快速测试,限制数据集大小 验证无误后,逐步扩大规模: 100 个文档 → 1,000 → 10,000 → 完整数据集 ( ) This helps identify issues early before committing to long processing times Embedding Model Selection: Understanding the
本指南可帮助您针对不同用例优化 LEANN,并了解各种配置选项之间的权衡。
初次尝试 LEANN 时,从一个小数据集入手,快速验证您的方法:
对于文档 RAG:默认的 data/ 目录非常合适——包含 2 篇 AI 研究论文、《傲慢与偏见》文学作品以及一份技术报告
python -m apps.document_rag --query "What techniques does LEANN use?"
对于其他数据源:为快速测试,限制数据集大小
# WeChat: Test with recent messages only python -m apps.wechat_rag --max-items 100 --query "What did we discuss about the project timeline?" # Browser history: Last few days python -m apps.browser_rag --max-items 500 --query "Find documentation about vector databases" # Email: Recent inbox python -m apps.email_rag --max-items 200 --query "Who sent updates about the deployment status?"
验证无误后,逐步扩大规模:
--max-items -1)Based on our experience developing LEANN, embedding models fall into three categories:
Example: sentence-transformers/all-MiniLM-L6-v2 (22M params)
Example: facebook/contriever (110M params), BAAI/bge-base-en-v1.5 (110M params)
Example: Qwen/Qwen3-Embedding-0.6B (600M params), intfloat/multilingual-e5-large (5.6 亿参数)
OpenAI 嵌入(最快设置)
无需下载本地模型即可立即测试(如果您没有 GPU且不太在意文档泄露问题,建议使用此方式,我们将计算嵌入并向 OpenAI API 重新计算):
# Set OpenAI embeddings (requires OPENAI_API_KEY) --embedding-mode openai --embedding-model text-embedding-3-small
Ollama 嵌入(注重隐私)
实现完全私密的本地嵌入:
# First, pull an embedding model ollama pull nomic-embed-text # Use Ollama embeddings --embedding-mode ollama --embedding-model nomic-embed-text
OpenAI 嵌入 (text-embedding-3-small/large)
Local Embeddings
Applies to both LLMs (
leann ask) and embeddings (leann build)。
LEANN 现在将 Ollama、LM Studio 和其他兼容 OpenAI 的运行时视为一流提供商。您只需通过几个标志或环境变量,就能让 LEANN 指向任何兼容的端点——无论是在同一台机器上还是跨网络。
# Works for OpenAI-compatible runtimes such as LM Studio, vLLM, SGLang, llamafile, etc. export OPENAI_API_KEY="your-key" # or leave unset for local servers that do not check keys export OPENAI_BASE_URL="http://localhost:1234/v1" # Ollama-compatible runtimes (Ollama, Ollama on another host, llamacpp-server, etc.) export LEANN_OLLAMA_HOST="http://localhost:11434" # falls back to OLLAMA_HOST or LOCAL_LLM_ENDPOINT
LEANN 还识别 LEANN_LOCAL_LLM_HOST (highest priority), LEANN_OPENAI_BASE_URL, and LOCAL_OPENAI_BASE_URL,因此现有脚本仍可正常工作。
# Build an index with a remote embedding server leann build my-notes \ --docs ./notes \ --embedding-mode openai \ --embedding-model text-embedding-qwen3-embedding-0.6b \ --embedding-api-base http://192.168.1.50:1234/v1 \ --embedding-api-key local-dev-key # Query using a local LM Studio instance via OpenAI-compatible API leann ask my-notes \ --llm openai \ --llm-model qwen3-8b \ --api-base http://localhost:1234/v1 \ --api-key local-dev-key # Query an Ollama instance running on another box leann ask my-notes \ --llm ollama \ --llm-model qwen3:14b \ --host http://192.168.1.101:11434
⚠️ 确保端点可访问:当您的推理服务器运行在家庭或工作站,而索引/搜索任务运行在云端时,服务器必须能够访问您配置的主机。常见方案包括:
tailscale, cloudflared, or ssh -R.When you set these options while building an index, LEANN stores them in meta.json. Any subsequent leann ask or searcher process automatically reuses the same provider settings – even when we spawn background embedding servers. This makes the “server without GPU talking to my local workstation” workflow from issue #80 work out-of-the-box.
Tip: If your runtime does not require an API key (many local stacks don’t), leave --api-key 将流量隧道化。LEANN 将跳过注入凭证。
您也可以从 Python 传递相同的配置:
from leann.api import LeannBuilder builder = LeannBuilder( backend_name="hnsw", embedding_mode="openai", embedding_model="text-embedding-qwen3-embedding-0.6b", embedding_options={ "base_url": "http://192.168.1.50:1234/v1", "api_key": "local-dev-key", }, ) builder.build_index("./indexes/my-notes", chunks)
embedding_options is persisted to the index meta.json, so subsequent LeannSearcher or LeannChat 会话会自动复用相同的提供商设置(嵌入服务器管理器会为您将这些设置转发给提供商)。
适用场景:小型到中型数据集(< 1,000 万向量)——默认且推荐用于极低存储
# Optimal for most use cases --backend-name hnsw --graph-degree 32 --build-complexity 64
适用场景:大型数据集,尤其是当您希望 recompute=True.
Key advantages:
recompute=True enables automatic graph partitioning for smaller indexesRecompute behavior:
recompute=True (recommended): Pure PQ traversal + final reranking - faster and enables partitioningrecompute=False 时:PQ + 遍历过程中部分真实距离——速度较慢但精度更高# Recommended for most use cases --backend-name diskann --graph-degree 32 --build-complexity 64
性能基准测试:运行 uv run benchmarks/diskann_vs_hnsw_speed_comparison.py to compare DiskANN and HNSW on your system.
OpenAI (--llm openai)
gpt-4o-mini (fast, cheap), gpt-4o (best quality), o3 (reasoning), o3-mini (reasoning, cheaper)--thinking-budget low/medium/high for o-series reasoning models (o3, o3-mini, o4-mini)Ollama (--llm ollama)
ollama pullqwen3:0.6b (ultra-fast), qwen3:1.7b (balanced), qwen3:4b (good quality), qwen3:7b (high quality), deepseek-r1:1.5b (reasoning)--thinking-budget low/medium/high for reasoning models like GPT-Oss:20bHuggingFace (--llm hf)
Qwen/Qwen3-1.7B-FP8--build-complexity (index building)
--search-complexity (query time)
--top-k (number of retrieved chunks)
--search-complexityTrade-off formula:
--thinking-budget (reasoning effort level)
low, medium, highlow: Fast responses, basic reasoning (default for simple queries)medium: Balanced speed and reasoning depthhigh: Maximum reasoning effort, best for complex analytical questionsgpt-oss:20b, gpt-oss:120bo3, o3-mini, o4-mini, o1 (o-series reasoning models)--thinking-budget high 可用于复杂分析问题** 如需详细用法示例和实现细节,请查看 思考预算文档**
** 快速示例:**
# OpenAI o-series reasoning model python apps/document_rag.py --query "What are the main techniques LEANN explores?" \ --index-dir hnswbuild --backend hnsw \ --llm openai --llm-model o3 --thinking-budget medium # Ollama reasoning model python apps/document_rag.py --query "What are the main techniques LEANN explores?" \ --index-dir hnswbuild --backend hnsw \ --llm ollama --llm-model gpt-oss:20b --thinking-budget high
--graph-degree
切换到更小的模型:
# From large model --embedding-model Qwen/Qwen3-Embedding-0.6B # To small model --embedding-model sentence-transformers/all-MiniLM-L6-v2
限制数据集大小进行测试:
--max-items 1000 # Process first 1k items only
可选优化:在 Apple Silicon 上使用 MLX:
--embedding-mode mlx --embedding-model mlx-community/Qwen3-Embedding-0.6B-8bit
MLX 可能不是最佳选择,因为我们测试发现它仅比 HF 提供 1.3 倍加速,因此可能使用 Ollama 更适合生成嵌入
--embedding-mode ollama --embedding-model nomic-embed-text
要发现 Ollama 中的更多嵌入模型,请访问 https://ollama.com/search?c=embedding 或阅读更多关于嵌入模型的信息:https://ollama.com/blog/embedding-models,请务必确认最适合您的模型大小
增加检索数量:
--top-k 30 # Retrieve more candidates
升级嵌入模型:
# For English --embedding-model BAAI/bge-base-en-v1.5 # For multilingual --embedding-model intfloat/multilingual-e5-large
每种配置选择都涉及权衡:
| 因素 | 小/快 | 大/质量 |
|---|---|---|
| 嵌入模型 | all-MiniLM-L6-v2 |
Qwen/Qwen3-Embedding-0.6B |
| Chunk Size | 512 tokens | 128 tokens |
| Index Type | HNSW | DiskANN |
| LLM | qwen3:1.7b |
gpt-4o |
关键在于为您的特定用例找到合适的平衡。从小处着手,简单开始,测量性能后再根据需要逐步扩展。
如果您没有本地 GPU 或构建/搜索速度太慢,可以使用以下一种或多种方案。
最快路径,无需本地 GPU 要求。设置您的 API 密钥,在构建和搜索时使用 OpenAI 嵌入:
export OPENAI_API_KEY=sk-... # Build with OpenAI embeddings leann build my-index \ --embedding-mode openai \ --embedding-model text-embedding-3-small # Search with OpenAI embeddings (recompute at query time) leann search my-index "your query" \ --recompute
借助 SkyPilot 将嵌入生成和索引构建卸载到 GPU VM 上。模板位于 sky/leann-build.yaml。
# One-time: install and configure SkyPilot pip install skypilot # Launch with defaults (L4:1) and mount ./data to ~/leann-data; the build runs automatically sky launch -c leann-gpu sky/leann-build.yaml # Override parameters via -e key=value (optional) sky launch -c leann-gpu sky/leann-build.yaml \ -e index_name=my-index \ -e backend=hnsw \ -e embedding_mode=sentence-transformers \ -e embedding_model=Qwen/Qwen3-Embedding-0.6B # Copy the built index back to your local .leann (use rsync) rsync -Pavz leann-gpu:~/.leann/indexes/my-index ./.leann/indexes/
如果您需要更低延迟且有更多存储/内存,可以禁用重新计算。这会存储完整嵌入,避免在搜索时重新计算。
# Build without recomputation (HNSW requires non-compact in this mode) leann build my-index --no-recompute --no-compact # Search without recomputation leann search my-index "your query" --no-recompute
适用场景:
约束条件:
--no-recompute is set, LEANN automatically disables compact mode during build--no-recompute 在搜索时跳过选择性重新计算存储影响:
转换现有索引(需要重建):
# Rebuild in-place (ensure you still have original docs or can regenerate chunks) leann build my-index --force --no-recompute --no-compact
Python API 使用:
from leann import LeannSearcher searcher = LeannSearcher("/path/to/my-index.leann") results = searcher.search("your query", top_k=10, recompute_embeddings=False)
权衡:
快速基准测试结果(benchmarks/benchmark_no_recompute.py,5k 文本,复杂度=32):
HNSW
recompute=True: search_time=0.818s, size=1.1MB recompute=False: search_time=0.012s, size=16.6MB
DiskANN
recompute=True: search_time=0.041s, size=5.9MB recompute=False: search_time=0.013s, size=24.6MB
结论:
no-recompute is significantly faster (no embedding recomputation) but requires much more storage (stores all embeddings)no-recompute uses PQ + partial real distances during traversal (slower but higher accuracy), while recompute=True 使用纯 PQ 遍历 + 最终重排序(遍历更快,支持构建时分区以减少存储)免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。