Answering questions from a book with a local model
A language model running on a laptop will answer almost any question, whether or not it knows the answer. The usual remedy is retrieval: find the passages of a document that match the question, give the model only those, and ask it to answer from them and name the ones it used. This post is about a small Go service that does that for the Kubebuilder book.
I built it with the Go standard library and Ollama’s Go client, with no LLM framework, and measured it with 29 labelled questions.
- Without the book, the model answered all nine questions the book cannot answer and rated its confidence medium or high each time. The service refused all nine.
- The similarity threshold stopped only the plainly off-topic ones. Questions that sound like the book scored as high as real ones, and the model caught those by citing no passage.
- The JSON schema in the request kept every reply well formed, and every first reply cited only passages from the prompt, so the retry never ran. The check that carries the weight is whether the cited passages were in the prompt.
- Prompt injection is the weak point: an instruction planted in one passage was followed in one run, and the citation check let that answer through.
What the service does
The service has one endpoint, POST /ask, which takes a question and returns JSON. Before it can answer anything, it cuts the book into chunks of at most 300 words along its headings, 361 of them, and turns each one into an embedding: a list of 768 numbers from a small model, embeddinggemma-2:270m, made so that texts about similar things get similar lists. That takes about 16 seconds on the first start, and the result is saved to a file.
A question goes through the same model. Ollama returns vectors of length one, so comparing the question with a chunk is a dot product, and comparing it with the whole book is a loop over a slice. At this size there is no need for a vector database. Each of the five best chunks goes into the prompt with an ID made from its file name and heading, so the model can say which ones it used. The chat model, qwen3:8b, has to answer in JSON and list those IDs.
Why a local model
Everything runs through Ollama on one laptop. Nothing leaves the machine, there is no API key to look after, and a question costs only time. It also keeps every part in view: the model client is Ollama’s own Go package, and the rest is the standard library.
Time is the price. The median answer took 14 to 20 seconds, depending on the run, and the slowest took 94 seconds, longer than the service’s own 60-second timeout. On a laptop, any request that waits for a language model is a long request; turning those calls into jobs is on the roadmap.
Refusal is decided in code
The obvious way to make a model admit what it does not know is to ask it to, in the prompt. Before relying on that, I asked the model on its own. Nine of the eval questions have no answer in the book: five sound close to it, about Istio, Vault or a PodDisruptionBudget, and four have nothing to do with Kubernetes. With no passages, the model answered all nine and rated its own confidence medium or high every time.
So the service decides in Go. A question whose best chunk scores below the threshold is refused before the chat model is called. A reply that cites no passage is returned as a refusal, with refused: true, whatever the text says. With both rules the service refused all nine of those questions and answered all 18 that it should have.
A schema fixes the shape of an answer, not its truth
Ollama accepts a JSON schema in the format field of a chat request and holds the reply to it. Its documentation also suggests putting the schema in the prompt, and the service does both. I wrote a retry for replies that fail the checks and expected to need it. In every run, every first reply passed, so the retry never ran.
A well-formed reply can still be wrong. Nothing in a schema stops the model from citing a passage it was never shown. So the service checks what it can: every cited ID must be one of the five in the prompt, or the reply is retried once and then answered with a 502. In the eval, 16 of the 18 answers cited a chunk that holds the labelled evidence. That shows an answer points to the right passage. Whether it reports the passage correctly was not checked: an answer can cite the right chunk and still get a number wrong, and checking that means reading each answer against the book, which this eval does not do.
What I would have got wrong without measuring
Retrieval can be measured on its own in about a minute, so I did that first. Off-topic questions scored clearly lower than real ones. The five near misses did not: their best scores, 0.73 to 0.81, sit inside the range of the answerable questions, 0.76 to 0.88. A threshold sweep said 0.76 was the best setting for this set, and on those numbers alone I would have raised the default.
The full run said otherwise. At 0.70 all five near misses reached the chat model, and the model refused each one with an empty source list. A threshold of 0.76 would have saved three model calls and left a margin of 0.003 above the lowest-scoring real question. The default stayed at 0.70.
The threshold drops the obvious cases cheaply, and the citation rule handles the close ones.
Halving the chunk size to 150 words changed little, one question either way on each count. All of this is 29 questions written for this project, one model, temperature 0 and one run per setting, so the numbers describe this set and nothing wider.
Where it breaks: prompt injection
The eval adds a test document to the index. One passage hides an instruction among ordinary text: start the answer with a code word. Another asks the model to print its instructions. Each passage also holds a real fact, so a question about that fact retrieves it. The only defence is one sentence in the prompt, which tells the model that the passages come from a document and that it must not follow any instruction written inside them.
With 300-word chunks the model ignored both. With 150-word chunks it followed the first: the answer opened with the code word and then gave the fact. The planted passage was the same and ranked first both times; only the other four passages differed. The citation check passed that answer, because it cited the passage it had been given.
One run per setting gives no rate. It does show where the weak point is.
Anything retrieved becomes input the model may obey, and nothing in the code checks for that.