# RAG Personal Resource Assistant An end-to-end Retrieval-Augmented Generation (RAG) pipeline built to extract, index, and query unstructured text data from PDF documents using **LangChain**, **Cohere LLMs**, and **ChromaDB**. This repository demonstrates how to build an AI assistant capable of performing semantic searches and answering technical questions directly from source literature (such as the original Transformers and YOLO research papers). --- ## πŸš€ How It Works The architecture is divided into two primary phases: Indexing and Querying. ``` Indexing: PDF β†’ Text Extraction β†’ Chunking β†’ Cohere Embeddings β†’ ChromaDB ↕ Querying: Question β†’ Embed β†’ Similarity Search β†’ Top-K Chunks β†’ LLM β†’ Answer ``` 1. **Indexing Phase:** * Text is parsed directly from binary raw PDFs using `pdfminer.six`. * The text is cleaned and recursively divided into manageable semantic chunks. * Chunks are translated into numerical vectors via Cohere's `embed-english-v3.0` model. * Vectors and original text metadata are indexed permanently into a local Chroma vector database. 2. **Querying Phase:** * A user submits a technical prompt or question. * The question is embedded and evaluated against the vector database using similarity metrics. * The top matching structural document chunks are injected directly into a structured prompt context window. * Cohere's `command-a-plus-05-2026` generative model reads the context and returns an isolated, context-grounded answer. --- ## πŸ› οΈ Tech Stack & Key Libraries * **Framework:** [LangChain](https://github.com/langchain-ai/langchain) β€” Manages the modular composition of pipelines (`RunnableParallel`, `RunnablePassthrough`), prompts, and chained components. * **LLM & Embeddings:** [Cohere API](https://cohere.com/) β€” Multi-stage deployment utilizing `embed-english-v3.0` for encoding syntax and `command-a-plus-05-2026` for deterministic answer parsing. * **Vector Store:** [ChromaDB](https://www.trychroma.com/) β€” Handles underlying database persistence, index management, and fast top-K similarity search scoring. * **Document Parsing:** [pdfminer.six](https://github.com/pdfminer/pdfminer.six) β€” High-level layout-aware data parsing directly from raw PDF streams. --- ## πŸ“‹ Installation & Prerequisites To run this pipeline locally or inside a cloud notebook environment, install the required core packages: ```bash pip install langchain-cohere langchain pdfminer.six chromadb langchain-community langchain-text-splitters ``` ### Environment Configuration The pipeline relies on a secure Cohere API key to initialize the embeddings engine and generative model. Set it up inside your workspace environment: ```python import os os.environ["COHERE_API_KEY"] = "your-cohere-api-key-here" ``` --- ## πŸ’» Code Architecture Overview ### 1. Document Preprocessing & Chunking Text parsed from source files is passed through a sliding window mechanism via the `RecursiveCharacterTextSplitter`. This maintains cross-boundary context by leveraging an explicit character overlap. ```python splitter = RecursiveCharacterTextSplitter(chunk_size=2048, chunk_overlap=512) docs = [ Document(page_content=chunk, metadata={"source": pdf_name}) for chunk in splitter.split_text(cleaned_text) ] ``` ### 2. Vector Index Aggregation Documents are stored within a persisted disk directory, mapping structural embeddings against unique file source origins. ```python vector_collection_fixed_size = Chroma.from_documents( documents=docs, persist_directory="/content/chroma_db", embedding=embedding ) ``` ### 3. Execution Chains & LLM Invocation Methods The system builds a declarative LangChain execution pipeline using the pipe (`|`) operator: ```python chain = retrieval | prompt | llm | output_parser ``` The system showcases three advanced invocation methods optimized for distinct application behaviors: * **Standard Invocation (`.invoke()`):** Submits a isolated question string and waits for a unified response block. * **Parallel Batching (`.batch()`):** Automatically handles concurrent inputs to process multiple distinct questions simultaneously, speeding up execution via native parallelization. * **Token Streaming (`.stream()`):** Bypasses console string buffering to flush individual response tokens out to the user interface the exact millisecond they are generated by the model. ```python # Real-time Streaming Example for chunk in chain.stream("What are the 3 vectors in Transformers architecture?"): print(chunk, flush=True, end="") ``` --- ## πŸ“„ Demo Evaluation Results The pipeline was evaluated against two baseline papers: *Attention Is All You Need* (Transformer) and *You Only Look Once* (YOLO v1). * **Target Query:** `"What is YOLO?"` * **Ground-Truth RAG Extraction Answer:** > *YOLO is a unified, end‑to‑end object detection model that uses a single convolutional network to look once at an image and directly predict multiple bounding boxes and class probabilities... designed to be extremely fast (running at 45–150 fps)...* ---