Splitting documents into chunks for a RAG system
Preparing text for retrieval with overlapping chunks.
Goal: Understanding the first step of LlamaIndex indexing before embedding.
from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter
docs = [Document(text="PyTorch إطار تعلم عميق. " * 30, metadata={"source": "pytorch.md"}),
Document(text="MONAI للصور الطبية. " * 30, metadata={"source": "monai.md"})]
nodes = SentenceSplitter(chunk_size=64, chunk_overlap=8).get_nodes_from_documents(docs)
print("Number of nodes:", len(nodes))
print("Source of the first node:", nodes[0].metadata["source"])
print("The first node has a next node:", nodes[0].next_node is not None)
SentenceSplitter turns Document objects into nodes while keeping metadata and the relationships between adjacent nodes. The nodes are then embedded in a VectorStoreIndex, a step that needs an embedding model (local or through an API), so we did not run it here.
Number of nodes: 18 Source of the first node: pytorch.md The first node has a next node: True
This example was actually run and tested on 21 November 2026 on a CPU — llama-index-core 0.14.26, python 3.13.18
Preparing text for retrieval with overlapping chunks.
Building the retrieval stage of RAG and running it locally.
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.