Building a RAG Pipeline With Open-Source Embeddings: Steps and Pitfalls
Build a RAG pipeline with open-source embeddings: load, chunk, embed with all-MiniLM-L6-v2, store in Chroma and retrieve with LangChain. Pitfalls included.
By Galactic News Editorial · Published 2026-11-22T16:41:23+00:00 · Updated 2026-11-22T16:41:23+00:00
Building a RAG Pipeline With Open-Source Embeddings: Steps and Pitfalls
Retrieval-augmented generation, or RAG, grounds a language model in your own documents. The model retrieves relevant passages at query time and answers from them, instead of guessing from training data. Open-source embeddings make the retrieval step free to run and easy to self-host. This tutorial builds a working RAG pipeline with sentence-transformers embeddings, Chroma and LangChain, then covers the pitfalls that break most first attempts.
How a RAG pipeline works
A RAG pipeline has two phases. At index time, you load documents, split them into chunks, convert each chunk into a vector with an embedding model, and store the vectors in a database. At query time, you embed the user's question with the same model, search for the nearest chunks, and pass those chunks to the language model as context.
The embedding model is the heart of retrieval. It maps text to a fixed-length vector, so similar meanings land close together. The open-source default is sentence-transformers/all-MiniLM-L6-v2. It outputs 384-dimensional vectors from a 22.7 million parameter encoder, and it runs on a laptop CPU with no API key. Its model card states a 256 word-piece input limit, so longer text is truncated. Chunk accordingly.
Chroma is the vector store in this tutorial. It is an open-source embedding database that stores vectors with their text and metadata, and it ships Python and JavaScript SDKs. LangChain provides the glue: document loaders, text splitters, embedding wrappers and retriever chains.
How to build rag pipeline open source embeddings in seven steps
- Set up the environment. Create a virtual environment and install the pieces:
pip install langchain chromadb sentence-transformers pypdf. Use a pinned requirements file so the build stays reproducible. - Load the documents. Point a loader at your files. LangChain's PyPDFLoader reads PDFs, and its directory loaders handle folders of text or Markdown. Keep the source path and file name in each chunk's metadata, so answers can cite where they came from.
- Split into chunks. Use RecursiveCharacterTextSplitter with a chunk size near 1,000 characters and an overlap of about 200 characters. Overlap keeps a sentence that straddles a boundary readable in both chunks. Never split below the sentence level if you can avoid it.
- Embed with an open-source model. Create the embeddings with
HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2"). The same model must embed both documents and queries, or the vectors will not match. Keep chunks under 256 word pieces, since the model truncates longer input. - Store vectors in Chroma. Create a persistent client with
chromadb.PersistentClient(path="./chroma_db")and add the chunks with stable IDs. Rebuild the collection from scratch when the source documents change, or update only the changed files by their IDs. - Build the retrieval chain. Wrap the store as a retriever and connect it to your language model. Retrieve the top four or five chunks per query. Pass them to the model with an instruction to answer only from the provided context, and to say it does not know when the context is silent.
- Test with real questions. Ask ten questions you already know the answers to. Check that the right chunks were retrieved, not just that the final answer sounds right. Retrieval quality decides answer quality.
Choosing the embedding model and vector store
all-MiniLM-L6-v2 is the safe default. It is small, fast and widely used, and its 384-dimensional vectors keep storage cheap. Its siblings trade size for quality: all-MiniLM-L12-v2 is slightly larger, and all-mpnet-base-v2 is the largest of the family and scores higher on retrieval benchmarks. Newer small models such as bge-small and gte-small score a few points higher on the MTEB leaderboard, so test them if retrieval quality matters more than speed.
Chroma suits prototypes and small deployments. It runs in-process or as a server, and it persists to disk. For larger collections, consider pgvector inside Postgres, Qdrant or Milvus. The pipeline code barely changes, since LangChain wraps each store behind the same retriever interface. Pick the store that fits your existing infrastructure, not the one with the most features.
Pitfalls that break first pipelines
Chunks that are too large. A 2,000-character chunk may exceed the embedding model's 256 word-piece limit. The model truncates silently, and the stored vector represents only the start of the chunk. Keep chunks short and check the token count, not just the character count.
Chunks that are too small. A single sentence rarely carries enough context to answer a question. The retriever finds the sentence, but the model cannot tell what it refers to. Aim for chunks that hold a complete idea, usually a paragraph or two.
No metadata. Without source file names and page numbers, you cannot trace an answer back to its document. Add metadata at load time. It costs nothing and saves hours during debugging.
Retrieving too few chunks. One chunk is rarely enough. Retrieve four or five, and let the model ignore the irrelevant ones. Retrieve too many and the context fills with noise, which raises cost and lowers accuracy.
Trusting the model over the context. A language model will answer confidently even when the retrieved chunks say nothing useful. The prompt must tell it to say it does not know. Test this case deliberately with questions outside your documents.
Skipping evaluation. A pipeline that works on five test questions can fail on the sixth. Keep a fixed set of test questions and re-run them after every change to chunking, the model or the prompt. A small regression test beats a long debugging session.
Open-source versus hosted embeddings
Open-source embeddings cost nothing per query and keep your documents on your own machines. That matters for private data and for teams with strict data rules. The trade-off is quality: paid APIs such as OpenAI's text-embedding models score higher out of the box, and they need no local setup. A practical path is to prototype with all-MiniLM-L6-v2, measure retrieval quality on your test set, and switch only if the numbers justify it.
Bottom line
Build the pipeline in small steps and test retrieval before you test answers. Use all-MiniLM-L6-v2 and Chroma to start: both are free, open source and well documented. Keep chunks near 1,000 characters with overlap, stay under the 256 word-piece limit, attach metadata to every chunk, and retrieve four or five chunks per query. The most common failure is not the model. It is chunking that hides the answer the model needed.