Update README.md
Browse files
README.md
CHANGED
|
@@ -1,185 +1,192 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
pip
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
-
|
| 121 |
-
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
.
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
β βββ
|
| 141 |
-
βββ
|
| 142 |
-
β
|
| 143 |
-
βββ
|
| 144 |
-
β
|
| 145 |
-
βββ
|
| 146 |
-
β
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
-
|
| 174 |
-
-
|
| 175 |
-
-
|
| 176 |
-
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
sdk: gradio
|
| 3 |
+
sdk_version: 3.50.2
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# RAG_Mini
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Enterprise-Ready RAG System with Gradio Interface
|
| 12 |
+
|
| 13 |
+
This is a powerful, enterprise-grade Retrieval-Augmented Generation (RAG) system designed to transform your documents into an interactive and intelligent knowledge base. Users can upload their own documents (PDFs, TXT files), build a searchable vector index, and ask complex questions in natural language to receive accurate, context-aware answers sourced directly from the provided materials.
|
| 14 |
+
|
| 15 |
+
The entire application is wrapped in a clean, user-friendly web interface powered by Gradio.
|
| 16 |
+
|
| 17 |
+

|
| 18 |
+

|
| 19 |
+
|
| 20 |
+
## β¨ Features
|
| 21 |
+
|
| 22 |
+
- **Intuitive Web UI**: Simple, clean interface built with Gradio for uploading documents and chatting.
|
| 23 |
+
- **Multi-Document Support**: Natively handles PDF and TXT files.
|
| 24 |
+
- **Advanced Text Splitting**: Uses a `HierarchicalSemanticSplitter` that first splits documents into large parent chunks (for context) and then into smaller child chunks (for precise search), respecting semantic boundaries.
|
| 25 |
+
- **Hybrid Search**: Combines the strengths of dense vector search (FAISS) and sparse keyword search (BM25) for robust and accurate retrieval.
|
| 26 |
+
- **Reranking for Accuracy**: Employs a Cross-Encoder model to rerank the retrieved documents, ensuring the most relevant context is passed to the language model.
|
| 27 |
+
- **Persistent Knowledge Base**: Automatically saves the built vector index and metadata, allowing you to load an existing knowledge base instantly on startup.
|
| 28 |
+
- **Modular & Extensible Codebase**: The project is logically structured into services for loading, splitting, embedding, and generation, making it easy to maintain and extend.
|
| 29 |
+
|
| 30 |
+
## ποΈ System Architecture
|
| 31 |
+
|
| 32 |
+
The RAG pipeline follows a logical, multi-step process to ensure high-quality answers:
|
| 33 |
+
|
| 34 |
+
1. **Load**: Documents are loaded from various formats and parsed into a standardized `Document` object, preserving metadata like source and page number.
|
| 35 |
+
2. **Split**: The raw text is processed by the `HierarchicalSemanticSplitter`, creating parent and child text chunks. This provides both broad context and fine-grained detail.
|
| 36 |
+
3. **Embed & Index**: The child chunks are converted into vector embeddings using a `SentenceTransformer` model and indexed in a FAISS vector store. A parallel BM25 index is also built for keyword search.
|
| 37 |
+
4. **Retrieve**: When a user asks a question, a hybrid search query is performed against the FAISS and BM25 indices to retrieve the most relevant child chunks.
|
| 38 |
+
5. **Fetch Context**: The parent chunks corresponding to the retrieved child chunks are fetched. This ensures the LLM receives a wider, more complete context.
|
| 39 |
+
6. **Rerank**: A powerful Cross-Encoder model re-evaluates the relevance of the parent chunks against the query, pushing the best matches to the top.
|
| 40 |
+
7. **Generate**: The top-ranked, reranked documents are combined with the user's query into a final prompt. This prompt is sent to a Large Language Model (LLM) to generate a final, coherent answer.
|
| 41 |
+
|
| 42 |
+
```
|
| 43 |
+
[User Uploads Docs] -> [Loader] -> [Splitter] -> [Embedder & Vector Store] -> [Knowledge Base Saved]
|
| 44 |
+
|
| 45 |
+
[User Asks Question] -> [Hybrid Search] -> [Get Parent Docs] -> [Reranker] -> [LLM] -> [Answer & Sources]
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## π οΈ Tech Stack
|
| 49 |
+
|
| 50 |
+
- **Backend**: Python 3.9+
|
| 51 |
+
- **UI**: Gradio
|
| 52 |
+
- **LLM & Embedding Framework**: Hugging Face Transformers, Sentence-Transformers
|
| 53 |
+
- **Vector Search**: Faiss (from Facebook AI)
|
| 54 |
+
- **Keyword Search**: rank-bm25
|
| 55 |
+
- **PDF Parsing**: PyMuPDF (fitz)
|
| 56 |
+
- **Configuration**: PyYAML
|
| 57 |
+
|
| 58 |
+
## π Getting Started
|
| 59 |
+
|
| 60 |
+
Follow these steps to set up and run the project on your local machine.
|
| 61 |
+
|
| 62 |
+
### 1. Prerequisites
|
| 63 |
+
|
| 64 |
+
- Python 3.9 or higher
|
| 65 |
+
- `pip` for package management
|
| 66 |
+
|
| 67 |
+
### 2. Create a `requirements.txt` file
|
| 68 |
+
|
| 69 |
+
Before proceeding, it's crucial to have a `requirements.txt` file so others can easily install the necessary dependencies. In your activated terminal, run:
|
| 70 |
+
|
| 71 |
+
```bash
|
| 72 |
+
pip freeze > requirements.txt
|
| 73 |
+
```
|
| 74 |
+
This will save all the packages from your environment into the file. Make sure this file is committed to your GitHub repository. The key packages it should contain are: `gradio`, `torch`, `transformers`, `sentence-transformers`, `faiss-cpu`, `rank_bm25`, `PyMuPDF`, `pyyaml`, `numpy`.
|
| 75 |
+
|
| 76 |
+
### 3. Installation & Setup
|
| 77 |
+
|
| 78 |
+
**1. Clone the repository:**
|
| 79 |
+
```bash
|
| 80 |
+
git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY_NAME.git
|
| 81 |
+
cd YOUR_REPOSITORY_NAME
|
| 82 |
+
```
|
| 83 |
+
|
| 84 |
+
**2. Create and activate a virtual environment (recommended):**
|
| 85 |
+
```bash
|
| 86 |
+
# For Windows
|
| 87 |
+
python -m venv venv
|
| 88 |
+
.\venv\Scripts\activate
|
| 89 |
+
|
| 90 |
+
# For macOS/Linux
|
| 91 |
+
python3 -m venv venv
|
| 92 |
+
source venv/bin/activate
|
| 93 |
+
```
|
| 94 |
+
|
| 95 |
+
**3. Install the required packages:**
|
| 96 |
+
```bash
|
| 97 |
+
pip install -r requirements.txt
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
**4. Configure the system:**
|
| 101 |
+
Review the `configs/config.yaml` file. You can change the models, chunk sizes, and other parameters here. The default settings are a good starting point.
|
| 102 |
+
|
| 103 |
+
> **Note:** The first time you run the application, the models specified in the config file will be downloaded from Hugging Face. This may take some time depending on your internet connection.
|
| 104 |
+
|
| 105 |
+
### 4. Running the Application
|
| 106 |
+
|
| 107 |
+
To start the Gradio web server, run the `main.py` script:
|
| 108 |
+
|
| 109 |
+
```bash
|
| 110 |
+
python main.py
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
The application will be available at **`http://localhost:7860`**.
|
| 114 |
+
|
| 115 |
+
## π How to Use
|
| 116 |
+
|
| 117 |
+
The application has two primary workflows:
|
| 118 |
+
|
| 119 |
+
**1. Build a New Knowledge Base:**
|
| 120 |
+
- Drag and drop one or more `.pdf` or `.txt` files into the "Upload New Docs to Build" area.
|
| 121 |
+
- Click the **"Build New KB"** button.
|
| 122 |
+
- The system status will show the progress (Loading -> Splitting -> Indexing).
|
| 123 |
+
- Once complete, the status will confirm that the knowledge base is ready, and the chat window will appear.
|
| 124 |
+
|
| 125 |
+
**2. Load an Existing Knowledge Base:**
|
| 126 |
+
- If you have previously built a knowledge base, simply click the **"Load Existing KB"** button.
|
| 127 |
+
- The system will load the saved FAISS index and metadata from the `storage` directory.
|
| 128 |
+
- The chat window will appear, and you can start asking questions immediately.
|
| 129 |
+
|
| 130 |
+
**Chatting with Your Documents:**
|
| 131 |
+
- Once the knowledge base is ready, type your question into the chat box at the bottom and press Enter or click "Submit".
|
| 132 |
+
- The model will generate an answer based on the documents you provided.
|
| 133 |
+
- The sources used to generate the answer will be displayed below the chat window.
|
| 134 |
+
|
| 135 |
+
## π Project Structure
|
| 136 |
+
|
| 137 |
+
```
|
| 138 |
+
.
|
| 139 |
+
βββ configs/
|
| 140 |
+
β βββ config.yaml # Main configuration file for models, paths, etc.
|
| 141 |
+
βββ core/
|
| 142 |
+
β βββ embedder.py # Handles text embedding.
|
| 143 |
+
β βββ llm_interface.py # Handles reranking and answer generation.
|
| 144 |
+
β βββ loader.py # Loads and parses documents.
|
| 145 |
+
β βββ schema.py # Defines data structures (Document, Chunk).
|
| 146 |
+
β βββ splitter.py # Splits documents into chunks.
|
| 147 |
+
β βββ vector_store.py # Manages FAISS & BM25 indices.
|
| 148 |
+
βββ service/
|
| 149 |
+
β βββ rag_service.py # Orchestrates the entire RAG pipeline.
|
| 150 |
+
βββ storage/ # Default location for saved indices (auto-generated).
|
| 151 |
+
β βββ ...
|
| 152 |
+
βββ ui/
|
| 153 |
+
β βββ app.py # Contains the Gradio UI logic.
|
| 154 |
+
βββ utils/
|
| 155 |
+
β βββ logger.py # Logging configuration.
|
| 156 |
+
βββ assets/
|
| 157 |
+
β βββ 1.png # Screenshot of the application.
|
| 158 |
+
βββ main.py # Entry point to run the application.
|
| 159 |
+
βββ requirements.txt # Python package dependencies.
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
## π§ Configuration Details (`config.yaml`)
|
| 163 |
+
|
| 164 |
+
You can customize the RAG pipeline by modifying `configs/config.yaml`:
|
| 165 |
+
|
| 166 |
+
- **`models`**: Specify the Hugging Face models for embedding, reranking, and generation.
|
| 167 |
+
- **`vector_store`**: Define the paths where the FAISS index and metadata will be saved.
|
| 168 |
+
- **`splitter`**: Control the `HierarchicalSemanticSplitter` behavior.
|
| 169 |
+
- `parent_chunk_size`: The target size for larger context chunks.
|
| 170 |
+
- `parent_chunk_overlap`: The overlap between parent chunks.
|
| 171 |
+
- `child_chunk_size`: The target size for smaller, searchable chunks.
|
| 172 |
+
- **`retrieval`**: Tune the retrieval and reranking process.
|
| 173 |
+
- `retrieval_top_k`: How many initial candidates to retrieve with hybrid search.
|
| 174 |
+
- `rerank_top_k`: How many final documents to pass to the LLM after reranking.
|
| 175 |
+
- `hybrid_search_alpha`: The weighting between vector search (`alpha`) and BM25 search (`1 - alpha`). `1.0` is pure vector search, `0.0` is pure keyword search.
|
| 176 |
+
- **`generation`**: Set parameters for the final answer generation, like `max_new_tokens`.
|
| 177 |
+
|
| 178 |
+
## π£οΈ Future Roadmap
|
| 179 |
+
|
| 180 |
+
- [ ] Support for more document types (e.g., `.docx`, `.pptx`, `.html`).
|
| 181 |
+
- [ ] Implement response streaming for a more interactive chat experience.
|
| 182 |
+
- [ ] Integrate with other vector databases like ChromaDB or Pinecone.
|
| 183 |
+
- [ ] Create API endpoints for programmatic access to the RAG service.
|
| 184 |
+
- [ ] Add more advanced logging and monitoring for enterprise use.
|
| 185 |
+
|
| 186 |
+
## π€ Contributing
|
| 187 |
+
|
| 188 |
+
Contributions are welcome! If you have ideas for improvements or find a bug, please feel free to open an issue or submit a pull request.
|
| 189 |
+
|
| 190 |
+
## π License
|
| 191 |
+
|
| 192 |
+
This project is licensed under the MIT License. See the `LICENSE` file for details.
|