Ollama and vLLM: Serving a Model

First lesson of the Self-Hosting AI Models Locally course, free to read: about 20 minutes, 3 exercises and 3 quiz questions. This is the catalogue's base lesson; in the student space, it is rewritten for your profile.

Objectives

  • Explain why and when to host a model yourself
  • Download, run, and query a model with Ollama
  • Place vLLM relative to Ollama and know its OpenAI-compatible API

OllamavLLMModel server API

Why host a model yourself?

Calling a model from a cloud provider is simple: one key, one request, one bill per token. Hosting an open model (one whose weights can be downloaded) on your own machine meets different needs:

  • confidentiality: data never leaves your infrastructure;
  • cost control: a predictable machine cost rather than a cost proportional to usage;
  • independence: no forced version changes, and offline operation is possible.

The trade-offs are real: you need suitable hardware (often a GPU), and you have to install, monitor, update, and secure it. The open models you can run on a single card are also generally less capable than the largest commercial models on difficult tasks. Hence the “local first, cloud as a fallback” architecture covered later.

Ollama: getting started in three commands

Ollama is an open-source tool that downloads, manages, and serves models. On Linux, the official installation is done with a script, then:

ollama serve &                 # starts the server (often run as a service)
ollama pull qwen2.5:0.5b       # downloads a small model
ollama run qwen2.5:0.5b "Explain a for loop in one sentence."
ollama list                    # downloaded models
ollama ps                      # models loaded in memory, on GPU or CPU

Models are identified by a name and a tag (qwen2.5:0.5b) that specifies the size and sometimes the quantization. Without a GPU, Ollama runs on CPU, more slowly: that is enough for learning with a small model.

Ollama’s HTTP API

By default, the server listens on 127.0.0.1:11434. Your application queries it over HTTP:

curl -s http://127.0.0.1:11434/api/generate -d '{
  "model": "qwen2.5:0.5b",
  "prompt": "Give a synonym for fast.",
  "stream": false
}'

The JSON response contains the text (response), a done flag, and measurements: prompt_eval_count and eval_count (tokens read and generated), eval_duration and total_duration (in nanoseconds). Other routes exist: /api/chat for a conversation, /api/tags for the list of models, /api/embed for embeddings.

Ollama also exposes an OpenAI-compatible API under /v1 (for example /v1/chat/completions): a client library designed for OpenAI works once you change the base URL.

vLLM: serving at scale

vLLM is an open-source inference engine designed for throughput on GPU. It dynamically groups in-flight requests (continuous batching) and manages the attention cache memory in pages (PagedAttention), which lets it serve many simultaneous requests efficiently.

vllm serve Qwen/Qwen2.5-7B-Instruct --max-model-len 8192 --api-key "$VLLM_API_KEY"
curl -s http://127.0.0.1:8000/v1/models -H "Authorization: Bearer $VLLM_API_KEY"

vLLM loads models in Hugging Face format (identified as organization/model) and by default serves an OpenAI-compatible API on port 8000. An official Docker image (vllm/vllm-openai) makes deployment easier.

Which one to choose?

Criterion Ollama vLLM
Setup very simple more technical
Hardware CPU or GPU mainly GPU
Model format GGUF (quantized) Hugging Face, AWQ and GPTQ quantizations…
Strength prototyping, small workloads many simultaneous requests

Since both speak the same OpenAI dialect, you can start with Ollama and move to vLLM when the load requires it, without rewriting the application: only the URL and model names change. Measure your own load before deciding: no generic figure replaces a test on your hardware.

Key takeaways

  • Self-hosting brings confidentiality and control, at the price of operations.
  • Ollama: pull, run, list, ps, API on port 11434.
  • vLLM: throughput-oriented on GPU, OpenAI-compatible API on port 8000.
  • The OpenAI-compatible API lets you switch servers without rewriting the application.

Exercises

Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.

  1. TaskDifficulty 12 hints

    Run and query Ollama

    In the exercise terminal:

    1. Install Ollama if the ollama command is missing (official script), then start the server in the background: ollama serve > ollama.log 2>&1 &.
    2. Download the qwen2.5:0.5b model and check that it appears in ollama list.
    3. Use curl to send a non-streamed request to /api/generate asking “Donne un synonyme de rapide.” and save the response in reponse.json.
    4. Display the response field with python3 -c (json module).
    • `ollama list` shows a line starting with `qwen2.5:0.5b`
    • `curl -s http://127.0.0.1:11434/api/tags` returns JSON containing `qwen2.5:0.5b`
    • `reponse.json` is a single JSON object with `"done": true`
    • The `response` field displayed is non-empty text
  2. TaskDifficulty 22 hints

    Read the measurements in a response

    Create the file mesures.json with this content (an Ollama response saved during a test):

    {"model": "qwen2.5:0.5b", "response": "Véloce.", "done": true,
     "total_duration": 2150000000, "load_duration": 900000000,
     "prompt_eval_count": 34, "prompt_eval_duration": 250000000,
     "eval_count": 120, "eval_duration": 1000000000}

    Using python3 -c, print: the number of tokens generated, the total duration in seconds, and the share of the total duration spent loading the model (as a percentage, rounded to the nearest integer).

    • The file `mesures.json` is valid JSON (`python3 -m json.tool mesures.json` succeeds)
    • Durations are converted from nanoseconds to seconds (division by 10^9)
    • The three values are printed by a single command
  3. Multiple choiceDifficulty 21 hint

    Choose the server

    A team must serve a 7-billion-parameter model to an internal application that regularly receives several dozen simultaneous requests, on a dedicated GPU server. Which choice is the best fit?

    • Ollama on CPU, to avoid using the GPU
    • vLLM on the GPU, designed for throughput with many simultaneous requests
    • A Python script that loads the model on every request

End-of-lesson quiz

  1. Which port does Ollama listen on by default?

    • 8000
    • 11434
    • 8080
  2. What benefit does the OpenAI-compatible API of Ollama and vLLM provide?

    • It makes the models more accurate
    • It lets you switch servers by changing the URL and the model name, without rewriting the application
    • It removes the need for a GPU
  3. What does the ollama ps command show?

    • The models available on the Ollama website
    • The models currently loaded in memory and where they run
    • The machine's system processes

The rest of the course

  1. Choosing a Model and Its Quantization20 min
  2. VRAM and Throughput20 min
  3. A "Local First, Cloud as a Fallback" Gateway20 min
  4. Securing a Model Server20 min

Self-Hosting AI Models Locally · Advanced · 1 h 40 min