07 - From Local Chat to a Grounded API Response Workshop

Contents

7. 07 - From Local Chat to a Grounded API Response Workshop#

Time: 60 minutes · Audience: beginners · Updated: November 18, 2026.

7.1. Workshop preparation#

The previous local-model workshop; Python 3.10+; workshop-kit extracted into one folder.

Download the shared workshop kit and extract it into a disposable folder. Browse all workshops. Code blocks run in your local terminal, not on this web page. Record your tool versions and use synthetic inputs.

7.2. Hands-on route#

  1. Run ollama list and choose an installed chat model. Record its exact ID rather than copying a model name from an older screenshot.

  2. Run the offline fixture checks: python retrieval.py and python graph.py.

  3. In Windows PowerShell set $env:OLLAMA_MODEL="YOUR_INSTALLED_MODEL". On macOS/Linux use export OLLAMA_MODEL="YOUR_INSTALLED_MODEL". Replace the placeholder before running anything.

  4. Run python local_rag.py from the extracted kit. The first result should answer the library-opening question using S1. The second should say insufficient evidence without calling the model.

  5. Record response latency, the model ID, and whether S1 supports the answer. Change the opening time in retrieval.py and rerun to demonstrate that the document, rather than a remembered answer, supplies the fact.

The starter is deliberately a lexical baseline with native Ollama chat. The longer reference below explores embeddings, tools and serving; these are extensions, not prerequisites for this acceptance test.

7.3. Acceptance evidence#

An actual cited response, an unsupported-question refusal, and a rerun after modifying the fixture.

Create evidence.md with your environment, commands, observed outputs, failures and fixes. Label anything you did not run. Do not include secrets, screenshots of account pages, or private paths.

7.4. Troubleshooting and cleanup#

Model not found: use the exact ID from ollama list. Wrong facts: inspect retrieved text before adjusting prompts. Timeout: reduce output/model size or allow the first load to complete. Stop any demo server when finished; retain only the small fixture and evidence needed to reproduce your result.

        flowchart LR
    A["Prepare synthetic fixture"] --> B["Run guided task"]
    B --> C["Check observed result"]
    C --> D["Record evidence and limitations"]
    

Image Suggestion (Nanobanana Prompt): A clean technical workshop illustration titled “From Local Chat to a Grounded API Response”, with a neutral editor window, a small synthetic fixture, and an evidence checklist. Use legible navy and teal typography on a light background, even lighting, and a visible “Concept illustration” label. Show no account identity, private paths, or simulated success presented as a screenshot.

7.5. Reference and optional extensions#

The material below provides background. Complete the guided acceptance test above first; vendor interfaces and example model IDs can change. Consult the linked official documentation for the version you use.

Goal: Tutorial 01 introduced the basic local LLM stack. Tutorial 02 focuses on mastering Ollama itself: model management, APIs, Modelfiles, Hugging Face GGUF models, embeddings, RAG, tool calling, cloud models, troubleshooting, and safe integration into local or web applications.


7.6. What You Should Be Able to Do After This Tutorial#

By the end, you should be able to:

Skill

What you can do

Model management

Pull, run, inspect, rename, remove, stop, and list models.

Local API

Call Qwen, Llama, GLM, Gemma, or other models from Python, JavaScript, Django, FastAPI, or any HTTP client.

OpenAI compatibility

Reuse existing OpenAI SDK code by changing only base_url, api_key, and model.

Modelfile customization

Create local assistants with system prompts, context length, temperature, stop tokens, and reusable presets.

Hugging Face GGUF

Run models that are not listed in Ollama’s official model library.

RAG

Use Ollama embedding models to build a local document Q&A pipeline.

Tool calling

Let a model call validated Python functions, internal APIs, or read-only database tools.

Performance tuning

Understand context length, quantization, memory pressure, ollama ps, and model unloading.

Cloud vs local

Know when a command uses local hardware and when a :cloud model consumes cloud usage.

Application integration

Build an Ollama-powered backend for general web apps, internal tools, research assistants, or document Q&A systems.


7.7. Mental Model: What Ollama Actually Is#

Ollama is not “the model.” It is a local model runtime and management layer.

Think of the stack like this:

Your app / Terminal / Open WebUI / AnythingLLM / Django / FastAPI
        ↓
Ollama API server: http://localhost:11434
        ↓
Ollama model manager: pull, run, create, rm, ps
        ↓
Inference engine + model weights
        ↓
Your hardware: Apple Silicon / NVIDIA GPU / AMD GPU / CPU

What this means: your model does not automatically browse the web, read files, call APIs, or run code. Ollama provides the local model engine. RAG, web search, function calling, and UI features are added around it by your application or by tools such as Open WebUI and AnythingLLM.

7.7.1. Ollama vs Llama vs llama.cpp vs GGUF#

Term

Meaning

Ollama

Local runtime, model manager, CLI, and API server.

Llama

Meta’s model family, similar to how Qwen, GLM, Gemma, and Mistral are model families.

llama.cpp

Lower-level C/C++ inference engine used across many local LLM tools.

GGUF

Binary model file format optimized for local inference.

Modelfile

Ollama’s recipe file for creating customized local models.

A useful analogy:

GGUF file        = model engine block
llama.cpp        = raw engine system
Ollama           = car with dashboard, ignition, model storage, and API controls
Open WebUI       = ChatGPT-like cockpit
Your application = custom product built around the model engine

7.8. Installation and First Checks#

7.8.1. Install Ollama#

Download Ollama from:

https://ollama.com/download

Then drag it into /Applications and open it once.

Verify installation:

ollama --version

Example description: this command checks whether the Ollama CLI is available in your terminal. If macOS says command not found, reopen the terminal or restart Ollama so the CLI link can be created.

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

Example description: the first command downloads and runs Ollama’s Linux installer. The second command confirms that installation succeeded and that the ollama executable is available in your shell.

Download the installer from:

https://ollama.com/download

Then open PowerShell:

ollama --version

Example description: PowerShell should print the installed Ollama version. If it does not, close and reopen PowerShell or reinstall Ollama.

7.8.2. Verify the API server#

Ollama normally starts its local server automatically. The default local API endpoint is:

http://localhost:11434

Use the command for your operating system.

curl http://localhost:11434/api/tags

Example description: this asks the local Ollama server for the list of installed models. If you receive JSON, the API server is working. This is the fastest health check before debugging Python, JavaScript, Open WebUI, or AnythingLLM connections.

Invoke-RestMethod http://localhost:11434/api/tags

Example description: Invoke-RestMethod is PowerShell’s native HTTP client. It converts the JSON response into PowerShell objects, which makes it easier to inspect model names on Windows.

curl.exe http://localhost:11434/api/tags

