Ollama (Lokale Modelle)

Führen Sie LLMs lokal mit vollständiger Privatsphäre aus – keine API-Schlüssel, kein Internet, keine Kosten.

Übersicht

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

Warum Ollama?

VorteilBeschreibung
🆓 KostenlosKeine API-Kosten, jemals
🔒 PrivatDaten bleiben auf Ihrem Rechner
📴 OfflineFunktioniert ohne Internet
🎛️ KontrolleWählen Sie ein beliebiges Modell, passen Sie Einstellungen an
SchnellKeine Netzwerklatenz

Schritt 1: Ollama installieren

macOS

brew install ollama

Linux

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

Windows

Herunterladen von ollama.com


Schritt 2: Den Server starten

ollama serve

Sie sollten sehen:

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"

Schritt 3: Ein Modell herunterladen

In einem neuen Terminal:

# Recommended starter model (3B parameters, fast)
ollama pull llama3.2:3b

# Other popular models
ollama pull qwen3.6:35b-a3b   # Latest Qwen — 73.4% SWE-bench, MoE 35B/3B active
ollama pull qwen3.5            # Strong multilingual and coding
ollama pull qwen3-coder:30b    # Code-focused with tool calling
ollama pull mistral:7b        # Good for code
ollama pull deepseek-r1:14b   # Reasoning model
ollama pull devstral:24b      # Optimized for coding
ollama pull gemma3:9b         # Google's efficient model
ollama pull codellama:13b     # Code generation

Schritt 4: Zu Ihrem Projekt hinzufügen

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

Schritt 5: Im Code verwenden

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

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // No API key needed!
    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()?;

    // Use the agent...
    Ok(())
}

Vollständiges funktionierendes Beispiel

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

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    dotenvy::dotenv().ok();
    
    // No API key needed!
    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()?;

    // Run interactive session
    Launcher::new(Arc::new(agent)).run().await?;
    
    Ok(())
}

Cargo.toml

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

Konfigurationsoptionen

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

let config = OllamaConfig::new("llama3.2")
    .with_base_url("http://localhost:11434")  // Custom server URL
    .with_temperature(0.7)                     // Creativity (0.0-1.0)
    .with_max_tokens(2048);                    // Max response length

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

ModellGrößeBenötigter RAMAm besten geeignet für
llama3.2:3b3B4GBSchnell, Allzweck
llama3.1:8b8B8GBBeliebtes, ausgewogenes Modell
qwen3.6:35b-a3b35B (3B active)24GBBeste agentische Codierung — 73.4% SWE-bench
qwen3.5variesvariesStark mehrsprachig und Codierung
qwen3-coder:30b30B19GBCode-fokussiert mit Tool-Aufruf
qwen2.5:7b7B8GBGuter Tool-Aufruf, leichtgewichtig
mistral:7b7B8GBCode und Argumentation
mistral-nemo:12b12B12GBVerbessertes Mistral (128K Kontext)
deepseek-r1:14b14B16GBDestillierte Argumentation
deepseek-r1:32b32B32GBGrößeres Argumentationsmodell
gemma3:9b9B10GBGoogles effizientes offenes Modell
devstral:24b24B24GBOptimiert für Codierung
codellama:13b13B16GBCodegenerierung
llama3.3:70b70B48GBHöchste Qualität

Modell auswählen

  • Begrenzter RAM (8GB)?llama3.2:3b
  • Beste agentische Codierung?qwen3.6:35b-a3b (24GB, MoE — only 3B active)
  • Benötigen Sie Tool Calling?qwen3.5 oder qwen3-coder:30b
  • Code schreiben?devstral:24b oder qwen3-coder:30b
  • Benötigen Sie Argumentation?deepseek-r1:14b oder deepseek-r1:32b
  • Beste Qualität?llama3.3:70b (benötigt 48GB+ RAM)

Tool Calling mit Ollama

Ollama unterstützt Funktion calling mit kompatiblen Modellen:

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()?;

Hinweis: Tool calling verwendet den Nicht-Streaming-Modus für Zuverlässigkeit mit lokalen Modellen.


Beispielausgabe

👤 User: Hello! What can you do?

🤖 Ollama (llama3.2): Hello! I'm a local AI assistant running on your 
machine. I can help with:
- Answering questions
- Writing and editing text
- Explaining concepts
- Basic coding help

All completely private - nothing leaves your computer!

Fehlerbehebung

"Verbindung verweigert"

# Make sure Ollama is running
ollama serve

"Modell nicht gefunden"

# Pull the model first
ollama pull llama3.2

Langsame Antworten

  • Verwenden Sie ein kleineres Modell (llama3.2 anstelle von llama3.1:70b)
  • Schließen Sie andere Anwendungen, um RAM freizugeben
  • Ziehen Sie eine GPU-Beschleunigung in Betracht, falls verfügbar

Verfügbare Modelle prüfen

ollama list

Beispiele ausführen

cargo check -p adk-rust --no-default-features --features ollama

Führen Sie ollama serve aus und laden Sie das Modell, bevor Sie einen Agent starten, der diesen Anbieter verwendet.



Zurück: ← Model Providers | Weiter: Local Models (mistral.rs) →

Ollama (Lokale Modelle) - ADK-Rust Dokumentation | ADK-Rust