Barefoot AI HubTHE BAREFOOT FREELANCER
Start learning
▣ Local AI · Lesson 3 of 4

17Ollama API, Modelfiles & Integrations

Call local models from code, use the OpenAI-compatible endpoint so existing tools just work, get structured JSON, and package custom assistants with Modelfiles.

⏱ 25 min📶 Intermediate🧪 3-question check

What you'll learn

  • Call the Ollama REST API with curl, Python and JavaScript
  • Use the OpenAI-compatible endpoint with existing SDKs and tools
  • Get schema-valid JSON from a local model
  • Create custom models with a Modelfile

The local API

While Ollama runs, it serves an HTTP API at http://localhost:11434. Everything — n8n, Open WebUI, Hermes, your scripts — talks to it.

EndpointUse
POST /api/chatChat with messages (roles: system, user, assistant, tool)
POST /api/generateSingle prompt → completion
POST /api/embedCreate embeddings for RAG
GET /api/tagsList local models
/v1/chat/completions etc.OpenAI-compatible endpoints
terminalbash
curl http://localhost:11434/api/chat -d '{
  "model": "qwen3:8b",
  "messages": [
    {"role": "system", "content": "You write short, upbeat marketing copy."},
    {"role": "user", "content": "3 taglines for a surf school in Siargao"}
  ],
  "stream": false
}'
chat.pypython
# pip install ollama
from ollama import chat

response = chat(
    model="qwen3:8b",
    messages=[{"role": "user", "content": "Summarize in 3 bullets: ..."}],
    options={"temperature": 0.2},
)
print(response.message.content)

# streaming
for part in chat(model="qwen3:8b", messages=[{"role": "user", "content": "Tell a short story"}], stream=True):
    print(part.message.content, end="", flush=True)
chat.mjsjavascript
// npm i ollama
import ollama from "ollama";

const response = await ollama.chat({
  model: "qwen3:8b",
  messages: [{ role: "user", content: "Write a 2-line description for handmade abaca bags" }],
});
console.log(response.message.content);
openai_compat.pypython
# pip install openai — any OpenAI-SDK tool can point at Ollama
from openai import OpenAI

client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")  # key is required but ignored
r = client.chat.completions.create(
    model="qwen3:8b",
    messages=[{"role": "user", "content": "Hello from my laptop!"}],
)
print(r.choices[0].message.content)

This is why Ollama plugs into so much software: anywhere that accepts an “OpenAI-compatible base URL”, enter http://localhost:11434/v1.

Structured outputs: guaranteed JSON

Pass a JSON schema in the format field and Ollama constrains the model to produce matching JSON — perfect for extraction pipelines.

extract_receipt.pypython
from ollama import chat
from pydantic import BaseModel

class Receipt(BaseModel):
    vendor: str
    date: str
    total_php: float
    vat_php: float | None
    category: str

text = """ISLAND SUPPLY CO. Iloilo City  2026-09-21  Inv IS-88213
Printer ink x2, Bond paper x5   VAT 312.00   TOTAL 2,912.00"""

r = chat(
    model="qwen3:8b",
    messages=[{"role": "user", "content": f"Extract the receipt data. Category is one of: Office supplies, Software, Meals, Travel, Other.\n\n{text}"}],
    format=Receipt.model_json_schema(),
    options={"temperature": 0},
)
receipt = Receipt.model_validate_json(r.message.content)
print(receipt)

Modelfiles: package your own assistant

A Modelfile bundles a base model with a system prompt and parameters under a new name — like a custom GPT that runs locally.

ModelfileModelfile
FROM qwen3:8b

PARAMETER temperature 0.3
PARAMETER num_ctx 16384

SYSTEM """
You are the Barefoot Freelancer assistant for a Philippine-based web designer.
You write clear, friendly client emails, proposals and project updates.
- Default to concise answers with bullets where helpful.
- Prices are in PHP unless stated. Never invent prices or dates — ask.
- Client-facing text: warm and professional English.
"""
terminalbash
ollama create barefoot-assistant -f Modelfile
ollama run barefoot-assistant "Draft a project kickoff email for Kapé Iloilo"
InstructionPurpose
FROMBase model (from the library, or a local GGUF file path)
PARAMETERDefaults like temperature, num_ctx (context), stop
SYSTEMThe built-in system prompt
MESSAGESeed example conversation turns (few-shot)
ADAPTERApply a fine-tuned LoRA adapter

Where to plug in your local models

⚙️

n8n

Ollama Chat Model and Embeddings nodes — local AI inside workflows (Lesson 14).

☤

Hermes Agent

Custom endpoint http://localhost:11434/v1 (Lesson 12).

💬

Open WebUI

A full ChatGPT-style interface for your team (next lesson).

🧑‍💻

Aider / VS Code

Local coding assistants via the OpenAI-compatible API.

Key takeaways

  • Ollama's API on port 11434 powers every integration; /v1 makes it OpenAI-compatible.
  • Pass a JSON schema via `format` for guaranteed structured output.
  • Modelfiles package a base model + system prompt + parameters into a named assistant.
  • One local API can serve n8n, Hermes, Open WebUI, coding tools and your own scripts.

Knowledge check

0 / 3

Q1Which base URL makes OpenAI-SDK tools use Ollama?

Q2How do you force schema-valid JSON from Ollama?

Q3In a Modelfile, what does SYSTEM set?

Finished this lesson?Your progress is saved in this browser.