Project · Advanced · AI Tools
Offline Datasheet RAG on a Pi (Small Models)
Chunk PDFs, embed locally, answer “what’s the absolute max on pin X?”
In this guide
- Goal & materials
- Steps / firmware
- Troubleshooting
- AI assist notes
- Related gear & books
Datasheets answer the questions that matter on the bench: the absolute maximum voltage on a pin, the I²C address options, the timing for a reset line. Finding the answer in a 100-page PDF is slow. Retrieval-augmented generation (RAG) lets a small local model search your own datasheet collection and answer with a page reference, entirely offline on a Raspberry Pi. This guide explains the pieces, gives a compact working pipeline, and is clear about where it falls short.
How RAG works here
- Extract text from each PDF, page by page.
- Chunk the text into small passages and keep the file name and page number with each one.
- Embed each chunk: an embedding model turns text into a vector so similar meanings land close together.
- Retrieve: embed the question, find the closest chunks.
- Answer: give those chunks to a small language model with instructions to answer only from them and cite the page.
The model does not need to "know" your parts. It only needs to read the right passage, which is why small models can work.
Hardware and software
- A Raspberry Pi 5 with 8 GB of RAM or more is the practical choice. Smaller boards work for embedding and search, but generation gets very slow.
- An SSD instead of an SD card makes model loading much faster.
- Ollama runs on 64-bit Raspberry Pi OS and serves both embedding and chat models over a local HTTP API.
- Pull a small embedding model and a small chat model, for example
ollama pull nomic-embed-textplus a chat model in the 1B to 4B parameter range. Speed and quality vary a lot between models and quantizations, so try a couple. - Python packages:
pymupdffor PDF text,numpy, andrequests.
A compact pipeline
import glob, json, numpy as np, requests, fitz # fitz is PyMuPDF
OLLAMA = "http://localhost:11434"
EMBED_MODEL = "nomic-embed-text"
CHAT_MODEL = "your-small-model"
def embed(texts):
r = requests.post(f"{OLLAMA}/api/embed",
json={"model": EMBED_MODEL, "input": texts})
r.raise_for_status()
return np.array(r.json()["embeddings"], dtype=np.float32)
def build_index(folder="datasheets"):
chunks = []
for path in glob.glob(f"{folder}/*.pdf"):
doc = fitz.open(path)
for page_no, page in enumerate(doc, start=1):
text = page.get_text()
for i in range(0, len(text), 1200):
piece = text.strip()
if piece:
chunks.append({"file": path, "page": page_no, "text": piece})
vectors = np.vstack( for c in chunks])
for i in range(0, len(chunks), 32)])
vectors /= np.linalg.norm(vectors, axis=1, keepdims=True)
np.save("vectors.npy", vectors)
json.dump(chunks, open("chunks.json", "w"))
def ask(question, k=4):
chunks = json.load(open("chunks.json"))
vectors = np.load("vectors.npy")
q = embed()
q /= np.linalg.norm(q)
best = np.argsort(vectors @ q)[::-1][:k]
context = "\n\n".join(
f"[{chunks['file']} p.{chunks['page']}]\n{chunks['text']}" for i in best)
prompt = ("Answer using only the excerpts below. Cite the file and page. "
"If the excerpts do not contain the answer, say so.\n\n"
f"{context}\n\nQuestion: {question}")
r = requests.post(f"{OLLAMA}/api/chat", json={
"model": CHAT_MODEL, "stream": False,
"messages": [{"role": "user", "content": prompt}]})
r.raise_for_status()
return r.json()["message"]["content"]
Run build_index() once (and again when you add PDFs), then call ask("What is the absolute maximum supply voltage?"). The chunks overlap slightly so a sentence split at a boundary still appears whole in one of them. For larger collections, swap the NumPy files for a vector database such as Chroma or SQLite with a vector extension.
Where it struggles
- Tables. Datasheet values often live in tables, and PDF text extraction can scramble rows and columns. The right number may be retrieved but misread. This is the biggest limitation for "absolute maximum" questions.
- Scanned PDFs have no text layer. They need OCR first, which adds errors.
- Similar parts. With several variants of a chip in the collection, retrieval can pull the wrong variant's page. Include the part number in your question and keep file names descriptive.
- Speed. On a Pi, indexing a big collection and generating answers takes time. Indexing is a one-off; answers are typically seconds to tens of seconds depending on the model.
Rules for using the answers
- Always open the cited page before acting on a number, especially voltages, currents, and temperatures.
- Treat "not found" as a good answer. It is better than a guess. If the model answers without a citation, discard it.
- Prefer the manufacturer's latest datasheet. Remove old revisions from the folder so they do not get retrieved.
Common mistakes
- Chunks that are too large, so retrieval returns pages of loosely related text and the small model loses the key line.
- Dropping page numbers during chunking, which makes verification hard.
- Using a large chat model on a Pi and concluding RAG is unusably slow. Small models are enough when retrieval is good.
- Mixing embedding models. Vectors from different models are not comparable; rebuild the index if you switch.
Summary
A Pi 5 running Ollama can index your datasheets, retrieve the most relevant pages for a question, and have a small model answer with citations, all offline. Keep chunks small with page numbers attached, require citations, and always confirm critical values on the actual datasheet page, especially when they come from tables.
Related
- Projects hub
- Newsletter
- Books for deeper reading