用途
本技能用于为 GrepAI 安装并配置 Ollama,将其作为本地嵌入(embedding)提供方。Ollama 可实现 100% 私密的代码搜索,代码永远不会离开你的机器。
适用场景包括:
- 为 GrepAI 设置本地、私密的嵌入
- 首次安装 Ollama
- 选择并下载嵌入模型
- 排查 Ollama 连接问题
为什么选择 Ollama
- 隐私:代码永远不会离开你的机器
- 免费:无 API 费用
- 快速:本地处理,无网络延迟
- 离线:无需互联网即可工作
安装
macOS(Homebrew)
# Install Ollama
brew install ollama
# Start the Ollama service
ollama serve
macOS(直接下载)
- 从 ollama.com 下载
- 打开 .dmg 并拖入 Applications
- 从 Applications 启动 Ollama
Linux
# One-line installer
curl -fsSL https://ollama.com/install.sh | sh
# Start the service
ollama serve
Windows
- 从 ollama.com 下载安装程序
- 运行安装程序
- Ollama 会自动作为服务启动
下载嵌入模型
GrepAI 需要一个嵌入模型来将代码转换为向量。
推荐模型:nomic-embed-text
# Download the recommended model (768 dimensions)
ollama pull nomic-embed-text
规格:
- 维度:768
- 大小:约 274 MB
- 性能:非常适合代码搜索
- 语言:针对英语优化
备选模型
# Multilingual support (better for non-English code/comments)
ollama pull nomic-embed-text-v2-moe
# Larger, more accurate
ollama pull bge-m3
# Maximum quality
ollama pull mxbai-embed-large
模型对比:
- nomic-embed-text:768 维,274 MB,适合通用代码搜索
- nomic-embed-text-v2-moe:768 维,500 MB,适合多语言代码库
- bge-m3:1024 维,1.2 GB,适合大型代码库
- mxbai-embed-large:1024 维,670 MB,追求最高准确度
验证安装
检查 Ollama 是否运行
# Check if Ollama server is responding
curl http://localhost:11434/api/tags
# Expected output: JSON with available models
列出已下载模型
ollama list
# Output:
# NAME ID SIZE MODIFIED
# nomic-embed-text:latest abc123... 274 MB 2 hours ago
测试嵌入生成
# Quick test (should return embedding vector)
curl http://localhost:11434/api/embeddings -d '{
"model": "nomic-embed-text",
"prompt": "function hello() { return world; }"
}'
为 Ollama 配置 GrepAI
安装 Ollama 后,配置 GrepAI 使用它:
# .grepai/config.yaml
embedder:
provider: ollama
model: nomic-embed-text
endpoint: http://localhost:11434
这是运行 grepai init 时的默认配置,因此如果使用 nomic-embed-text,无需修改。
运行 Ollama
前台运行(开发)
# Run in current terminal (see logs)
ollama serve
后台运行(macOS/Linux)
# Using nohup
nohup ollama serve &
# Or as a systemd service (Linux)
sudo systemctl enable ollama
sudo systemctl start ollama
检查状态
# Check if running
pgrep -f ollama
# Or test the API
curl -s http://localhost:11434/api/tags | head -1
资源注意事项
内存占用
嵌入模型会加载到 RAM:
- nomic-embed-text:约 500 MB RAM
- bge-m3:约 1.5 GB RAM
- mxbai-embed-large:约 1 GB RAM
CPU 与 GPU
Ollama 默认使用 CPU。为加快嵌入速度:
- macOS:自动使用 Metal(Apple Silicon)
- Linux/Windows:安装 CUDA 以支持 NVIDIA GPU
常见问题
- 问题:连接 localhost:11434 被拒绝。解决:启动 Ollama:
ollama serve - 问题:找不到模型。解决:先拉取模型:
ollama pull nomic-embed-text - 问题:嵌入生成缓慢。解决:使用更小的模型;确保 Ollama 使用 GPU(检查
ollama ps);关闭其他占用内存的应用 - 问题:内存不足。解决:使用更小的模型或增加系统 RAM
最佳实践
- 在 GrepAI 之前启动 Ollama:确保 ollama serve 正在运行
- 使用推荐模型:nomic-embed-text 提供最佳平衡
- 保持 Ollama 运行:将其作为后台服务
- 定期更新:
ollama pull nomic-embed-text以获取更新
输出格式
成功设置后:
✅ Ollama Setup Complete
Ollama Version: 0.1.x
Endpoint: http://localhost:11434
Model: nomic-embed-text (768 dimensions)
Status: Running
GrepAI is ready to use with local embeddings.
Your code will never leave your machine.