06 · Serving Qwen behind an API
Run it in Colab View the .ipynb
You are reading this notebook. To run it, open it in Colab with the button above - no install, no GPU needed for most of them - or clone the repository and open it in Jupyter.
- Running this notebook in Colab?
- 6.1 · Start a server
- 6.2 · Your first API call
- 6.3 · Streaming over the API
- 6.4 · Thinking mode over the API
- 6.5 · The portability payoff
- 6.6 · Concurrency: the real reason for vLLM
- 6.7 · Serving checklist for a real deployment
- Checkpoint
Day 2 · ~60 minutes
So far the model has lived inside the notebook. Real applications need it behind a network boundary, so that a web app, a phone, or three of your classmates can all use one GPU.
The key insight: vLLM, Ollama, llama.cpp and LM Studio all speak the OpenAI API. Write your application against that interface and you can swap model and runtime by changing two strings.
By the end you will be able to:
- start a local OpenAI-compatible server;
- call it with the official
openaiPython SDK; - stream responses and count tokens;
- explain when to choose vLLM over Ollama;
- write code that runs unchanged against either.
# --- Setup: works on your laptop AND in Google Colab ------------------------
# In Colab this clones the workshop repo and installs the dependencies (about a
# minute, first run only) so that `src/`, `data/` and the sample files exist.
# On your own machine it just locates the repo. Safe to re-run either way.
import os
import subprocess
import sys
from pathlib import Path
if "google.colab" in sys.modules:
if not Path("/content/ROBT407LLM").exists():
subprocess.run(["git", "clone", "--depth", "1",
"https://github.com/berdakh/ROBT407LLM.git",
"/content/ROBT407LLM"], check=True)
os.chdir("/content/ROBT407LLM")
subprocess.run([sys.executable, "-m", "pip", "install", "-q",
"-r", "requirements.txt"], check=True)
REPO_ROOT = next(p for p in [Path.cwd(), *Path.cwd().parents]
if (p / "src" / "qwen_workshop").is_dir())
sys.path.insert(0, str(REPO_ROOT / "src"))
print("repo root:", REPO_ROOT)
from qwen_workshop.client import BACKENDS, is_up
for name, spec in BACKENDS.items():
status = "RUNNING" if is_up(name) else "not running"
print(f"{name:<12}{spec['base_url']:<34}{status}")
Running this notebook in Colab?
This notebook needs a local OpenAI-compatible server. Colab has none, so run the cell below to install Ollama, start it, and pull the model. It takes two to four minutes the first time. Skip it if you already have Ollama running on your own machine - the notebook will find it.
from qwen_workshop.colab import in_colab, start_ollama
if in_colab():
start_ollama("qwen3:0.6b")
else:
print("Not in Colab - make sure your own server is running: ollama serve")
6.1 · Start a server
Pick one. Run it in a terminal (not in this notebook — it needs to stay running), then come back.
Option A — Ollama (recommended for this workshop)
ollama serve # usually already running after install
ollama pull qwen3:0.6b
Option B — vLLM (needs an NVIDIA GPU)
pip install vllm
vllm serve Qwen/Qwen3-0.6B --max-model-len 8192 --port 8000
Option C — llama.cpp
llama-server -hf Qwen/Qwen3-0.6B-GGUF:Q4_K_M -c 8192 --port 8080 --jinja
Then re-run the cell above until one says RUNNING.
BACKEND = "ollama" # change to "vllm" or "llamacpp" to match what you started
MODEL = "qwen3:0.6b" # for vLLM use "Qwen/Qwen3-0.6B"
from qwen_workshop.client import require_backend
try:
require_backend(BACKEND)
print(f"{BACKEND} is up")
except RuntimeError as exc:
print(exc)
6.2 · Your first API call
Note what is happening here: we are using the official OpenAI SDK, but
every byte stays on your machine. The api_key is required by the SDK and
ignored by the server.
try:
from openai import OpenAI
except ImportError:
import subprocess
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "openai"], check=True)
from openai import OpenAI
from qwen_workshop.client import get_client
client, backend = get_client(BACKEND, model=MODEL)
print(backend)
response = client.chat.completions.create(
model=backend.model,
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "In one sentence: why run a model locally?"},
],
temperature=0.7,
max_tokens=100,
)
print(response.choices[0].message.content)
print()
print(f"prompt tokens : {response.usage.prompt_tokens}")
print(f"completion tokens: {response.usage.completion_tokens}")
print(f"finish reason : {response.choices[0].finish_reason}")
finish_reason matters more than students expect:
stop— the model finished naturally. Good.length— it was cut off bymax_tokens. Your JSON is truncated, your sentence is half-written. Always check this in production code.tool_calls— it wants to call a tool (notebook 07).
truncated = client.chat.completions.create(
model=backend.model,
messages=[{"role": "user", "content": "Count from 1 to 100, one number per line."}],
max_tokens=20,
)
print(f"finish_reason = {truncated.choices[0].finish_reason}")
print(repr(truncated.choices[0].message.content))
print("\n-> if you parsed that as a complete answer, you would be wrong")
6.3 · Streaming over the API
stream = client.chat.completions.create(
model=backend.model,
messages=[{"role": "user", "content": "Write a haiku about cold weather and hot tea."}],
stream=True,
max_tokens=120,
)
pieces = []
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
pieces.append(delta)
print(delta, end="", flush=True)
print(f"\n\n[{len(pieces)} chunks received]")
Note: with stream=True you do not get a usage block by default. Request
it with stream_options={"include_usage": True} if your server supports it.
6.4 · Thinking mode over the API
Each server exposes Qwen3’s thinking switch differently — an annoying but real portability wrinkle. Our helper sets both known forms and lets the server ignore the one it does not recognise.
from qwen_workshop.client import complete
for thinking in (False, True):
result = complete(
client, backend,
[{"role": "user", "content": "If a train leaves at 14:20 and takes 3h45m, when does it arrive?"}],
temperature=0.6, max_tokens=800, enable_thinking=thinking,
)
content = result.choices[0].message.content or ""
# Some servers split reasoning into a separate field instead of <think> tags.
reasoning = getattr(result.choices[0].message, "reasoning_content", None)
print(f"--- enable_thinking={thinking} ---")
if reasoning:
print(f"[reasoning: {len(reasoning.split())} words, in a separate field]")
print(content.strip()[:400])
print(f"({result.usage.completion_tokens} completion tokens)\n")
(The train arrives at 18:05.)
6.5 · The portability payoff
Here is the entire point of this notebook. One function, any backend.
def ask(question: str, backend_name: str = BACKEND, model: str | None = None) -> str:
"""Ask a question to whichever local runtime is available."""
client, backend = get_client(backend_name, model=model)
reply = complete(client, backend, [{"role": "user", "content": question}],
temperature=0.3, max_tokens=120, enable_thinking=False)
return (reply.choices[0].message.content or "").strip()
question = "Name one advantage of running an AI model on your own computer."
for name in ("ollama", "vllm", "llamacpp"):
if is_up(name):
print(f"[{name}] {ask(question, name, BACKENDS[name]['model'])}\n")
else:
print(f"[{name}] skipped - not running\n")
Nothing changed except the backend name. This is why you should never write application code against a specific model’s Python API. Put the OpenAI interface in the middle and stay free to change your mind.
6.6 · Concurrency: the real reason for vLLM
transformers handles one request at a time. vLLM uses continuous batching
and PagedAttention to serve many concurrent users on one GPU, at
dramatically higher total throughput.
Let’s measure concurrency on whatever you have running.
import time
from concurrent.futures import ThreadPoolExecutor
QUESTIONS = [
"Name a vegetable that stores well in winter.",
"What is a good beginner bicycle maintenance task?",
"Suggest one way to reduce electricity use at home.",
"What should I pack for an overnight train?",
"Name a cheap source of protein.",
"How often should I back up my laptop?",
"Suggest a 20-minute study technique.",
"What is a reasonable indoor humidity level?",
]
def timed_sequential(questions):
start = time.perf_counter()
for q in questions:
complete(client, backend, [{"role": "user", "content": q}],
max_tokens=50, temperature=0.7, enable_thinking=False)
return time.perf_counter() - start
def timed_concurrent(questions, workers=8):
start = time.perf_counter()
with ThreadPoolExecutor(max_workers=workers) as pool:
list(pool.map(
lambda q: complete(client, backend, [{"role": "user", "content": q}],
max_tokens=50, temperature=0.7, enable_thinking=False),
questions,
))
return time.perf_counter() - start
if is_up(BACKEND):
sequential = timed_sequential(QUESTIONS)
concurrent = timed_concurrent(QUESTIONS)
print(f"sequential ({len(QUESTIONS)} requests): {sequential:.1f}s")
print(f"concurrent (8 at once) : {concurrent:.1f}s")
print(f"speed-up : {sequential / concurrent:.1f}x")
print("\nOn vLLM with a real GPU expect 4-10x. On Ollama/CPU expect much less -")
print("it queues requests rather than truly batching them.")
else:
print("Start a server first.")
6.7 · Serving checklist for a real deployment
Before you let anyone else use your endpoint:
- Bind carefully.
--host 0.0.0.0exposes it to your whole network. Default to localhost; use a reverse proxy with auth if you need remote access. - Set
--max-model-len. Unbounded context lets one user exhaust your VRAM. - Rate-limit. An open LLM endpoint is a free compute service for whoever finds it.
- Log prompts and latencies — but think hard first about whether you are allowed to log user content. Often you are not.
- Pin versions. Model, runtime and tokenizer. Silent upgrades change outputs.
- Health checks.
GET /v1/modelsis a cheap liveness probe. - Cap
max_tokensserver-side. Otherwise one request can run for an hour.
📖 More: docs/guides/deployment.md
Checkpoint
- Why does the OpenAI SDK work against a local server?
- What does
finish_reason == "length"mean and why must you check it? - What does vLLM give you that Ollama does not?
- What are the first two things you would lock down before exposing a server?
➡️ Next: 07_structured_output_and_tools.ipynb —
make the model return data your program can trust.