Example description: Windows also has curl.exe. In CMD, use curl.exe explicitly so Windows does not confuse it with PowerShell aliases.

If the health check fails, start or restart Ollama manually.

open -a Ollama
# or, if you want to run the server directly in this terminal:
ollama serve

Example description: opening the app starts Ollama in the menu bar. ollama serve starts the API server in the foreground, which is useful when you want to see server logs while debugging.

sudo systemctl status ollama
sudo systemctl restart ollama

Example description: on most Linux installs, Ollama runs as a systemd service. status checks whether the service is running; restart reloads it after configuration or environment changes.

ollama serve

Example description: running ollama serve in PowerShell starts the local API server in the foreground. Keep that window open while testing from another terminal or application.

7.8.3. Cross-platform command convention#

Most Ollama commands are identical across macOS, Linux, and Windows:

ollama list
ollama run qwen3.6
ollama ps
ollama stop qwen3.6

The main differences are the shell syntax around HTTP calls, environment variables, virtual environments, file paths, and service management.

Task

macOS / Linux

Windows PowerShell

HTTP request

curl ...

Invoke-RestMethod ... or curl.exe ...

Environment variable

export NAME=value

$env:NAME="value"

Python launcher

python3

py or python

Activate venv

source .venv/bin/activate

.\.venv\Scripts\Activate.ps1

Home directory

~/Models

$HOME\Models

Example description: the model commands are portable, but the surrounding operating-system commands are not always portable. When copying examples, match the shell you are using.

7.8.4. Where Ollama stores models#

Model storage locations are useful when you need to estimate disk usage, back up models, or move a model cache to a larger drive.

OS

Common location

macOS

~/.ollama/models

Linux user install

~/.ollama/models

Linux service install

often /usr/share/ollama/.ollama/models

Windows

%USERPROFILE%\.ollama\models

du -sh ~/.ollama/models 2>/dev/null

Example description: this estimates how much disk space your local Ollama models consume. Large models and multiple quantizations can quickly use hundreds of gigabytes.

Get-ChildItem "$env:USERPROFILE\.ollama\models" -Recurse | Measure-Object -Property Length -Sum

Example description: this recursively sums model files in the Windows Ollama model directory. The result is shown in bytes, which you can divide by 1GB for a rough gigabyte estimate.


7.9. Core CLI Commands You Must Know#

7.9.1. Download a model#

ollama pull qwen3.6
ollama pull llama3.2
ollama pull embeddinggemma

Example description: pull downloads model weights into Ollama’s local model store. qwen3.6 and llama3.2 are chat/generation models, while embeddinggemma is an embedding model used for semantic search and RAG.

7.9.2. Run a model interactively#

ollama run qwen3.6

Example description: run loads the model and opens an interactive terminal chat. If the model is not already downloaded, Ollama usually pulls it first.

Exit the interactive session:

/bye

Example description: /bye exits the terminal chat without deleting the model. The model may stay loaded briefly for faster reuse.

7.9.3. List downloaded models#

ollama list
# or
ollama ls

Example description: this shows local model names, IDs, sizes, and modified dates. Use the exact model name from this output when calling Ollama from code.

7.9.4. Show model information#

ollama show qwen3.6
ollama show --modelfile qwen3.6

Example description: ollama show displays metadata such as architecture, parameter size, quantization, context information, and template details. --modelfile shows how Ollama packages the model internally.

7.9.5. List running models#

ollama ps

Example description: this is one of the most useful diagnostic commands. It shows currently loaded models, memory size, processor placement, context allocation, and how long the model will remain loaded.

7.9.6. Stop a running model#

ollama stop qwen3.6

Example description: this unloads a model from memory. Use it when your machine feels slow, when a different model needs memory, or before changing large context settings.

7.9.7. Remove a model#

ollama rm llama3.2

Example description: this deletes a local model from disk. It is useful because model files can easily consume tens or hundreds of gigabytes.

7.9.8. Copy or rename a model#

ollama cp qwen3.6 qwen-local
ollama run qwen-local

Example description: cp creates a local alias. This is useful when you want a stable model name for an application even if the underlying base model changes later.

OpenAI-style alias example:

ollama cp qwen3.6 gpt-4

Example description: some OpenAI-compatible tools expect a model name such as gpt-4. This alias lets those tools call model="gpt-4" while actually using your local Ollama model. Use aliases carefully so users are not confused about which model is really running.


7.10. Model Names, Tags, and Versions#

Ollama model names often follow this pattern:

model:tag

Examples:

qwen3.6
qwen3.6:35b-mlx
llama3.2
deepseek-r1:7b
glm-5.2:cloud
hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M

Example description: the part before : is the model family or repository. The part after : is the variant, such as size, quantization, MLX build, or cloud mode. If you omit the tag, Ollama often uses latest.

7.10.1. Local vs cloud#

ollama run qwen3.6

Example description: this usually runs locally. It uses your machine’s CPU/GPU/unified memory and does not consume Ollama Cloud usage.

ollama run glm-5.2:cloud

Example description: the :cloud suffix means the model runs through Ollama Cloud. It may consume cloud usage, credits, or plan limits. Use it for tasks that are too large for local hardware, not for routine private or offline work.

7.10.2. Hugging Face models#

For GGUF models on Hugging Face:

ollama run hf.co/<username>/<repo>:<quantization>

Example description: hf.co/... tells Ollama to load a GGUF model from Hugging Face instead of the official Ollama library. <quantization> selects the compressed version, such as Q4_K_M or Q5_K_M.

Concrete example:

ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M

Example description: this runs a specific Hugging Face GGUF model with a Q4 quantization. It is useful when Ollama’s official library does not yet list a new or specialized model.


7.11. Quantization: Q4, Q5, Q8, IQ2, MLX#

Quantization compresses model weights to reduce memory and disk usage. Lower precision usually means lower memory cost, but sometimes lower output quality.

Quant

Rough meaning

Good for

Q8_0

High quality, large memory

Smaller models when quality matters

Q6_K

Very good quality

Medium models on strong hardware

Q5_K_M

Balanced quality

Daily use when memory allows

Q4_K_M

Popular default

Most local use

IQ3_M

Smaller, lower quality

Large models

IQ2_M

Very compressed

Huge models or quick testing

IQ1_M

Extreme compression

Only when necessary

mlx

Apple Silicon optimized variant

Mac M-series machines when available

General rule:

Small models: Q5 or Q6 if possible
Medium models: Q4 or Q5
Huge models: IQ2 or IQ3 may be necessary
Apple Silicon: try MLX variants when available

Example description: quantization choice is a trade-off. If a model fails to load, choose a smaller model or lower quantization. If output quality is poor and memory is available, choose a higher-quality quantization.


