Sub topic 2
Managing Ollama Models and Calling Its API
This chapter assumes you can use a terminal and understand basic HTTP and JSON. It also assumes Ollama is installed and its local server is running.
Pulling, listing, inspecting, and removing models
pull downloads a model from the Ollama library. The tag after the model name selects a version or size.
ollama pull llama3.2
ollama list
ollama show llama3.2
ollama ps
ollama rm llama3.2listshows models stored on disk.showdisplays a model's template, parameters, and metadata.psshows models currently loaded in memory.rmdeletes a local model. You can pull it again later.
run downloads a missing model and starts an interactive chat:
ollama run llama3.2Picking a model that fits your hardware
A model's parameter count measures the number of learned values it contains. More parameters can improve capability, but they require more memory. Quantization stores those values with fewer bits, reducing memory use and often slightly reducing quality.
| Choice | Memory use | Typical trade-off |
|---|---|---|
| Smaller model or stronger quantization | Lower | Faster, but potentially less capable |
| Larger model or weaker quantization | Higher | Better quality, but more memory and slower loading |
Leave memory for the operating system and the context window. If a model fails with an out-of-memory error, choose a smaller tag or a stronger quantization.
Context window, temperature, and other parameters
The context window is the maximum amount of conversation and input the model can consider at once. A larger window consumes more memory. Temperature controls randomness: lower values produce more consistent answers, while higher values produce more varied answers.
A model's defaults can be overridden in a Modelfile or API request. Change one parameter at a time so you can identify its effect.
Calling the REST API with `generate` and `chat`
Ollama exposes HTTP endpoints on port 11434. generate accepts one prompt. chat accepts a sequence of role-based messages.
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Define quantization in one sentence.",
"stream": false
}'With stream: false, the response is one JSON object. Without it, Ollama returns newline-delimited JSON pieces as they are generated.
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [
{"role": "user", "content": "Give one tip for clear documentation."}
],
"stream": false
}'The chat response places the answer in message.content.
Exercise
Which command shows models currently loaded in memory, and which API endpoint is designed for role-based messages?
Show answer
ollama ps shows loaded models, and /api/chat accepts role-based messages.