The Free Encyclopedia

AI Tips and Tricks with Ollama

Revision as of Jun 25, 2026 22:26 by migration. This is an old revision — view current.

Quick Start Guide

Installation & Setup

Get Ollama running on your system in minutes.

# Linux/macOS
curl -fsSL https://ollama.ai/install.sh | sh
# Windows — Download from ollama.ai and run installer

First Model Download

Start with a lightweight, versatile model.

# Terminal
ollama pull llama2:7b

This downloads the 7B parameter Llama 2 model (approx. 3.8GB).

Test Your Setup

Verify everything works correctly.

# Terminal
ollama run llama2:7b "Hello, world!"

Smart Model Management

List Available Models

ollama list

Remove Unused Models

ollama rm model-name:tag

Update Models

ollama pull model-name:tag
Model Size Description
Llama 2 7B 3.8GB Great balance of performance and resource usage. Ideal for general tasks and development.
Code Llama 7B 3.8GB Specialized for code generation, debugging, and programming assistance.
Mistral 7B 4.1GB High-quality responses with excellent instruction following capabilities.
Neural Chat 7B 4.1GB Optimized for conversational AI and chat applications.

Performance Optimization

GPU Acceleration

Ollama automatically detects and uses GPU acceleration when available. Ensure you have CUDA (NVIDIA) or Metal (Apple) support.

# Check GPU usage
nvidia-smi # For NVIDIA
activity monitor # For Apple Silicon

Memory Management

Set memory limits to prevent system overload:

# Set in environment
export OLLAMA_MAX_MEMORY=8GB

Concurrent Requests

Configure parallel request handling:

# Set max concurrent requests
export OLLAMA_MAX_QUEUE=10

Model Loading

Keep frequently used models in memory:

ollama run llama2:7b --keep-alive 30m
WARNING Resource Requirements: 7B models require at least 8GB RAM, 13B models need 16GB+, and 70B models require 64GB+ RAM for optimal performance.

API Integration Tips

REST API Examples

Basic Chat Completion

# cURL
curl http://localhost:11434/api/generate \
  -d '{
    "model": "llama2:7b",
    "prompt": "Why is the sky blue?",
    "stream": false
  }'

Python Integration

import requests
import json

response = requests.post('http://localhost:11434/api/generate',
    json={
        'model': 'llama2:7b',
        'prompt': 'Explain quantum computing',
        'stream': False
    })

result = response.json()
print(result['response'])

Streaming Responses

const response = await fetch('http://localhost:11434/api/generate', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    model: 'llama2:7b',
    prompt: 'Write a story about AI',
    stream: true
  })
});

const reader = response.body.getReader();
while (true) {
  const { done, value } = await reader.read();
  if (done) break;

  const chunk = new TextDecoder().decode(value);
  const data = JSON.parse(chunk);
  console.log(data.response);
}

Advanced Techniques

Custom Model Files

Create specialized models with custom parameters.

# Modelfile
FROM llama2:7b
PARAMETER temperature 0.8
PARAMETER top_p 0.9
SYSTEM "You are a helpful coding assistant."
# Build Model
ollama create my-coding-assistant -f ./Modelfile

Prompt Engineering

Optimize prompts for better responses.

  • Be specific and clear in instructions
  • Use examples for complex tasks
  • Break down complex requests
  • Specify output format when needed

Context Window Management

Efficiently handle long conversations.

# Limit context length
ollama run llama2:7b \
  --context-length 2048 \
  "Your prompt here"

Parameter Tuning Guide

Parameter Range Default Effect
temperature 0.0 - 2.0 0.8 Controls randomness (0=deterministic, 2=very creative)
top_p 0.1 - 1.0 0.9 Nucleus sampling threshold
top_k 1 - 100 40 Limits vocabulary to top K tokens
repeat_penalty 0.0 - 2.0 1.1 Reduces repetition (1.0=no penalty)
num_predict 1 - 2048 128 Maximum tokens to generate

Troubleshooting Common Issues

Out of Memory Errors

  • Use smaller models (7B instead of 13B)
  • Reduce context length
  • Close other memory-intensive applications
  • Set OLLAMA_MAX_MEMORY environment variable

Slow Response Times

  • Check GPU utilization
  • Use quantized models (Q4, Q5)
  • Keep models loaded with --keep-alive
  • Reduce num_predict parameter

Connection Issues

  • Verify Ollama service is running
  • Check port 11434 is accessible
  • Restart Ollama service
  • Check firewall settings