7.12. Interactive Chat: Useful Terminal Techniques#

7.12.1. One-shot prompt#

ollama run qwen3.6 "Explain SQL joins in simple terms."

Example description: this sends one prompt and prints one answer without keeping you in a long interactive workflow. It is useful for quick tests, shell scripts, and comparing model outputs.

7.12.2. Multiline prompt#

Inside ollama run:

>>> """You are helping design a generic document management system.
... Propose database tables for documents, users, permissions, and audit logs.
... """

Example description: triple quotes make it easier to send longer instructions. The key operation is giving the model enough structured context without needing an external file.

7.12.3. Image input with a vision model#

Only works with vision-capable models.

ollama run gemma4 "Describe this image: ./sample-image.jpg"

Example description: this sends an image path to a vision-capable model. Use it for image description, visual QA, and report writing. Do not assume every text model can read images.


7.13. REST API: The Most Important Interface#

Ollama’s local API usually runs here:

http://localhost:11434

7.13.1. /api/generate#

Good for simple prompt completion.

curl http://localhost:11434/api/generate \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6",
    "prompt": "Explain RAG in 5 bullet points.",
    "stream": false
  }'

Example description: /api/generate is the simplest endpoint. model selects the local model, prompt contains the user instruction, and stream:false asks Ollama to return one complete JSON response instead of many small streaming chunks.

7.13.2. /api/chat#

Better for multi-turn chat-style messages.

curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6",
    "messages": [
      {"role": "system", "content": "You are a careful technical tutor."},
      {"role": "user", "content": "Explain the difference between SQL and NoSQL."}
    ],
    "stream": false
  }'

Example description: /api/chat separates messages by role. The system message controls behavior, while the user message contains the task. This format is better for apps because it preserves conversation structure.

7.13.3. Streaming vs non-streaming#

For easier development:

"stream": false

Example description: non-streaming is simpler because your code receives one complete JSON object. It is best for scripts, tests, and backend jobs.

For ChatGPT-like UI:

"stream": true

Example description: streaming returns partial tokens as the model generates them. It makes the UI feel faster but requires your frontend or backend to handle a stream of chunks.

7.13.4. Windows PowerShell API examples#

The REST API is the same on every operating system, but Windows users often prefer PowerShell objects instead of raw JSON strings.

$body = @{
  model = "qwen3.6"
  messages = @(
    @{ role = "user"; content = "Explain what Ollama does in two sentences." }
  )
  stream = $false
} | ConvertTo-Json -Depth 5

$response = Invoke-RestMethod `
  -Uri "http://localhost:11434/api/chat" `
  -Method Post `
  -ContentType "application/json" `
  -Body $body

$response.message.content

Example description: this is the PowerShell equivalent of the /api/chat cURL example. ConvertTo-Json -Depth 5 is important because chat messages are nested objects; without enough depth, PowerShell can truncate nested JSON.

If you prefer the OpenAI-compatible endpoint:

$body = @{
  model = "qwen3.6"
  messages = @(
    @{ role = "system"; content = "You are a concise assistant." },
    @{ role = "user"; content = "Give me a one-line definition of RAG." }
  )
} | ConvertTo-Json -Depth 5

$response = Invoke-RestMethod `
  -Uri "http://localhost:11434/v1/chat/completions" `
  -Method Post `
  -ContentType "application/json" `
  -Body $body

$response.choices[0].message.content

Example description: this uses Ollama’s OpenAI-compatible path. It is useful when your application already follows the OpenAI chat completion response structure.


7.14. Python: Native Ollama SDK#

Install:

pip install ollama

Example description: this installs the official Python client, which wraps Ollama’s local HTTP API so you do not need to write raw requests calls.

7.14.1. Basic chat#

from ollama import chat

response = chat(
    model="qwen3.6",
    messages=[
        {"role": "system", "content": "You are a helpful local AI assistant."},
        {"role": "user", "content": "Explain what GGUF is."},
    ],
)

print(response.message.content)

Example description: chat() sends structured role-based messages to the local model. The important part is model="qwen3.6": it must match a model shown by ollama list.

7.14.2. Streaming#

from ollama import chat

stream = chat(
    model="qwen3.6",
    messages=[{"role": "user", "content": "Write a short intro to Ollama."}],
    stream=True,
)

for chunk in stream:
    print(chunk.message.content, end="", flush=True)

Example description: stream=True returns tokens gradually. The loop prints each partial chunk immediately, which is how many chat interfaces create the real-time typing effect.


7.15. Python: OpenAI-Compatible SDK#

This is the most useful method if your existing code already uses the OpenAI SDK.

Install:

pip install openai

Example description: the OpenAI SDK can talk to Ollama because Ollama exposes an OpenAI-compatible /v1 API. This makes provider switching much easier.

7.15.1. Minimal example#

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key="ollama",  # any non-empty string works for local Ollama
)

response = client.chat.completions.create(
    model="qwen3.6",
    messages=[
        {"role": "system", "content": "You are a professional coding assistant."},
        {"role": "user", "content": "Write a Python function that validates an email address."},
    ],
)

print(response.choices[0].message.content)

Example description: base_url redirects the OpenAI SDK to your local Ollama server. api_key is required by the SDK, but Ollama does not require a real cloud API key for local use. model must be an installed Ollama model name.

7.15.2. Why this matters#

You can switch providers by changing only these values:

base_url = "http://localhost:11434/v1"
api_key = "ollama"
model = "qwen3.6"

Example description: this pattern prevents vendor lock-in. The same application can use local Ollama during development and a cloud or server-hosted model later by changing environment variables.


7.16. JavaScript / Node.js#

Install:

npm install openai

Example description: this installs the official OpenAI JavaScript SDK. Like the Python SDK, it can call Ollama through the OpenAI-compatible endpoint.

Example:

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "http://localhost:11434/v1",
  apiKey: "ollama",
});

const completion = await client.chat.completions.create({
  model: "qwen3.6",
  messages: [
    { role: "system", content: "You are a careful software architect." },
    { role: "user", content: "Design a REST API for a document approval workflow." },
  ],
});

console.log(completion.choices[0].message.content);

Example description: this is the Node.js equivalent of the Python OpenAI-compatible example. It is useful for Express, Next.js, Electron apps, and browser-facing backends that need to call a local or internal model server.


7.17. Modelfile: Create Your Own Local Assistant#

A Modelfile is a recipe for creating a customized Ollama model. It can define a base model, system prompt, context length, sampling parameters, and other behavior.

7.17.1. Basic generic assistant#

Create a file named Modelfile:

FROM qwen3.6

SYSTEM """
You are a local AI assistant for technical work.
Give concise, accurate, practical answers.
When information is missing, ask for the missing input or state the uncertainty clearly.
Do not invent facts, file contents, IDs, dates, or external source details.
"""

