PyPDFLoader loads a PDF document for downstream processing in a retrieval-augmented generation pipeline. The loaded pages are split into chunks, embedded, and inserted into MongoDB Atlas for vector search. section 14.5.2
A practical ingestion flow uses PyPDFLoader alongside:
- RecursiveCharacterTextSplitter, which divides the loaded document into 500-character chunks with no overlap. section 14.5.2
- OpenAIEmbeddings, which generates vectors for each chunk. section 14.5.2
- MongoDBAtlasVectorSearch, which inserts the processed chunks and makes them searchable. section 14.5.2
Load a PDF for ingestion
Instantiate PyPDFLoader with a PDF URL, then call loader.load() to obtain document data: loader = PyPDFLoader("https://mng.bz/PwxP"); data = loader.load(). 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, )
View the surrounding ingestion script showing PDF loading, chunking, embedding, and MongoDB connection setup. section 14.5.2
