New Tool Radar

Local AI

How to Search Your Documents from a Python Script with GPT4All

Embed your documents with GPT4All Embed4All, retrieve the closest chunks to your question with a similarity search, and hand those chunks to a local GPT4All model to answer from.

Schematic diagram with three steps: embed document chunks with Embed4All, retrieve the chunks closest to your question, then ask a local GPT4All model using those chunks.

GPT4All’s desktop app has LocalDocs: point it at a folder and it indexes your files, retrieves the relevant passages for a question, and adds them to the model’s prompt. That retrieval pipeline lives in the chat app only — it is not exposed through the Python SDK. What the SDK does give you are the two building blocks: Embed4All, which turns text into embedding vectors on your own computer, and the GPT4All class, which generates answers. This guide wires those two blocks together in a small script: embed your documents, retrieve the chunks closest to your question, and ask the model with those chunks in the prompt.

Embed your documents

GPT4All’s Python SDK docs list the embedding models you can use with the Embed4All class. The Nomic Embed v1.5 model runs locally as a GGUF file with a 2048-token context length and 64 to 768 embedding dimensions, at 262 MiB:

from gpt4all import Embed4All
embedder = Embed4All("nomic-embed-text-v1.5.f16.gguf")

Split each document into short chunks first — a use-case guide for GPT4All shows a helper that splits text into chunks of about 800 words — then embed every chunk. The guide marks document chunks with the prefix "search_document" when embedding them:

chunks = [
    "The lighthouse keeper named Mara checked the lamp at noon.",
    "The supply boat arrives on Tuesdays.",
    "The harbour office opens at eight in the morning.",
]
chunk_vectors = [embedder.embed(c, prefix="search_document") for c in chunks]

The SDK docs’ own embeddings example, using the nomic library instead, embeds two strings locally and reports 768 dimensions per embedding — the same size the v1.5 model reports at full length.

Retrieve the chunks closest to your question

Embed the question the same way, with the matching query prefix, then compare it with every chunk vector using a dot product and keep the three closest. The guide’s retrieval helper does exactly that:

import numpy as np

def retrieve(question, k=3):
    q_vec = np.array(embedder.embed(question, prefix="search_query"))
    sims = [np.dot(q_vec, np.array(v)) for v in chunk_vectors]
    top_k = np.argsort(sims)[-k:][::-1]
    return [chunks[i] for i in top_k]

found = retrieve("When is the lamp checked?")

For the practice question “When is the lamp checked?”, the chunk that should come back first is “The lighthouse keeper named Mara checked the lamp at noon.” — it shares the words lamp and checked with the question, while the boat and office chunks do not.

Ask the local model with those chunks

Load a chat model with the GPT4All class — the SDK docs’ own example is a 4.66 GB model that needs 8 GB of RAM — and ask inside a chat session, pasting the retrieved chunks into the prompt yourself:

from gpt4all import GPT4All
model = GPT4All("Meta-Llama-3-8B-Instruct.Q4_0.gguf")
context = "\n".join(found)
with model.chat_session():
    print(model.generate("Using only this context:\n" + context + "\nQuestion: When is the lamp checked?"))
Check What you should see If you do not see it
Embedded Three chunk vectors, one per practice sentence The embedding model is still downloading on first use — wait for it to finish
Retrieved The lamp chunk ranks first for “When is the lamp checked?” You embedded the question with the document prefix — use "search_query" for the question and "search_document" for the chunks
Answered The model answers “at noon” from the retrieved chunk The chunks were not pasted into the prompt — the model cannot see what you did not give it

Expected result. We checked GPT4All’s own Python SDK docs for the Embed4All model table and the chat-session example, and a third-party GPT4All use-case guide for the chunk, embed, retrieve, and ask pattern. We did not run the script.

Limits

GPT4All gives you an embedding model and a generator, not a retrieval framework: the same guide warns that the built-in LocalDocs pipeline is chat-app only, and that chunking strategy, re-ranking, and hybrid search are yours to build — tools like LangChain or a dedicated vector database add real work on top. Retrieval is probabilistic, not a guarantee: the wrong chunk can rank first, and a chunk the model never sees cannot inform its answer, so check the retrieved chunks before you trust the reply. The practice sentences above are fictional; use your own documents. We did not run the script, so no retrieved chunk or answer here is a recorded result.

Sources

See all