PARAMETER temperature 0.3
PARAMETER num_ctx 32768

Example description: FROM chooses the base model. SYSTEM defines default behavior. temperature 0.3 makes answers more deterministic. num_ctx 32768 increases how much text the model can consider, but also increases memory use.

Create and run the model:

ollama create local-technical-assistant -f Modelfile
ollama run local-technical-assistant

Example description: ollama create builds a named model from your recipe. After that, local-technical-assistant behaves like any other Ollama model and can be called from CLI or API.

7.17.2. Useful Modelfile instructions#

Instruction

Purpose

FROM

Base model name or local GGUF path.

SYSTEM

Default behavior, persona, constraints, and response policy.

PARAMETER

Runtime parameters such as temperature and context length.

TEMPLATE

Custom prompt template. Advanced use only.

ADAPTER

Apply LoRA / QLoRA adapter when supported.

MESSAGE

Seed conversation examples.

LICENSE

Add model license text.

REQUIRES

Minimum Ollama version.

7.17.3. View an existing model’s Modelfile#

ollama show --modelfile qwen3.6

Example description: this reveals the model’s packaging details. It is useful when you want to learn how a model formats prompts or when you want to copy a template into your own custom model.


7.18. Runtime Parameters You Should Understand#

Parameter

Meaning

Typical value

temperature

Randomness / creativity

0.1–0.4 for code, 0.7–1.0 for writing

top_p

Nucleus sampling

0.8–0.95

num_ctx

Context window length

4096, 32768, 65536, 131072

num_predict

Max generated tokens

512–4096

stop

Stop sequences

model-specific

repeat_penalty

Reduces repetition

1.05–1.2

7.18.1. Practical presets#

Coding assistant:

PARAMETER temperature 0.2
PARAMETER top_p 0.9
PARAMETER num_ctx 32768

Example description: low temperature reduces randomness and is useful for code, SQL, config files, and deterministic instructions.

Creative writing:

PARAMETER temperature 0.8
PARAMETER top_p 0.95
PARAMETER num_ctx 32768

Example description: higher temperature allows more variety. Use it for brainstorming, writing, naming, and creative drafts.

Strict RAG assistant:

PARAMETER temperature 0.1
PARAMETER top_p 0.8
PARAMETER num_ctx 65536

Example description: strict RAG should be conservative. Low randomness helps the model stay close to retrieved documents, while larger context leaves room for retrieved chunks.


7.19. Context Length and Memory#

Context length is how much text the model can “see” at once.

More context helps with:

long documents
large code files
RAG chunks
web search results
agent tool outputs
multi-turn conversations

But larger context uses more memory.

7.19.1. Check actual context allocation#

ollama ps

Example description: look at the CONTEXT column. A model may support a large maximum context, but Ollama may allocate a smaller context unless configured.

7.19.2. Increase context globally when serving#

OLLAMA_CONTEXT_LENGTH=64000 ollama serve

Example description: this starts the Ollama server with a larger default context length. It is useful for experiments but can increase memory pressure for every loaded model.

7.19.3. Increase context through Modelfile#

FROM qwen3.6
PARAMETER num_ctx 65536

Example description: setting num_ctx in a Modelfile creates a reusable model preset with a larger context window.

Then build and run:

ollama create qwen-large-context -f Modelfile
ollama run qwen-large-context

Example description: this keeps the larger-context behavior attached to the custom model name rather than requiring you to remember environment variables each time.


7.20. Toy Experiment 1 — Compare System Prompts#

Goal: see how a system prompt changes behavior.

7.20.1. Plain model#

ollama run qwen3.6 "Design a database schema for a document review system."

Example description: this uses the base model without a custom system prompt. The result will depend mostly on the model’s default behavior.

7.20.2. Custom model#

Create Modelfile:

FROM qwen3.6
SYSTEM """
You are a senior backend architect.
Always produce practical tables, fields, relationships, indexes, and implementation notes.
"""
PARAMETER temperature 0.2

Example description: this custom prompt tells the model what role to take and what output structure to prefer. temperature 0.2 makes the schema more stable and less creative.

Build and run:

ollama create backend-architect -f Modelfile
ollama run backend-architect "Design a database schema for a document review system."

Example description: now the same task goes through a customized model wrapper. Compare the outputs and note whether the custom model gives more structured database design.

Expected lesson: system prompts and temperature matter a lot even with the same base model.


7.21. Toy Experiment 2 — Build a Tiny Local RAG System#

Goal: understand RAG without a framework.

Install dependencies:

pip install ollama numpy

Example description: ollama provides access to local chat and embedding APIs. numpy is used to compute cosine similarity between vectors.

Pull an embedding model:

ollama pull embeddinggemma
# or
ollama pull qwen3-embedding

Example description: embedding models convert text into vectors. You need them for semantic search because normal chat models do not automatically index documents.

Create tiny_rag.py:

import numpy as np
import ollama

DOCS = [
    "A knowledge base stores short text chunks from uploaded documents.",
    "Embeddings convert text into vectors that can be compared by similarity.",
    "RAG retrieves relevant chunks before asking the language model to answer.",
    "A strict RAG assistant should say when the provided context is insufficient.",
]

def embed(text: str):
    result = ollama.embed(model="embeddinggemma", input=text)
    return np.array(result["embeddings"][0], dtype=np.float32)

doc_vecs = [embed(d) for d in DOCS]

def cosine(a, b):
    return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)))

question = "Why does RAG need embeddings?"
q_vec = embed(question)

scores = [(cosine(q_vec, v), doc) for v, doc in zip(doc_vecs, DOCS)]
scores.sort(reverse=True, key=lambda x: x[0])

context = "\n".join(doc for _, doc in scores[:2])

response = ollama.chat(
    model="qwen3.6",
    messages=[
        {
            "role": "system",
            "content": "Answer strictly based on the provided context. If the context is insufficient, say so.",
        },
        {
            "role": "user",
            "content": f"Context:\n{context}\n\nQuestion:\n{question}",
        },
    ],
)

print(response["message"]["content"])

Example description: this toy script demonstrates the entire RAG loop in miniature. DOCS are your fake knowledge base, embed() vectorizes text, cosine similarity retrieves relevant chunks, and the retrieved context is inserted into the model prompt.

Run:

python tiny_rag.py

Example description: the final answer should be grounded in the two most relevant chunks. If you change the question, different chunks should be retrieved.

Expected lesson: RAG is not magic. It is retrieve → insert context → ask model.


7.22. RAG Architecture for Real Projects#

For a serious local RAG stack:

Layer

Recommended options

File parsing

Docling, Marker, PyMuPDF, Unstructured, Apache Tika

