Ollama (本地模型)

在本地运行 LLM,完全保护隐私——无需 API 密钥、无需互联网、无需成本。

概述

┌─────────────────────────────────────────────────────────────────────┐
│                         Ollama Local Setup                          │
├─────────────────────────────────────────────────────────────────────┤
│                                                                     │
│   Your Machine                                                      │
│   ┌─────────────────────────────────────────────────────────────┐  │
│   │                                                             │  │
│   │   ┌──────────────┐      ┌──────────────┐                   │  │
│   │   │  ADK-Rust    │ ───▶ │   Ollama     │                   │  │
│   │   │  Agent       │      │   Server     │                   │  │
│   │   └──────────────┘      └──────┬───────┘                   │  │
│   │                                │                            │  │
│   │                         ┌──────▼───────┐                   │  │
│   │                         │  Local LLM   │                   │  │
│   │                         │  (llama3.2)  │                   │  │
│   │                         └──────────────┘                   │  │
│   │                                                             │  │
│   └─────────────────────────────────────────────────────────────┘  │
│                                                                     │
│   🔒 100% Private - Data never leaves your machine                 │
│                                                                     │
└─────────────────────────────────────────────────────────────────────┘

为什么选择 Ollama?

优点描述
🆓 免费永远没有 API 费用
🔒 私密数据保留在您的机器上
📴 离线无需互联网即可工作
🎛️ 控制选择任何模型,自定义设置
快速无网络延迟

步骤 1:安装 Ollama

macOS

brew install ollama

Linux

curl -fsSL https://ollama.com/install.sh | sh

Windows

ollama.com 下载


步骤 2:启动服务器

ollama serve

您应该会看到:

Couldn't find '/Users/you/.ollama/id_ed25519'. Generating new private key.
Your new public key is: ssh-ed25519 AAAA...
time=2024-01-05T12:00:00.000Z level=INFO source=server.go msg="Listening on 127.0.0.1:11434"

步骤 3:拉取模型

在新终端中:

# 推荐的入门模型(3B 参数,速度快)
ollama pull llama3.2

# 其他流行模型
ollama pull qwen2.5:7b    # 优秀的工具调用能力
ollama pull mistral       # 适用于代码
ollama pull codellama     # 代码生成
ollama pull gemma2        # Google 的高效模型

步骤 4:添加到您的项目

[dependencies]
adk-model = { version = "0.2", features = ["ollama"] }

步骤 5:在代码中使用

use adk_model::ollama::{OllamaModel, OllamaConfig};
use adk_agent::LlmAgentBuilder;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // 无需 API 密钥!
    let model = OllamaModel::new(OllamaConfig::new("llama3.2"))?;

    let agent = LlmAgentBuilder::new("local_assistant")
        .instruction("You are a helpful assistant running locally.")
        .model(Arc::new(model))
        .build()?;

    // 使用 Agent...
    Ok(())
}

完整的工作示例

use adk_rust::prelude::*;
use adk_rust::Launcher;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    // 无需 API 密钥!
    let model = OllamaModel::new(OllamaConfig::new("llama3.2"))?;

    let agent = LlmAgentBuilder::new("ollama_assistant")
        .description("Ollama-powered local assistant")
        .instruction("You are a helpful assistant running locally via Ollama. Be concise.")
        .model(Arc::new(model))
        .build()?;

    // 运行交互式会话
    Launcher::new(Arc::new(agent)).run().await?;
    
    Ok(())
}

Cargo.toml

[dependencies]
adk-rust = { version = "0.2", features = ["cli", "ollama"] }
tokio = { version = "1", features = ["full"] }
dotenvy = "0.15"
anyhow = "1.0"

配置选项

use adk_model::ollama::{OllamaModel, OllamaConfig};

let config = OllamaConfig::new("llama3.2")
    .with_base_url("http://localhost:11434")  // 自定义服务器 URL
    .with_temperature(0.7)                     // 创造力 (0.0-1.0)
    .with_max_tokens(2048);                    // 最大响应长度

let model = OllamaModel::new(config)?;

推荐模型

模型大小所需内存最适合
llama3.23B4GB快速,通用
llama3.2:7b7B8GB更好的质量
qwen2.5:7b7B8GB最佳工具调用
mistral7B8GB代码和推理
codellama7B8GB代码生成
gemma29B10GB均衡性能
llama3.1:70b70B48GB最高质量

选择模型

  • 内存有限 (8GB)?llama3.2 (3B)
  • 需要工具调用?qwen2.5:7b
  • 编写代码?codellamamistral
  • 最佳质量?llama3.1:70b (需要 48GB+ 内存)

使用 Ollama 进行工具调用

Ollama 支持兼容模型的函数调用:

use adk_model::ollama::{OllamaModel, OllamaConfig};
use adk_agent::LlmAgentBuilder;
use adk_tool::FunctionTool;
use std::sync::Arc;

// qwen2.5 has excellent tool calling support
let model = OllamaModel::new(OllamaConfig::new("qwen2.5:7b"))?;

let weather_tool = Arc::new(FunctionTool::new(
    "get_weather",
    "Get weather for a location",
    |_ctx, args| async move {
        let location = args.get("location").and_then(|v| v.as_str()).unwrap_or("unknown");
        Ok(serde_json::json!({
            "location": location,
            "temperature": "72°F",
            "condition": "Sunny"
        }))
    },
));

let agent = LlmAgentBuilder::new("weather_assistant")
    .instruction("Help users check the weather.")
    .model(Arc::new(model))
    .tool(weather_tool)
    .build()?;

注意:工具调用使用非流式模式,以确保本地模型的可靠性。


示例输出

👤 用户: 你好!你能做什么?

🤖 Ollama (llama3.2): 你好!我是一个运行在你本地机器上的 AI 助手。我可以帮助你:
- 回答问题
- 编写和编辑文本
- 解释概念
- 提供基础编码帮助

所有操作都完全私密——没有任何内容会离开你的电脑!

故障排除

"连接被拒绝"

# 确保 Ollama 正在运行
ollama serve

"模型未找到"

# 首先拉取模型
ollama pull llama3.2

响应缓慢

  • 使用更小的模型(例如 llama3.2 而不是 llama3.1:70b
  • 关闭其他应用程序以释放 RAM
  • 如果可用,请考虑 GPU 加速

检查可用模型

ollama list

运行示例

# 在 official_docs_examples 文件夹中
cd official_docs_examples/models/providers_test
cargo run --bin ollama_example

相关


上一个: ← 模型提供商 | 下一个: 本地模型 (mistral.rs) →