Topic 4 of 5 · 4 min read
Local embeddings for private document search
Goal: Build a small local search over a folder of documents with a locally run embedding model.
An embedding model turns text into a list of numbers so that similar meanings land close together, and running one locally lets you search private files without sending a word to a cloud service.
Check yourself · 1 of 3
Why can an embedding search find a document about "refund policy" for the query "getting my money back"?
Why it matters
Keyword search misses a document that says "refund policy" when you search for "getting my money back". Embeddings find it by meaning. If the documents are contracts, medical notes or source code, keeping the whole pipeline local also removes a data-protection question.
How to do it
- Pull an embedding model, such as
nomic-embed-textor another listed by your runner. These are separate from chat models. - Split each file into chunks of a few hundred words, with a small overlap, so one vector covers one idea.
- Embed every chunk and store the vector with the file path and chunk text. A SQLite table, a local vector extension, or even a JSON file works for thousands of chunks.
- At query time embed the question with the same model and compute cosine similarity with every stored vector. Return the top few chunks.
- Optionally, paste those chunks into a local chat model's prompt and ask it to answer from them only. That is retrieval-augmented generation (RAG).
import ollama, numpy as np
def embed(texts):
return np.array(ollama.embed(model="nomic-embed-text", input=texts)["embeddings"])
chunks = [c for c in load_chunks("./docs")] # [{"path":..., "text":...}]
vectors = embed([c["text"] for c in chunks])
vectors /= np.linalg.norm(vectors, axis=1, keepdims=True)
def search(q, k=3):
v = embed([q])[0]
v /= np.linalg.norm(v)
best = np.argsort(vectors @ v)[::-1][:k]
return [chunks[i] for i in best]
Before and after
Before: The search box runs
grepover a folder and returns nothing for a query using different words.After: The same query returns the three closest chunks with file names, and the app shows the path so the user can open the source.
What goes wrong
- Mixing models. Vectors from one embedding model cannot be compared with vectors from another, and changing the model means re-embedding everything.
- Bad chunks. A chunk that cuts a table or a sentence in half retrieves poorly. Check some results by eye.
- Over-trust. A close match is only the nearest text, not proof that the answer is in it. Show sources and let the user check.
- Stale index. Files change. Store a file hash or modified time and re-embed what changed.
- Prompt format. Some embedding models expect a prefix, such as a query or document marker. Read the model card.
Examples
A notes folder
A developer indexes 800 markdown notes. Each note is split at headings into chunks, embedded once and saved in SQLite with the path. Searching "how did we handle the failed deploy" returns the postmortem note even though it never uses the word "failed".
Model swap breaks results
A team upgrades to a newer embedding model but keeps the old vectors. New queries are embedded with the new model and compared with old vectors, so results become near random. The fix is to re-embed every chunk and store the model name with the index.
Key points
- Embeddings map text to vectors so that similar meaning gives nearby vectors, which finds matches that keywords miss.
- A local embedding model keeps private documents on your machine for the whole search pipeline.
- Chunk files, embed each chunk, store vectors beside path and text, and rank by cosine similarity.
- Query and documents must use the same embedding model; changing it means re-embedding.
- Show source chunks, because nearest text is not proof of an answer.
Common mistakes
- Comparing vectors made by two different embedding models.
- Embedding whole files as single vectors, which blurs different topics together.
- Using a chat model's endpoint to produce embeddings.
- Never updating the index when files change.
Check yourself · 2 of 3
You change the embedding model but keep the old stored vectors. What happens?
Check yourself · 3 of 3
You want private files searched without any cloud call. Which setup does that?
Practice items, not counted towards your mastery.
Start here
- Ollama: OpenAI compatibility (opens in a new tab)DocsFree
Lists the endpoints and request fields Ollama accepts through its OpenAI-style API, and what is not supported.
- llama.cpp server README (opens in a new tab)DocsFree
Flags and endpoints of llama-server, including its OpenAI-compatible routes and JSON schema options.
- vLLM: structured outputs (opens in a new tab)DocsFree
How vLLM constrains output to a JSON schema, regex or grammar.
More for this topic
- Ollama: embeddings (opens in a new tab)DocsFree
Generating embeddings locally and the shape of the response, with a search example.