Local LLM File Search: Embeddings, Indexing & Retrieval
In this article
Local LLM file search lets you find documents by meaning, not just by exact keywords. Instead of matching typed words, it converts text into numerical vectors (embeddings) and finds files whose vectors are closest to your query. This guide explains how embeddings, indexing, and retrieval work together, and how you can build your own local semantic search system.
What You'll Need
- A computer with at least 8 GB RAM (16 GB recommended)
- Python 3.8+ installed
- Basic familiarity with the command line
- An embedding model (e.g., from Hugging Face) and a vector database (like Chroma or FAISS)
- Libraries:
sentence-transformers,chromadborfaiss-cpu,pypdffor PDFs,python-docxfor Word files
Step-by-Step
- Install required libraries. Use pip:
pip install sentence-transformers chromadb pypdf python-docx. For FAISS, usepip install faiss-cpu. - Choose an embedding model. For local use, a good default is
all-MiniLM-L6-v2(fast, 384 dimensions) orall-mpnet-base-v2(more accurate, 768 dimensions). Load it withSentenceTransformer('all-MiniLM-L6-v2'). - Extract text from your files. Write a script that walks through your target folder, reads PDFs (using
pypdf), Word documents (python-docx), and plain text files, and stores the file path and its content. - Chunk the text. Break each document into smaller pieces (e.g., 500–1000 characters with some overlap). This helps the model capture context and makes retrieval more precise. Store each chunk with its source file and position.
- Generate embeddings. For each chunk, call the model to produce a vector. For example,
embedding = model.encode(chunk_text). This converts the text into a list of numbers that represents its meaning. - Index the embeddings. Store the vectors in a vector database. With Chroma, create a collection and add documents with their embeddings. With FAISS, build an index and add the vectors. This step organizes the vectors for fast similarity search.
- Save the index. Persist the database or index to disk so you don’t have to re-embed every time. Chroma can persist automatically; FAISS requires
faiss.write_index(). - Perform a search. When a user enters a query, embed the query using the same model, then search the index for the most similar vectors. Return the corresponding chunks and their source files, ranked by similarity score.
- Display results. Show the top N results with the file name, the chunk text, and the similarity score. Optionally, highlight the matching part.
Common Mistakes
- Using different models for indexing and querying. Always embed both documents and queries with the exact same model, or the vectors won’t be comparable.
- Not chunking text. Embedding an entire 100-page PDF as one vector loses detail and makes retrieval vague. Always chunk.
- Ignoring overlap. Without overlap, a sentence might be split across chunks, causing missed matches. Use 10–20% overlap.
- Forgetting to normalize vectors. Some similarity metrics (like cosine) work best with normalized vectors. If your model doesn’t normalize, do it manually.
- Storing raw text in the index. Vector indexes only store vectors and IDs. You need a separate mapping from ID to the actual text and file path.
- Not cleaning extracted text. PDFs often include headers, footers, and page numbers that can pollute embeddings. Clean the text before chunking.
Tips
- Start with a small test folder. Verify the pipeline works on 10–20 files before scaling to thousands.
- Use a GPU if possible. Embedding is compute-heavy; a GPU speeds it up significantly. For CPU-only, choose a smaller model like
all-MiniLM-L6-v2. - Cache embeddings. If you re-index frequently, save embeddings to disk (e.g., as a pickle or parquet) to avoid recomputing.
- Experiment with chunk size. For technical documents, 300–500 characters might work better; for general text, 1000 is fine. Test different sizes.
- Use metadata filters. Store metadata like file type, date, or author to filter results before similarity search, improving speed and relevance.
- Consider hybrid search. Combine vector search with keyword search (e.g., BM25) to catch exact terms that embeddings might miss.
Troubleshooting
- No results or poor results: Check that the query is embedded with the same model. Also verify that the chunks contain meaningful text — if you extracted garbage from a PDF, embeddings will be meaningless.
- Out of memory: Reduce chunk size, process files one at a time, or use a smaller embedding model. For FAISS, use the
IndexFlatIPorIndexIVFFlatwhich are memory-efficient. - Slow indexing: Use batch processing (
model.encode(list_of_texts)) instead of looping. Also consider using a smaller model or reducing the number of files. - Different results on re-run: Ensure you’re not re-embedding with a different model version. Save the model name and version with the index.
- Can’t load the index after restart: For Chroma, make sure the persistence directory is set. For FAISS, use
faiss.read_index()and ensure the file exists. - Search returns irrelevant results: Increase chunk overlap, or use a more accurate model like
all-mpnet-base-v2. Also, try normalizing vectors and using cosine similarity.
Frequently Asked Questions
What exactly is an embedding?
An embedding is a list of numbers (a vector) that represents the meaning of a text. Words or sentences with similar meanings have vectors that are close together in high-dimensional space.
Why do I need a vector database? Can’t I just store the vectors in a CSV?
You can, but a vector database is optimized for fast similarity search. It uses algorithms like HNSW or IVF to find the nearest neighbors in milliseconds, even with millions of vectors. A CSV would require a brute-force scan, which is slow.
How many dimensions should my embeddings have?
It depends on the model. MiniLM produces 384 dimensions, MPNet 768. More dimensions can capture more nuance but require more memory and computation. For most local use, 384 is sufficient.
Can I use this to search images or audio?
Yes, but you need a multimodal embedding model that can encode images or audio into the same vector space as text. That’s more complex and requires more resources.
Do I need an internet connection?
No, once you download the embedding model, everything runs locally. This is a key advantage for privacy and offline use.
How do I handle very large documents?
Chunk them into smaller pieces and index each chunk separately. When you search, you’ll get the specific chunk that matches, along with the source file. This is more useful than a single vector for the whole document.