liveBook · Manning

RecursiveCharacterTextSplitter

As explained in MongoDB 8.0 in Action, Third Edition

RecursiveCharacterTextSplitter is a LangChain text-splitting component that recursively divides text into smaller character-based chunks.

RecursiveCharacterTextSplitter divides loaded document text into smaller character-based chunks for embedding and retrieval workflows. In the MongoDB Atlas pipeline, it runs after PyPDFLoader and before OpenAIEmbeddings. section 14.5.2

Configure the splitter around the embedding workload:

  • Set chunk_size=500 to create 500-character chunks. section 14.5.2
  • Set chunk_overlap=0 when the ingestion flow requires no repeated characters between chunks. section 14.5.2
  • Pass the loaded documents to split_documents before generating embeddings and inserting them into MongoDB Atlas. section 14.5.2

Embedding-ingestion configuration

from langchain.text_splitter import RecursiveCharacterTextSplitter text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0) docs = text_splitter.split_documents(data) section 14.5.2

import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from pymongo import MongoClient

MONGO_URI = os.environ["MONGO_URI"]

# Note that if you change this, you also
➥need to change it in `rag_mongo/chain.py`
DB_NAME = "langchain" #1
COLLECTION_NAME = "mongodb" #2
ATLAS_VECTOR_SEARCH_INDEX_NAME = "default" #3
EMBEDDING_FIELD_NAME = "embedding"
client = MongoClient(MONGO_URI)
db = client[DB_NAME]
MONGODB_COLLECTION = db[COLLECTION_NAME]

if __name__ == "__main__":
    # Load docs
    loader = PyPDFLoader("https://mng.bz/PwxP") #4
    data = loader.load()

    # Split docs
    text_splitter = RecursiveCharacterTextSplitter
➥(chunk_size=500, chunk_overlap=0)
    docs = text_splitter.split_documents(data)

    # Insert the documents in MongoDB Atlas Vector Search
    _ = MongoDBAtlasVectorSearch.from_documents(
        documents=docs,
        embedding=OpenAIEmbeddings(disallowed_special=()),
        collection=MONGODB_COLLECTION,
        index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
    )

Inspect the complete ingestion listing, including PDF loading, splitting, embeddings, MongoDB connection, and vector-search insertion. section 14.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines chunk size and overlap and shows document splitting, but does not explain the recursive algorithm internally.
Examples
Uses a MongoDB documentation PDF, 500-character chunks, zero overlap, and embedding insertion.
What it assumes
Assumes a document-loading and embedding pipeline using LangChain components.
What you can do after
Build it from scratch by configuring a splitter and passing loaded documents into an embedding-ingestion pipeline.
How it connects
Connects document loaders, embeddings, vector stores, and MongoDB Atlas.

Also explained in

Compare how 4 books explain RecursiveCharacterTextSplitter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.