Chunking

LangChain, LlamaIndex, custom splitter

Embedding

embeddinggemma, qwen3-embedding, nomic-embed-text, bge-m3

Vector DB

PostgreSQL + pgvector, Qdrant, Chroma, FAISS

LLM

Qwen, Llama, Gemma, Mistral, GLM local/cloud

App backend

Django, FastAPI, Flask, Express

UI

Open WebUI, AnythingLLM, custom React/Vue/Svelte UI

7.22.1. RAG database design example#

For PostgreSQL + pgvector:

Document
- id
- title
- source_type
- file_path
- uploaded_by
- created_at

DocumentChunk
- id
- document_id
- chunk_index
- text
- page_number
- token_count
- embedding vector
- metadata jsonb

ChatSession
- id
- user_id
- created_at

ChatMessage
- id
- session_id
- role
- content
- retrieved_chunk_ids
- created_at

Example description: Document stores file-level metadata. DocumentChunk stores searchable pieces of the document and their embeddings. ChatMessage can store which chunks were retrieved so answers can be audited later.


7.23. Toy Experiment 3 — Tool Calling#

Goal: let the model call a Python function.

Install:

pip install ollama

Example description: this installs the Ollama Python SDK, which supports chat requests with tool definitions.

Create tool_calling_demo.py:

import json
import ollama


def estimate_ticket_priority(severity: int, affected_users: int, business_impact: int) -> dict:
    """
    Estimate support ticket priority.
    severity: 1-5
    affected_users: 1-5
    business_impact: 1-5
    """
    score = severity * 0.45 + affected_users * 0.25 + business_impact * 0.30
    if score >= 4:
        level = "High"
    elif score >= 2.5:
        level = "Medium"
    else:
        level = "Low"
    return {"score": round(score, 2), "priority": level}


tools = [
    {
        "type": "function",
        "function": {
            "name": "estimate_ticket_priority",
            "description": "Estimate priority for a support ticket.",
            "parameters": {
                "type": "object",
                "properties": {
                    "severity": {"type": "integer", "description": "Issue severity from 1 to 5"},
                    "affected_users": {"type": "integer", "description": "Number/importance of affected users from 1 to 5"},
                    "business_impact": {"type": "integer", "description": "Business impact from 1 to 5"},
                },
                "required": ["severity", "affected_users", "business_impact"],
            },
        },
    }
]

messages = [
    {
        "role": "user",
        "content": "A login outage has severity 5, affected users 4, and business impact 5. Estimate its priority.",
    }
]

first = ollama.chat(
    model="qwen3.6",
    messages=messages,
    tools=tools,
)

messages.append(first["message"])

if first["message"].get("tool_calls"):
    for call in first["message"]["tool_calls"]:
        name = call["function"]["name"]
        args = call["function"]["arguments"]

        if name == "estimate_ticket_priority":
            result = estimate_ticket_priority(**args)
            messages.append({
                "role": "tool",
                "content": json.dumps(result),
            })

second = ollama.chat(
    model="qwen3.6",
    messages=messages,
)

print(second["message"]["content"])

Example description: the model does not execute Python directly. It sees the tool schema, decides whether to call the tool, and returns a tool call request. Your code validates the function name and arguments, runs the Python function, appends the tool result, and asks the model to produce the final answer.

Run:

python tool_calling_demo.py

Example description: the final response should explain the priority using the numeric result from your function. This pattern is the foundation for safe agents.

Expected lesson: the model proposes tool calls; your application executes only approved tools.


7.24. Tool Calling Pattern for Web Applications#

A real agent loop looks like this:

User asks a question
        ↓
LLM decides whether it needs a tool
        ↓
Backend validates the tool call
        ↓
Backend executes a safe function / API / DB query
        ↓
Tool result is appended to messages
        ↓
LLM writes the final answer

Useful generic tools:

Tool

What it does

search_documents(query)

Search a RAG knowledge base.

query_records(filters)

Query internal records with validated filters.

get_summary(record_id)

Summarize a selected record.

create_draft(payload)

Create a draft object for human review.

run_sql_readonly(sql)

Run only approved read-only analytics queries.

web_search(query)

Search current public web information.

read_uploaded_file(file_id)

Parse a user-uploaded file.

Security rule:

Never let the model execute arbitrary SQL, shell commands, file writes, or external API calls directly.
The model proposes; your backend validates.

Example description: tools should be narrow, typed, and permission-checked. A model should never receive unrestricted access to production systems.


7.25. Embeddings: What They Are and How Ollama Uses Them#

Embeddings turn text into numeric vectors.

They are used for:

semantic search
duplicate detection
document clustering
RAG
recommendation
similarity search

7.25.1. Generate embeddings with cURL#

curl -X POST http://localhost:11434/api/embed \
  -H "Content-Type: application/json" \
  -d '{
    "model": "embeddinggemma",
    "input": "A user reports that login fails after entering a correct password."
  }'

Example description: /api/embed returns a vector instead of a natural-language answer. You store this vector in a vector database and compare it to other vectors for semantic search.

7.25.2. Generate embeddings in Python#

import ollama

result = ollama.embed(
    model="embeddinggemma",
    input="A user reports that login fails after entering a correct password."
)

vector = result["embeddings"][0]
print(len(vector))

Example description: the printed length is the embedding dimension. Every vector in the same index must have the same dimension and come from the same embedding model.

7.25.3. Important embedding rule#

Do not mix embedding models in the same vector index.

Index documents with embeddinggemma → query with embeddinggemma
Index documents with qwen3-embedding → query with qwen3-embedding

Example description: different embedding models produce vectors in different spaces. Mixing them makes similarity search unreliable, even if the vectors look like normal arrays.


7.26. Structured Outputs / JSON Mode#

For app development, you often do not want prose. You want JSON.

7.26.1. JSON output with /api/chat#

curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6",
    "messages": [
      {
        "role": "user",
        "content": "Return JSON for a support ticket with fields: title, severity, priority, reason."
      }
    ],
    "format": "json",
    "stream": false
  }'

Example description: format:"json" asks Ollama to constrain the output to JSON. This is useful for classification, extraction, database previews, and backend workflows.

7.26.2. Practical use#

Structured output is useful for:

classification
entity extraction
feature attributes
inspection summaries
database insert previews
workflow routing

Example target shape:

{
  "ticket_id": "TICKET-00123",
  "issue_type": "login_failure",
  "severity": 4,
  "priority": "high",
  "recommended_action": "investigate authentication service"
}

Example description: the model can generate this JSON, but your backend must validate required fields, allowed enum values, data types, and permissions before storing or acting on it.


7.27. Vision Models#

Some Ollama models can accept images.

Example CLI:

