Skip to content

本地 AI 完整指南

在 Obsidian 中使用本地部署的 AI 模型,实现完全隐私保护、零成本的智能知识管理。

为什么选择本地 AI?

优势说明
🔒 隐私保护数据完全不出本地,敏感内容安全
💰 零成本无 API 费用,无限次调用
🌐 离线可用无需网络,随时随地使用
⚙️ 完全控制自由选择模型、调整参数
劣势说明
💻 硬件要求需要足够的内存和存储空间
🧠 模型能力相比 GPT-4 等云端模型能力有限
🛠️ 配置复杂需要一定的技术知识

方案对比

主流本地 AI 方案

方案特点难度推荐度
Ollama一键安装,简单易用⭐⭐⭐⭐⭐
LM Studio图形界面,可视化操作⭐⭐⭐⭐⭐⭐
GPT4All开源免费,跨平台⭐⭐⭐⭐⭐
llama.cpp命令行,高度可定制⭐⭐⭐⭐⭐⭐

推荐方案:Ollama(简单易用,社区支持好)

Ollama 完整配置

安装 Ollama

macOS / Linux

bash
# 一键安装
curl -fsSL https://ollama.ai/install.sh | sh

# 启动服务
ollama serve

Windows

powershell
# 下载安装包
# 访问 https://ollama.ai/download 下载 Windows 版本

Docker 部署

bash
# 使用 Docker 运行
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama

# 进入容器
docker exec -it ollama ollama run llama3.2

模型选择指南

推荐模型列表

模型大小特点适用场景
llama3.22GB通用、速度快日常使用
qwen2.54.7GB中文优秀中文笔记
deepseek-r14.7GB推理强复杂任务
mistral4.1GB均衡通用场景
gemma25.5GBGoogle 开源多语言
phi32.2GB微软小模型轻量部署

模型下载命令

bash
# 通用模型
ollama pull llama3.2

# 中文优化模型
ollama pull qwen2.5

# 推理增强模型
ollama pull deepseek-r1

# 嵌入模型(用于语义搜索)
ollama pull nomic-embed-text

# 查看已安装模型
ollama list

# 删除模型
ollama rm model_name

硬件要求

模型规模最小内存推荐内存GPU 显存
3B 参数8GB16GB6GB+
7B 参数16GB32GB8GB+
13B 参数32GB64GB12GB+
70B 参数64GB128GB48GB+

启动与测试

bash
# 启动服务
ollama serve

# 新终端窗口测试
ollama run llama3.2

# 交互示例
>>> 你好,请介绍一下自己
>>> 请帮我总结以下内容:...

# 退出
>>> /bye

Obsidian 插件配置

Text Generator 配置

  1. 安装 Text Generator 插件
  2. 打开设置,选择 API Provider
  3. 配置如下:
yaml
API Provider: OpenAI Compatible
Base URL: http://localhost:11434/v1
API Key: ollama  # 任意值即可
Model: llama3.2

# 高级设置
Temperature: 0.7
Max Tokens: 4096
Top P: 0.9
Frequency Penalty: 0
Presence Penalty: 0

Smart Connections 配置

实现与知识库的智能对话:

yaml
# 嵌入模型设置
Embedding Engine: Ollama
Embedding Model: nomic-embed-text
Ollama URL: http://localhost:11434

# 对话模型设置
Chat Engine: Ollama
Chat Model: llama3.2

# 检索设置
Chunk Size: 1000
Overlap: 200
Top K: 5

Copilot 插件配置

yaml
Model Provider: Ollama
Base URL: http://localhost:11434
Model: llama3.2

# 高级选项
Stream: true
Max History: 10
System Prompt: "你是一个知识管理助手..."

高级配置

GPU 加速

NVIDIA GPU

bash
# 检查 NVIDIA 驱动
nvidia-smi

# Ollama 自动检测 GPU
# 确保 CUDA 已安装

# 验证 GPU 使用
ollama run llama3.2
# 在另一个终端
nvidia-smi  # 查看 GPU 使用率

macOS (Apple Silicon)

bash
# Mac M1/M2/M3 自动使用 Metal 加速
# 无需额外配置

# 验证
ollama run llama3.2
# 查看活动监视器中的 GPU 使用

模型参数调优

bash
# 创建自定义模型配置
ollama create my-model -f Modelfile

# Modelfile 示例
FROM llama3.2

# 设置参数
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER num_ctx 4096

# 设置系统提示
SYSTEM 你是一个专业的知识管理助手,擅长总结和整理笔记。

API 调用示例

bash
# 直接调用 API
curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "请总结以下内容:Obsidian 是一款强大的知识管理工具",
  "stream": false
}'

# 对话模式
curl http://localhost:11434/api/chat -d '{
  "model": "llama3.2",
  "messages": [
    {"role": "user", "content": "你好"}
  ],
  "stream": false
}'

