Splitting documents into chunks for a RAG system
Preparing text for retrieval with overlapping chunks.
Goal: Building the retrieval stage of RAG and running it locally.
from haystack import Document, Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
store = InMemoryDocumentStore()
store.write_documents([
Document(content="MONAI framework for medical imaging deep learning", meta={"lib": "monai"}),
Document(content="Ultralytics YOLO for real-time object detection", meta={"lib": "ultralytics"}),
Document(content="PennyLane for quantum machine learning and differentiable circuits", meta={"lib": "pennylane"}),
])
pipe = Pipeline()
pipe.add_component("retriever", InMemoryBM25Retriever(document_store=store, top_k=1))
for q in ["medical imaging segmentation", "quantum circuits"]:
doc = pipe.run({"retriever": {"query": q}})["retriever"]["documents"][0]
print(q, "->", doc.meta["lib"], round(doc.score, 3))
We store the documents in an InMemoryDocumentStore and use a BM25 retriever inside a Pipeline. This part needs no language model; to complete RAG, a PromptBuilder and a generator (local or through an API) are added after the retriever.
medical imaging segmentation -> monai 2.481 quantum circuits -> pennylane 2.399
This example was actually run and tested on 21 November 2026 on a CPU — haystack-ai 3.5.0, python 3.13.18
Preparing text for retrieval with overlapping chunks.
Understanding the first step of LlamaIndex indexing before embedding.
Sections
No section has this name. Press “Search” to search all site content.
Favourites
My results & certificates
My academy progressMy quiz resultsKids’ progressAcademy certificatesQuiz certificatesStart any lesson or quiz and it will show up here so you can get back to it quickly.
Settings
I am the AI Frameworks & Libraries Assistant. Describe your project or ask me about a library, a comparison or a code example, and I will answer from the encyclopedia's database with links and sources.
Answers from the encyclopedia's database only, without a language model and without sending your question to any third party.
The assistant can make mistakes, so check the attached sources.