ollama run gemma4 "What is visible in this image? ./sample-image.jpg"

Example description: this uses a vision-capable model to inspect an image. The text model and vision model may be different; always confirm the model supports images before relying on image input.

Example API pattern:

{
  "model": "vision-model-name",
  "messages": [
    {
      "role": "user",
      "content": "Describe the important objects in this image.",
      "images": ["BASE64_IMAGE_STRING"]
    }
  ]
}

Example description: API calls usually send images as base64 strings. This is better for web apps where the image comes from an upload rather than a local file path.

Recommended pattern:

Specialized computer vision model = precise detection or segmentation
Vision LLM = explanation, report writing, visual QA, human-readable summary

Example description: vision LLMs are strong at descriptions and reasoning, but they are not always a substitute for specialized detection models when exact measurements or high recall are required.


7.28. Hugging Face GGUF Workflow#

Ollama’s official library will not always have the newest models. Hugging Face often has GGUF versions earlier.

7.28.1. Direct run from Hugging Face#

ollama run hf.co/<username>/<repo>

Example description: this points Ollama to a Hugging Face repository. It works best when the repository contains a compatible GGUF file and a simple layout.

With quantization tag:

ollama run hf.co/<username>/<repo>:Q4_K_M

Example description: the tag after : selects a quantization. If the tag does not exist, check the repository’s file list and use the exact available quantization or filename.

Concrete example:

ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M

Example description: this runs a Hugging Face GGUF model that may not be present in the official Ollama library. It is a useful pattern for trying newer community quantizations.

7.28.2. Full filename as tag#

Sometimes this works:

ollama run hf.co/<username>/<repo>:model-name-Q4_K_M.gguf

Example description: some repositories expose multiple .gguf filenames instead of simple tags. Using the full filename can disambiguate which file Ollama should download.

7.28.3. Manual GGUF import#

Download a .gguf file, then create a Modelfile:

FROM ./model.Q4_K_M.gguf

Example description: this tells Ollama to build a local model from a GGUF file already on your machine. It is useful when direct Hugging Face pulling fails or when you want to manage files yourself.

Create model:

ollama create my-gguf-model -f Modelfile
ollama run my-gguf-model

Example description: after import, my-gguf-model becomes a normal Ollama model name. Your Python, JavaScript, and API calls can use it like any other model.

7.28.4. Sharded GGUF warning#

If files look like this:

model-00001-of-00008.gguf
model-00002-of-00008.gguf
...

that is a sharded GGUF model.

Example description: sharding means one model is split across multiple GGUF files. Some Ollama workflows may not support direct pulling of sharded GGUF repositories, depending on version and layout.

Possible solutions:

1. use llama.cpp directly
2. merge shards if supported
3. find a single-file GGUF
4. use a different quantization
5. wait for improved Ollama support

7.29. Cloud Models#

Some models are too large to run locally conveniently.

Example:

ollama run glm-5.2:cloud

Example description: :cloud tells Ollama to use cloud execution. This is not a local deployment, and it can consume account usage or plan limits.

This is not the same as:

ollama run qwen3.6

Example description: a normal local model command loads weights on your computer. It may be slower or memory-heavy, but it does not depend on cloud model usage.

Command

Runs where

Consumes local hardware

Consumes cloud usage

ollama run qwen3.6

Your machine

Yes

No

ollama run qwen3.6:35b-mlx

Apple Silicon machine

Yes

No

ollama run glm-5.2:cloud

Ollama Cloud

Minimal

Yes

ollama run hf.co/...

Usually local download/run

Yes

No, unless the model is cloud-specific

Use cloud models for:

huge long-context tasks
difficult coding tasks
occasional high-quality comparison
models too large for local memory

Use local models for:

daily coding
RAG over private documents
offline work
bulk experimentation
sensitive data


7.31. Open WebUI and AnythingLLM#

Ollama is the engine. It is not a complete ChatGPT replacement by itself.

For a ChatGPT-like local UI:

Tool

Best for

Open WebUI

ChatGPT-like UI, model management, RAG, tools, web search, users

AnythingLLM

Document Q&A, workspaces, simple RAG

LM Studio

Desktop GUI for model testing

Dify

Building LLM apps with workflows

LlamaIndex / LangChain

Python RAG/agent frameworks

A practical path:

Step 1: Ollama + a local chat model
Step 2: Open WebUI for daily local ChatGPT-like use
Step 3: AnythingLLM for document workspaces
Step 4: Custom backend integration for your own application

Example description: this separates responsibilities. Ollama runs models, Open WebUI/AnythingLLM provide user-facing workflows, and your backend handles business rules and data permissions.


7.32. Production Pattern for Django / FastAPI#

7.32.1. Minimal backend architecture#

Django/FastAPI
  ├── /api/chat
  ├── /api/rag/query
  ├── /api/documents/upload
  ├── /api/tools/query-records
  ├── /api/tools/create-draft
  └── Ollama client
          └── http://localhost:11434/v1

Example description: the application should not expose Ollama directly to every user. Your backend should validate requests, enforce permissions, manage RAG retrieval, and decide which tools are allowed.

7.32.2. Generic service wrapper#

# services/llm.py
from openai import OpenAI
from django.conf import settings

client = OpenAI(
    base_url=getattr(settings, "OLLAMA_BASE_URL", "http://localhost:11434/v1"),
    api_key=getattr(settings, "OLLAMA_API_KEY", "ollama"),
)

def ask_local_model(messages, model="qwen3.6", temperature=0.2):
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=temperature,
    )
    return response.choices[0].message.content

Example description: this wrapper hides provider details from the rest of your application. Views, tasks, or services call ask_local_model() instead of directly constructing API requests everywhere.

Usage:

answer = ask_local_model([
    {"role": "system", "content": "You are a careful internal documentation assistant."},
    {"role": "user", "content": "Summarize this technical note in 5 bullets."},
])

Example description: application code passes a standard messages list. This makes it easy to add RAG context, conversation history, or tool results before calling the model.


7.33. Performance Tuning Checklist#

7.33.1. If responses are slow#

Check:

ollama ps

Example description: look at PROCESSOR, CONTEXT, and SIZE. If a model is running mostly on CPU or using an enormous context, responses can be slow.

Possible fixes:

use a smaller model
use lower quantization
reduce num_ctx
close other apps
stop unused models
use MLX version on Apple Silicon if available

7.33.2. If you run out of memory#

Try:

ollama stop <model>

Example description: stopping a loaded model frees memory. Replace <model> with the exact model name from ollama ps.

Use a smaller model:

ollama run qwen3.6:27b-mlx

Example description: a smaller or optimized variant can be much more practical than the largest available model, especially for daily use.

Reduce context:

PARAMETER num_ctx 8192