Templater 集成

自动调用本地 AI

创建自定义函数:

javascript
// 在 Templater 配置中添加用户脚本

async function askLocalAI(prompt, model = "llama3.2") {
  const response = await fetch("http://localhost:11434/api/generate", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      model: model,
      prompt: prompt,
      stream: false
    })
  });
  
  const data = await response.json();
  return data.response;
}

module.exports = askLocalAI;

使用模板

markdown
<%*
// 在模板中使用
const content = tp.file.selection();
if (content) {
  const prompt = `请用中文总结以下内容,不超过200字:\n\n${content}`;
  const summary = await askLocalAI(prompt);
  tR += `\n\n## AI 摘要\n${summary}`;
}
%>

实用 Prompt 模板

笔记摘要

markdown
请用中文总结以下笔记内容:

{{selection}}

要求:
1. 提取 3-5 个核心观点
2. 每个观点不超过 30 字
3. 使用列表格式
4. 保留关键数据

内容润色

markdown
请润色以下文字:

{{selection}}

要求:
1. 修正语法错误
2. 改善表达流畅度
3. 保持原意不变
4. 使用简洁的语言

知识问答

markdown
基于我的问题,请提供详细解答:

问题:{{question}}

背景:{{context}}

要求:
1. 回答准确完整
2. 如有不确定的地方请说明
3. 提供相关建议

学习卡片

markdown
将以下内容转化为学习卡片:

{{content}}

格式:
Q: 问题
A: 答案

要求:
- 每个知识点一张卡片
- 问题简洁明了
- 答案完整准确

性能优化

模型量化

bash
# Ollama 默认使用 Q4_K_M 量化
# 查看模型信息
ollama show llama3.2 --modelfile

# 不同量化级别
# Q4_K_M - 4bit 量化(推荐)
# Q5_K_M - 5bit 量化(更精确,更慢)
# Q8_0 - 8bit 量化(最精确,最大)

批量处理

javascript
// 批量处理多个笔记
const files = app.vault.getMarkdownFiles()
  .filter(f => f.path.includes("ToProcess"));

for (const file of files) {
  const content = await app.vault.read(file);
  const summary = await askLocalAI(`总结:${content.substring(0, 1000)}`);
  // 处理结果...
}

缓存策略

javascript
// 简单的结果缓存
const cache = new Map();

async function askLocalAIWithCache(prompt) {
  const hash = prompt.substring(0, 100); // 简单缓存键
  if (cache.has(hash)) {
    return cache.get(hash);
  }
  const result = await askLocalAI(prompt);
  cache.set(hash, result);
  return result;
}

常见问题排查

服务无法启动

bash
# 检查端口占用
lsof -i :11434

# 杀死占用进程
kill -9 <PID>

# 重启服务
ollama serve

模型下载失败

bash
# 检查网络连接
ping ollama.ai

# 使用代理(如需要)
export HTTP_PROXY=http://proxy:port
export HTTPS_PROXY=http://proxy:port

# 重新下载
ollama pull llama3.2

内存不足

bash
# 检查内存使用
free -h

# 使用更小的模型
ollama pull phi3  # 仅 2.2GB

# 或使用量化版本

响应速度慢

yaml
优化方案:
  1. 使用更小的模型 (phi3, llama3.2:1b)
  2. 启用 GPU 加速
  3. 减少生成长度
  4. 使用更低的量化级别

隐私与安全最佳实践

数据安全

yaml
原则:
  - 敏感数据只使用本地模型
  - 定期检查模型配置
  - 不在公网暴露 Ollama 服务
  - 使用防火墙限制访问

网络安全

bash
# 仅允许本地访问
# Ollama 默认只监听 localhost

# 如需远程访问,使用 SSH 隧道
ssh -L 11434:localhost:11434 user@server

# 或设置认证代理

进阶用法

多模型协作

markdown
<!-- 使用不同模型处理不同任务 -->

## 快速摘要
<!-- 使用小模型 -->
Model: phi3

## 深度分析
<!-- 使用大模型 -->
Model: llama3.2

## 中文处理
<!-- 使用中文优化模型 -->
Model: qwen2.5

自定义工作流

javascript
// 创建复杂的 AI 工作流
async function analyzeContent(content) {
  // 第一步:提取关键点
  const keyPoints = await askLocalAI(
    `提取关键点:${content}`,
    "phi3"
  );
  
  // 第二步:深度分析
  const analysis = await askLocalAI(
    `分析以下关键点的关联:${keyPoints}`,
    "llama3.2"
  );
  
  // 第三步:生成建议
  const suggestions = await askLocalAI(
    `基于分析提出行动建议:${analysis}`,
    "qwen2.5"
  );
  
  return { keyPoints, analysis, suggestions };
}

相关资源

最后更新:2026年3月31日编辑此页反馈问题