# Mac exercise for mission 10.3: From ollama run to the API, part 2: both APIs

Part of the course “AI from Zero to Autonomous Agents” (https://llm.yoga/en). Page for humans: https://llm.yoga/en/mac-10-3

About 12 minutes at your computer · Type this address: **llm.yoga/en/mac-10-3**

In the mission, Kai learned that for the chat call shown here, the address, key and model name change. Other options must also be supported. Here you send the same question to both of Ollama’s APIs and see that the same model is at work behind them. This is the second part of the exercise from chapter 10.

**Before you start**

You need **Ollama** in the terminal. Ollama comes as an app: download it from ollama.com/download and drag it into your Applications folder. On first launch, the app asks permission, if needed, to set up the `ollama` terminal command. It requires macOS 14 or newer. More on this in [chapter 10](https://llm.yoga/en/10-lokale-modelle-4.md). Check:

```
ollama --version
```

`curl`, which you use here to send requests, comes with macOS.

### 1. Get a small model ready

If you still have qwen3:1.7b from the exercise for 10.1, skip this step.

```
ollama pull qwen3:1.7b
```

### 2. Ollama’s own API

`/api/chat` takes a list of messages. `"think": false` turns off Qwen3’s thinking. The prompt is in German, as in the measurement: „Nenne drei Farben, nur die Wörter.“ (name three colors, just the words).

```
curl -s http://localhost:11434/api/chat -d '{"model": "qwen3:1.7b", "think": false, "stream": false, "messages": [{"role": "user", "content": "Nenne drei Farben, nur die Wörter."}]}'
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
{"model":"qwen3:1.7b","created_at":"2026-11-25T07:36:45.774564Z","message":{"role":"assistant","content":"Rot, Blau, Gelb."},"done":true,"done_reason":"stop","total_duration":732869791,"load_duration":557128041,"prompt_eval_count":28,"prompt_eval_cached_count":0,"prompt_eval_duration":56198000,"eval_count":9,"eval_duration":115130000}
```

The answer means “red, blue, yellow”. The times are in nanoseconds: 9 tokens in 0.115 seconds is 78 tokens per second.

### 3. The OpenAI-compatible API

The same question in the OpenAI format. Here, `reasoning_effort: "none"` turns off thinking.

```
curl -s http://localhost:11434/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "qwen3:1.7b", "reasoning_effort": "none", "messages": [{"role": "user", "content": "Nenne drei Farben, nur die Wörter."}]}'
```

**Output (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
{"id":"chatcmpl-291","object":"chat.completion","created":1795592205,"model":"qwen3:1.7b","system_fingerprint":"fp_ollama","choices":[{"index":0,"message":{"role":"assistant","content":"Röte, Blau, Gelb."},"finish_reason":"stop"}],"usage":{"prompt_tokens":28,"prompt_tokens_details":{"cached_tokens":27},"completion_tokens":11,"total_tokens":39}}
```

The two APIs arrange the data differently: the text is now under `choices`, the tokens under `usage`. 27 of the 28 tokens came from the cache that the first call had filled. And „Röte“ (redness) instead of „Rot“ (red): at temperature 0.6, wording can vary between calls.

### 4. Let the page check it

Paste both answers from steps 2 and 3. The page compares how many tokens the two APIs count for the question.

The check needs JavaScript. Without it: prompt_eval_count and prompt_tokens are both 28.

### 5. The token limit trap

The same OpenAI request with a limit of 20 tokens, but without `reasoning_effort`:

```
curl -s http://localhost:11434/v1/chat/completions -H "Content-Type: application/json" -d '{"model": "qwen3:1.7b", "max_tokens": 20, "messages": [{"role": "user", "content": "Nenne drei Farben, nur die Wörter."}]}'
```

**Output, shortened (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
"message":{"role":"assistant","content":"","reasoning":"Okay, the user asked for three colors, but they only want the words. Let me"},"finish_reason":"length"
```

The model thought, in English, and used up the 20 tokens before it got to the answer. `content` is empty. A script that only reads `content` would get an empty text. With `"reasoning_effort": "none"`, „Röte, Grün, Blau.“ (redness, green, blue) came back after 11 tokens.

### 6. Which model names are available

```
curl -s http://localhost:11434/v1/models
```

**Output, shortened (Measured on an M4 Mac (24 GB) on November 25, 2026)**

```
{"object":"list","data":[{"id":"qwen3:1.7b","object":"model",…},{"id":"qwen3:8b","object":"model",…}]}
```

These exact names go into Kai’s script as `model`.

### 7. In the Python script

This example follows Ollama’s OpenAI compatibility documentation. It requires Python and the installed OpenAI library:

```
from openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1/", api_key="ollama")
antwort = client.chat.completions.create(model="qwen3:1.7b", messages=[{"role": "user", "content": "Nenne drei Farben."}], reasoning_effort="none")
print(antwort.choices[0].message.content)
```

(Checked against the documentation, not run here) The `openai` library wasn’t installed on the test Mac; the two `curl` calls above show the same API. (The variable antwort is German for “answer”.)

### 8. And Docker?

According to the Ollama FAQ, Docker Desktop on the Mac provides no GPU acceleration for the Ollama container. So run Ollama as an app. An interface like Open WebUI can go into the container; it reaches the app via `http://host.docker.internal:11434`. (Checked against the documentation, not run here)

### 9. Clean up

```
ollama stop qwen3:1.7b
```

If you no longer need the model:

```
ollama rm qwen3:1.7b
```

LM Studio (port 1234), the llama.cpp server (9931) and mlx-lm (8080) support parts of the OpenAI format. For the call in step 3, check the address, model name and supported options; disabling thinking is also model-dependent.

#### On the DGX Spark

The OpenAI-compatible query from step 3 can also be used on the Spark. Address, port, model name and options must match the server. With the playbook “Chat with LLMs Using Open WebUI and Ollama”, Ollama runs in a container there; for steps 2 and 3, you run the commands inside the container or set up protected access to the Ollama port. If you want to serve many users at once, use the playbook for vLLM; it speaks the OpenAI format too. (Checked against the documentation, not run here)
