Make sure your
OPENAI_API_KEY is set in the environment before running any of the code snippets.1. Install Dependencies
2. Import Required Modules
We’ll use:- PyPDFLoader to load PDFs
- RecursiveCharacterTextSplitter for chunking
- OpenAIEmbeddings & ChatOpenAI for LLM interactions
- Chroma as our vector store
- PromptTemplate and core runnables
3. Load and Chunk the PDF
3.1 Load the PDF
3.2 Split into Overlapping Chunks
Adjust
chunk_size and chunk_overlap based on your document length and the LLM’s context window.4. Generate Embeddings & Build Vector Store
5. Configure Retriever & Formatter
Convert the vector store to a retriever and define a helper to merge relevant chunks:6. Define the LLM and Prompt Template
We’ll create a system prompt that forces the model to answer based only on retrieved context:7. Assemble the RAG Chain
Chain steps:- Retrieve relevant chunks
- Format them into a context string
- Inject into the prompt template
- Generate a factual answer
- Parse output to string
8. Query the Chain
Summary of Steps
| Step | Action | Description |
|---|---|---|
| 1 | Install & Import | Install langchain, import loaders, splitters, embeddings, etc. |
| 2 | Load PDF | Use PyPDFLoader |
| 3 | Chunk Documents | Split pages with RecursiveCharacterTextSplitter |
| 4 | Embed & Store Vectors | Generate embeddings and index with Chroma.from_documents |
| 5 | Create Retriever | Convert vector store into a retriever |
| 6 | Format Context | Define format_docs helper |
| 7 | Define Prompt & LLM | Set up PromptTemplate and ChatOpenAI |
| 8 | Build & Invoke Chain | Assemble LCEL chain and query with chain.invoke() |