Example description: context length is often a hidden memory cost. Reducing it can make a model usable even when the base model size stays the same.

7.33.3. If API connection fails#

Test:

curl http://localhost:11434/api/tags

Example description: this isolates the problem. If this fails, the issue is Ollama server connectivity, not your Python or JavaScript code.

Start server:

ollama serve

Example description: use this when Ollama is installed but no API server is listening.

Check port:

lsof -i :11434

Example description: this shows whether another process is using Ollama’s default port. Port conflicts can prevent the server from starting.


7.34. Security and Privacy Rules#

Local models are powerful, but your wrapper code decides safety.

7.34.1. Good rules#

Use local models for private documents.
Use read-only tools first.
Validate all tool arguments.
Whitelist database queries.
Never expose arbitrary shell access.
Log model outputs that create actions.
Use human approval for writes.

Example description: the model can generate convincing but wrong actions. Your application must enforce boundaries and require review for anything that changes data or sends information externally.

7.34.2. Bad patterns#

Letting the model run any SQL it writes.
Letting the model execute shell commands.
Letting the model send emails without confirmation.
Letting the model create/delete production records without review.
Trusting model JSON without validation.

Example description: these mistakes turn a helpful assistant into an unsafe automation system. Treat model output as untrusted until validated.


7.35. Troubleshooting Table#

Problem

Likely cause

Fix

command not found: ollama

CLI not in PATH

Restart terminal; reinstall; on macOS allow Ollama to create CLI link.

connection refused localhost:11434

Ollama server not running

Open Ollama app or run ollama serve.

Model is too slow

Too large / CPU offload / huge context

Use smaller model, MLX version, smaller num_ctx.

Out of memory

Model/context too large

Stop other models, reduce context, use lower quantization.

glm-5.2:cloud uses credit

It is a cloud model

Use local models for daily work; reserve cloud for hard tasks.

HF model says sharded GGUF unsupported

Repo contains split GGUF files

Use llama.cpp, find single-file GGUF, or different quant.

RAG answers are wrong

Bad chunking / wrong embedding / weak retrieval

Improve chunking, re-index, use better embedding, cite chunks.

API returns streaming chunks

stream default

Set "stream": false.

OpenAI SDK cannot find model

Model name mismatch

Run ollama list; use exact model name.

Context too small

Default context allocation

Use PARAMETER num_ctx or OLLAMA_CONTEXT_LENGTH.



7.37. End-to-End Workflow for a Local AI Stack#

        graph TD
    A["Install Ollama"] --> B["Pull a local chat model"]
    B --> C["Test in terminal"]
    C --> D["Call from Python with OpenAI-compatible SDK"]
    D --> E["Create custom Modelfile"]
    E --> F["Add embedding model"]
    F --> G["Build tiny RAG"]
    G --> H["Store chunks in pgvector / Qdrant / Chroma"]
    H --> I["Add tool calling"]
    I --> J["Connect backend app"]
    J --> K["Add Open WebUI or custom UI"]
    K --> L["Optional: compare with cloud models"]
    

Example description: this workflow shows how to grow from one local command into a full application stack. Each step adds a capability: model access, customization, private knowledge, tools, backend integration, and UI.


7.38. Mini Project — Generic Local Documentation Assistant#

7.38.1. Goal#

Build a local assistant that can:

1. answer questions about uploaded documents
2. summarize long technical notes
3. classify support tickets or internal requests
4. generate draft responses for human review
5. call safe read-only backend tools

Example description: this project is intentionally generic. It teaches the same building blocks needed for many private AI assistants without depending on any specific organization or domain.

7.38.2. Suggested stack#

LLM: qwen3.6, Llama, Gemma, Mistral, or another local model
Embedding: embeddinggemma, qwen3-embedding, nomic-embed-text, or bge-m3
Backend: Django, FastAPI, Flask, or Express
Database: PostgreSQL + pgvector, Qdrant, Chroma, or FAISS
RAG: uploaded documents, markdown notes, PDFs, internal manuals
Tools: search_documents, query_records, create_draft, summarize_file
UI: Open WebUI for testing, custom web UI later

Example description: this stack separates the chat model, embedding model, database, backend, tools, and UI. That separation makes the system easier to debug and replace piece by piece.

7.38.3. Example system prompt#

You are a local documentation assistant.
Answer using provided documents and retrieved context.
If the evidence is missing, say what is missing instead of guessing.
Never invent document titles, record IDs, dates, or approval status.
For actions that change data, create a draft and ask for human confirmation.

Example description: this system prompt is conservative on purpose. It encourages document-grounded answers and prevents the assistant from pretending to know facts that were not retrieved.


7.39. Knowledge Quiz#

7.39.1. Q1. What command shows downloaded local models?#

Answer
ollama list

or:

ollama ls

7.39.2. Q2. What command shows currently running models and context length?#

Answer
ollama ps

7.39.3. Q3. What does glm-5.2:cloud mean?#

Answer

It means the model runs through Ollama Cloud, not fully on your local machine. It may consume Ollama Cloud usage, credits, or plan limits.

7.39.4. Q4. What is the easiest way to reuse existing OpenAI SDK code with Ollama?#

Answer

Set:

client = OpenAI(
    base_url="http://localhost:11434/v1",
    api_key="ollama",
)

Then use your local model name, such as:

model="qwen3.6"

7.39.5. Q5. What is the basic RAG pipeline?#

Answer
documents → chunks → embeddings → vector database → retrieve relevant chunks → send context to LLM → answer with sources

7.39.6. Q6. Why should you not mix embedding models in one vector index?#

Answer

Different embedding models produce vectors in different vector spaces. If documents are embedded with one model and questions with another, similarity search becomes unreliable.

7.39.7. Q7. What is a Modelfile?#

Answer

A Modelfile is Ollama’s recipe for creating a customized model from a base model or local model file. It can define FROM, SYSTEM, PARAMETER, TEMPLATE, ADAPTER, and other instructions.


7.40. Appendix A — Cross-Platform Examples: macOS, Windows, and Linux#

This section collects equivalent commands for the most common operations. Use it as a quick translation table when an example appears to be written for a different operating system.

7.40.1. A.1 Health check and model list#

ollama list
curl http://localhost:11434/api/tags

Example description: ollama list checks the local model registry from the CLI. curl /api/tags checks the same idea through the HTTP API, which is what applications use.

ollama list
Invoke-RestMethod http://localhost:11434/api/tags

Example description: the first command confirms the CLI works. The second confirms the local API works and returns the installed models as PowerShell objects.

ollama list
curl.exe http://localhost:11434/api/tags

Example description: CMD can use curl.exe for the API health check. This is useful on machines where PowerShell execution policies are restricted.

