Local LLMs with Ollama
File: ollama.lisp
This chapter uses the same pattern as the Gemini chapter, but calls a model on your own machine through Ollama’s HTTP API.
The code
1 ;; sbcl --script ollama.lisp (needs Quicklisp + local Ollama)
2 #-quicklisp (load "~/quicklisp/setup.lisp")
3 (ql:quickload :com.inuoe.jzon :silent t)
4
5 (defun ollama (prompt &optional (model "qwen3.5:4b"))
6 (let* ((fmt
7 (concatenate
8 'string
9 "{\"model\":~a,\"prompt\":~a,"
10 "\"stream\":false,\"think\":false}"))
11 (body (format nil fmt
12 (com.inuoe.jzon:stringify model)
13 (com.inuoe.jzon:stringify prompt)))
14 (j (com.inuoe.jzon:parse
15 (uiop:run-program
16 (list "curl" "-s"
17 "http://localhost:11434/api/generate"
18 "-d" body)
19 :output :string))))
20 (or (gethash "response" j)
21 (error "Ollama: ~a" (gethash "error" j)))))
22
23 (print (ollama "what is 1 + 10?"))
How it works
- Ollama serves a local HTTP API on port 11434. The
/api/generateendpoint takes a model name and a prompt. "stream":falseasks for the whole reply in one JSON document instead of a stream of token fragments, so a singlejzon:parsehandles it."think":falseturns off the reasoning trace on models that have one (like the Qwen 3 family), so theresponsefield holds just the answer.- The reply’s
responsekey holds the generated text, which the function returns. When something fails (most often a model you have not pulled), the reply has anerrorkey instead, and the finalorsignals it as a Lisp error such asOllama: model 'llama9' not found. If the server is down,curlexits with a non-zero status anduiop:run-programsignals an error itself.
Running it
Install Ollama, pull the model (ollama pull qwen3.5:4b), start the server, then:
1 $ sbcl --script ollama.lisp
Actual output (the wording varies between runs):
1 "The result of $1 + 10$ is **11**."
Using it
Local models are private, cost nothing per call, and work offline, which suits batch jobs over local documents (pair this with the summarizer or vector-store chapters). Change the model name to use any model you have pulled: (ollama "hi" "llama3.2"). Speed depends on your hardware; small 4B models run well on a laptop.