Embeddings of a single notes directory
Working with a growing collection of markdown notes locally, I needed fast semantic sorting without moving data to the cloud. The solution: local embedding generation via sentence-transformers and FAISS-based search, everything staying on disk under full control.
Why need embeddings
In a notes folder, finding a file by keywords in title or body can be hard. An embedding (vector representation of text) lets compare file contents as points in multi-dimensional space. Practically, this enables searching similar notes, grouping topics, and building a simple personal index. No external services, all computation on the DevOps machine.
Tools and local run
Main stack: Python, sentence-transformers for embeddings, faiss-cpu for approximate nearest neighbor search. Install with one command:
Model choice depends on languages/topics in notes. all-MiniLM-L6-v2 suits most text corpora, supports RU and EN. For domain-specific precision, swap model from Hugging Face repo, keeping compatibility with library input format.
After installation can quickly verify with one command:
Notes directory structure
Typical structure: root notes/ folder, .md files, possibly subfolders by years or projects. Paths must be stable, script walks them recursively.
Script scans only .md files, ignores _drafts or .git to avoid service data.
Vector generation script
Minimal script gen_embeddings.py walks directory, reads each markdown, forms embedding, saves to .npy alongside source. Allows not storing heavy matrix in memory, update indexes per file.
Run one-liner:
After execution, .npy files appear alongside each .md, size (384,) for chosen model, ready for search.
Search and use results
Search built on FAISS. Example search.py accepts query and returns paths to three most similar notes:
Usage:
Output shows files and similarity score. All data stays on disk, queries don’t leave machine.
With large file counts (>10 000) IndexFlatIP may consume significant RAM. For production directories, switch to faiss.IndexIVFFlat with prior training, requiring split into training and search sets.
Security and data privacy
Main plus local approach: data never leaves environment. Embeddings are mathematical text representations, files remain untouched. If notes contain confidential data, consider: using own model trained on local data, or filtering paths excluding confidential or secrets directories. Storing .npy alongside markdown doesn’t increase leak risk, as they’re not readable without corresponding script.
Automation of updates
To keep index fresh after each note edit, set up simple cron job or git hook. Example update_embeddings.sh:
Add to crontab (daily run at 03:00):
Can also use git hooks: post-commit in notes repo will regenerate embeddings for changed file, but full recalculation is simpler and more reliable for small folders.
Conclusion and ideas for next steps
Local embeddings for single notes directory give fast semantic access without external service dependencies. Configured generation script and FAISS search work within minutes after library install. Possible next steps:
- Integrate with vim/neovim via plugin for searching similar notes directly from editor.
- Replace
IndexFlatIPwith quantized indexIndexIVFPQto save RAM with thousands of files. - Add metadata (tags, author, date) into vector alongside text for more precise filtering.
- Export index to format understandable by other tools (e.g.,
jsonwith coordinates for later t-SNE/UMAP visualization).
Archive of scripts and example configs posted to personal repo for quick replication on other machines.