7.40.2. A.2 Run the same model on each OS#

ollama run qwen3.6

Example description: this loads the model and starts an interactive terminal chat. If the model is missing, Ollama downloads it first.

ollama run qwen3.6

Example description: Ollama’s model commands are the same on Windows. The difference usually appears only when you write JSON, paths, or environment variables.

7.40.3. A.3 Python virtual environment setup#

python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -U ollama openai numpy

Example description: this creates an isolated Python environment, activates it, and installs the Ollama SDK, OpenAI SDK, and NumPy for simple RAG experiments.

py -m venv .venv
.\.venv\Scripts\Activate.ps1
py -m pip install -U ollama openai numpy

Example description: Windows virtual environments use a different activation script. If activation is blocked, run PowerShell as the current user and set a less restrictive execution policy for the session.

py -m venv .venv
.venv\Scripts\activate.bat
py -m pip install -U ollama openai numpy

Example description: CMD uses activate.bat instead of the PowerShell activation script.

7.40.4. A.4 Run toy scripts#

python3 tiny_rag.py
python3 tool_calling_demo.py

Example description: these commands run the tutorial’s toy RAG and tool-calling scripts with the active Python environment.

py tiny_rag.py
py tool_calling_demo.py

Example description: py is the Windows Python launcher. It helps select the installed Python version consistently.

7.40.5. A.5 Environment variables#

export OLLAMA_HOST=127.0.0.1:11434
export OLLAMA_KEEP_ALIVE=5m
export OLLAMA_CONTEXT_LENGTH=8192
ollama serve

Example description: these variables affect the Ollama server launched from the same shell. They are temporary and disappear when the terminal closes.

$env:OLLAMA_HOST="127.0.0.1:11434"
$env:OLLAMA_KEEP_ALIVE="5m"
$env:OLLAMA_CONTEXT_LENGTH="8192"
ollama serve

Example description: $env: sets variables for the current PowerShell session. Start ollama serve from that same window so the server receives the settings.

sudo systemctl edit ollama

Then add:

[Service]
Environment="OLLAMA_KEEP_ALIVE=5m"
Environment="OLLAMA_CONTEXT_LENGTH=8192"

Reload and restart:

sudo systemctl daemon-reload
sudo systemctl restart ollama

Example description: Linux service installs do not automatically inherit shell variables. A systemd override is the correct place for persistent server-level settings.

7.40.6. A.6 API calls: cURL vs PowerShell#

curl http://localhost:11434/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen3.6",
    "messages": [
      {"role": "user", "content": "Explain local LLMs in one paragraph."}
    ],
    "stream": false
  }'

Example description: this sends a complete chat request to the local Ollama API. stream:false makes the response easier to read in scripts because Ollama returns one final JSON object.

$body = @{
  model = "qwen3.6"
  messages = @(
    @{ role = "user"; content = "Explain local LLMs in one paragraph." }
  )
  stream = $false
} | ConvertTo-Json -Depth 5

Invoke-RestMethod `
  -Uri "http://localhost:11434/api/chat" `
  -Method Post `
  -ContentType "application/json" `
  -Body $body

Example description: PowerShell objects are safer than manually escaping a long JSON string. ConvertTo-Json turns the hashtable into the JSON body expected by Ollama.

7.40.7. A.7 Check which process owns port 11434#

lsof -i :11434

Example description: this checks whether Ollama or another process is listening on the default API port.

ss -ltnp | grep 11434

Example description: ss is the modern Linux tool for checking listening TCP ports. It helps diagnose connection refused errors.

Get-NetTCPConnection -LocalPort 11434

Example description: this shows Windows TCP listeners using port 11434. If another process owns the port, stop it or change Ollama’s host/port.

7.40.8. A.8 Import a local GGUF file#

mkdir -p ~/Models/demo-gguf
cd ~/Models/demo-gguf
# Put model.Q4_K_M.gguf in this folder first.
cat > Modelfile <<'EOF'
FROM ./model.Q4_K_M.gguf
PARAMETER temperature 0.3
EOF
ollama create demo-local -f Modelfile
ollama run demo-local

Example description: the Modelfile tells Ollama which local GGUF file to wrap as a runnable model. ollama create registers it under the name demo-local.

New-Item -ItemType Directory -Force -Path "$HOME\Models\demo-gguf"
Set-Location "$HOME\Models\demo-gguf"
# Put model.Q4_K_M.gguf in this folder first.
@"
FROM ./model.Q4_K_M.gguf
PARAMETER temperature 0.3
"@ | Set-Content Modelfile
ollama create demo-local -f Modelfile
ollama run demo-local

Example description: PowerShell’s here-string creates the Modelfile. Forward slashes inside FROM ./model... are fine because Ollama reads the path relative to the Modelfile.

7.40.9. A.9 Open WebUI with Docker#

docker run -d \
  -p 3000:8080 \
  --add-host=host.docker.internal:host-gateway \
  -v open-webui:/app/backend/data \
  --name open-webui \
  --restart always \
  ghcr.io/open-webui/open-webui:main

Example description: this runs Open WebUI and points containers to the host machine so the web UI can reach Ollama at host.docker.internal:11434.

docker run -d `
  -p 3000:8080 `
  --add-host=host.docker.internal:host-gateway `
  -v open-webui:/app/backend/data `
  --name open-webui `
  --restart always `
  ghcr.io/open-webui/open-webui:main

Example description: this is the PowerShell line-continuation version of the Docker command. The backtick character continues the command on the next line.

7.40.10. A.10 Common path translations#

Concept

macOS / Linux

Windows PowerShell

Home folder

~

$HOME

Models folder example

~/Models

$HOME\Models

Current directory

./file.gguf

.\file.gguf

Activate venv

source .venv/bin/activate

.\.venv\Scripts\Activate.ps1

Delete model file/folder manually

rm -rf path

Remove-Item -Recurse -Force path

Example description: most Ollama commands are OS-neutral, but file paths are not. When an example fails, check path syntax before assuming the model or API is broken.


7.41. Final Practical Advice#

A reliable learning order is:

1. Use a local chat model through Ollama.
2. Learn the CLI: list, run, show, ps, stop, rm.
3. Use the OpenAI-compatible SDK in Python or JavaScript.
4. Create a custom Modelfile.
5. Add an embedding model.
6. Build a tiny RAG system.
7. Add tool calling for safe backend actions.
8. Use Open WebUI for daily local ChatGPT-like use.
9. Use cloud models only for difficult tasks or comparison.
10. Use Hugging Face GGUF when Ollama's official library lacks a model.

The big idea:

Ollama gives you the local model engine.
RAG gives it your knowledge.
Tools give it controlled actions.
Your backend gives it rules.
Your UI gives it a useful product experience.