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.
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.
| Endpoint | Use |
|---|---|
POST /api/chat | Chat with messages (roles: system, user, assistant, tool) |
POST /api/generate | Single prompt → completion |
POST /api/embed | Create embeddings for RAG |
GET /api/tags | List local models |
/v1/chat/completions etc. | OpenAI-compatible endpoints |
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
}'
# 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)
// 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);
# 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.
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.
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.
"""
ollama create barefoot-assistant -f Modelfile
ollama run barefoot-assistant "Draft a project kickoff email for Kapé Iloilo"
| Instruction | Purpose |
|---|---|
FROM | Base model (from the library, or a local GGUF file path) |
PARAMETER | Defaults like temperature, num_ctx (context), stop |
SYSTEM | The built-in system prompt |
MESSAGE | Seed example conversation turns (few-shot) |
ADAPTER | Apply 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 / 3Q1Which base URL makes OpenAI-SDK tools use Ollama?
Ollama's OpenAI-compatible API lives under /v1.
Q2How do you force schema-valid JSON from Ollama?
Structured outputs constrain generation to the schema.
Q3In a Modelfile, what does SYSTEM set?
SYSTEM defines the default system prompt of the custom model.