├── .env.example
├── .github
├── FUNDING.yml
└── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── .gitignore
├── LICENSE
├── README.md
├── assets
└── img
│ ├── pinecone.png
│ └── rag-architecture.png
├── docs
├── [1]_sources.md
├── [2]_sources.md
├── [3]_sources.md
├── [4]_sources.md
└── [5]_sources.md
├── full_basic_rag.ipynb
├── notebooks
├── [1]_rag_setup_overview.ipynb
├── [2]_rag_with_multi_query.ipynb
├── [3]_rag_routing_and_query_construction.ipynb
├── [4]_rag_indexing_and_advanced_retrieval.ipynb
└── [5]_rag_retrieval_and_reranking.ipynb
├── requirements.txt
└── test
└── langchain_turing.pdf
/.env.example:
--------------------------------------------------------------------------------
1 | # LLM Model - Get key at https://platform.openai.com/api-keys
2 | OPENAI_API_KEY=your-api-key
3 |
4 | # LangSmith - Get key at https://smith.langchain.com
5 | LANGCHAIN_TRACING_V2=true
6 | LANGCHAIN_ENDPOINT=https://api.smith.langchain.com
7 | LANGCHAIN_API_KEY=your-api-key
8 | LANGCHAIN_PROJECT=your-project-name
9 |
10 | # Pinecone Vector Database - Get key at https://app.pinecone.io
11 | PINECONE_INDEX_NAME=your-project-index
12 | PINECONE_API_HOST=your-host-url
13 | PINECONE_API_KEY=your-api-key
14 |
15 | # Cohere - Get key at https://dashboard.cohere.com/api-keys
16 | COHERE_API_KEY=your-api-key
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4 | patreon: # bRAGAI
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
12 | polar: # Replace with a single Polar username
13 | buy_me_a_coffee: bragai
14 | thanks_dev: #gh/tahababou12
15 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
16 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .env
2 | venv/
3 | .ragatouille/
4 | .DS_Store
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2025 bRAG AI
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | 1. The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | 2. If modifications or derivative works are made, proper credit must be given
16 | to the original project: **bRAG AI** (https://github.com/bragai/brag-langchain).
17 | This includes:
18 | - Clearly stating that the work is **derived from bRAG AI** in any documentation,
19 | readme files, or prominent places in the project.
20 | - Including a reference to the original repository.
21 |
22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 | FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
27 | OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 | THE SOFTWARE.
29 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Retrieval-Augmented Generation (RAG) Project
2 |
3 | **_Think it. Build it. bRAG it._ 🚀 bRAGAI's coming soon (🤫)**
4 |
5 | **[Join the waitlist](https://bragai.dev/)** for exclusive early access, be among the first to try your AI-powered full-stack development assistant, and transform ideas into production-ready web apps in minutes.
6 |
7 | ---------------------
8 |
9 | This repository contains a comprehensive exploration of Retrieval-Augmented Generation (RAG) for various applications.
10 | Each notebook provides a detailed, hands-on guide to setting up and experimenting with RAG from an introductory level to advanced implementations, including multi-querying and custom RAG builds.
11 |
12 | 
13 |
14 | ## Project Structure
15 |
16 | If you want to jump straight into it, check out the file `full_basic_rag.ipynb` -> this file will give you a boilerplate starter code of a fully customizable RAG chatbot.
17 |
18 | Make sure to run your files in a virtual environment (checkout section `Get Started`)
19 |
20 | The following notebooks can be found under the directory `notebooks/`.
21 |
22 | ### [1]\_rag_setup_overview.ipynb
23 |
24 | This introductory notebook provides an overview of RAG architecture and its foundational setup.
25 | The notebook walks through:
26 | - **Environment Setup**: Configuring the environment, installing necessary libraries, and API setups.
27 | - **Initial Data Loading**: Basic document loaders and data preprocessing methods.
28 | - **Embedding Generation**: Generating embeddings using various models, including OpenAI's embeddings.
29 | - **Vector Store**: Setting up a vector store (ChromaDB/Pinecone) for efficient similarity search.
30 | - **Basic RAG Pipeline**: Creating a simple retrieval and generation pipeline to serve as a baseline.
31 |
32 | ### [2]\_rag_with_multi_query.ipynb
33 |
34 | Building on the basics, this notebook introduces multi-querying techniques in the RAG pipeline, exploring:
35 | - **Multi-Query Setup**: Configuring multiple queries to diversify retrieval.
36 | - **Advanced Embedding Techniques**: Utilizing multiple embedding models to refine retrieval.
37 | - **Pipeline with Multi-Querying**: Implementing multi-query handling to improve relevance in response generation.
38 | - **Comparison & Analysis**: Comparing results with single-query pipelines and analyzing performance improvements.
39 |
40 | ### [3]_rag_routing_and_query_construction.ipynb
41 |
42 | This notebook delves deeper into customizing a RAG pipeline.
43 | It covers:
44 | - **Logical Routing:** Implements function-based routing for classifying user queries to appropriate data sources based on programming languages.
45 | - **Semantic Routing:** Uses embeddings and cosine similarity to direct questions to either a math or physics prompt, optimizing response accuracy.
46 | - **Query Structuring for Metadata Filters:** Defines structured search schema for YouTube tutorial metadata, enabling advanced filtering (e.g., by view count, publication date).
47 | - **Structured Search Prompting:** Leverages LLM prompts to generate database queries for retrieving relevant content based on user input.
48 | - **Integration with Vector Stores:** Links structured queries to vector stores for efficient data retrieval.
49 |
50 |
51 | ### [4]_rag_indexing_and_advanced_retrieval.ipynb
52 |
53 | Continuing from the previous customization, this notebook explores:
54 | - **Preface on Document Chunking:** Points to external resources for document chunking techniques.
55 | - **Multi-representation Indexing:** Sets up a multi-vector indexing structure for handling documents with different embeddings and representations.
56 | - **In-Memory Storage for Summaries:** Uses InMemoryByteStore for storing document summaries alongside parent documents, enabling efficient retrieval.
57 | - **MultiVectorRetriever Setup:** Integrates multiple vector representations to retrieve relevant documents based on user queries.
58 | - **RAPTOR Implementation:** Explores RAPTOR, an advanced indexing and retrieval model, linking to in-depth resources.
59 | - **ColBERT Integration:** Demonstrates ColBERT-based token-level vector indexing and retrieval, which captures contextual meaning at a fine-grained level.
60 | - **Wikipedia Example with ColBERT:** Retrieves information about Hayao Miyazaki using the ColBERT retrieval model for demonstration.
61 |
62 | ### [5]_rag_retrieval_and_reranking.ipynb
63 |
64 | This final notebook brings together the RAG system components, with a focus on scalability and optimization:
65 | - **Document Loading and Splitting:** Loads and chunks documents for indexing, preparing them for vector storage.
66 | - **Multi-query Generation with RAG-Fusion:** Uses a prompt-based approach to generate multiple search queries from a single input question.
67 | - **Reciprocal Rank Fusion (RRF):** Implements RRF for re-ranking multiple retrieval lists, merging results for improved relevance.
68 | - **Retriever and RAG Chain Setup:** Constructs a retrieval chain for answering queries, using fused rankings and RAG chains to pull contextually relevant information.
69 | - **Cohere Re-Ranking:** Demonstrates re-ranking with Cohere’s model for additional contextual compression and refinement.
70 | - **CRAG and Self-RAG Retrieval:** Explores advanced retrieval approaches like CRAG and Self-RAG, with links to examples.
71 | - **Exploration of Long-Context Impact:** Links to resources explaining the impact of long-context retrieval on RAG models.
72 |
73 | ## Getting Started
74 |
75 | ### Pre-requisites
76 |
77 | Ensure **Python 3.11.11** (preferred) is installed on your system. Follow the platform-specific instructions below to install it if not already installed.
78 |
79 | #### macOS
80 | 1. Install [Homebrew](https://brew.sh/) if not already installed:
81 | ```bash
82 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
83 | ```
84 | 2. Install Python 3.11.11:
85 | ```bash
86 | brew install python@3.11
87 | ```
88 | 3. Verify installation:
89 | ```bash
90 | python3.11 --version
91 | ```
92 |
93 | #### Linux
94 | 1. Update your package manager:
95 | ```bash
96 | sudo apt update
97 | ```
98 | 2. Install Python 3.11.11:
99 | ```bash
100 | sudo apt install python3.11 python3.11-venv
101 | ```
102 | 3. Verify installation:
103 | ```bash
104 | python3.11 --version
105 | ```
106 |
107 | #### Windows
108 | 1. Download the Python 3.11.11 installer from [Python.org](https://www.python.org/downloads/).
109 | 2. Run the installer and ensure you check the box **"Add Python to PATH"**.
110 | 3. Verify installation:
111 | ```cmd
112 | python --version
113 | ```
114 | ---
115 |
116 | ### Installation Instructions
117 |
118 | #### 1. Clone the Repository
119 | ```bash
120 | git clone https://github.com/bRAGAI/bRAG-langchain.git
121 | cd bRAG-langchain
122 | ```
123 |
124 | #### 2. Create a Virtual Environment
125 | Use Python 3.11.11 to create a virtual environment:
126 | ```bash
127 | python3.11 -m venv venv
128 | ```
129 |
130 | Activate the virtual environment:
131 | - **macOS/Linux**:
132 | ```bash
133 | source venv/bin/activate
134 | ```
135 | - **Windows**:
136 | ```cmd
137 | venv\Scripts\activate
138 | ```
139 |
140 | #### 3. Verify and Fix Python Version
141 | If the virtual environment defaults to a different Python version (e.g., Python 3.13):
142 | 1. Verify the current Python version inside the virtual environment:
143 | ```bash
144 | python --version
145 | ```
146 | 2. Use Python 3.11 explicitly within the virtual environment:
147 | ```bash
148 | python3.11
149 | ```
150 | 3. Ensure the `python` command uses Python 3.11 by creating a symbolic link:
151 | ```bash
152 | ln -sf $(which python3.11) $(dirname $(which python))/python
153 | ```
154 | 4. Verify the fix:
155 | ```bash
156 | python --version
157 | ```
158 |
159 | #### 4. Install Dependencies
160 | Install the required packages:
161 | ```bash
162 | pip install -r requirements.txt
163 | ```
164 |
165 | ---
166 |
167 | ### Additional Steps
168 |
169 | #### 5. Run the Notebooks
170 | Begin with `[1]_rag_setup_overview.ipynb` to get familiar with the setup process. Proceed sequentially through the other notebooks:
171 |
172 | - `[1]_rag_setup_overview.ipynb`
173 | - `[2]_rag_with_multi_query.ipynb`
174 | - `[3]_rag_routing_and_query_construction.ipynb`
175 | - `[4]_rag_indexing_and_advanced_retrieval.ipynb`
176 | - `[5]_rag_retrieval_and_reranking.ipynb`
177 |
178 | #### 6. Set Up Environment Variables
179 | 1. Duplicate the `.env.example` file in the root directory and rename it to `.env`.
180 | 2. Add the following keys (replace with your actual values):
181 |
182 | ```env
183 | # LLM Model - Get key at https://platform.openai.com/api-keys
184 | OPENAI_API_KEY="your-api-key"
185 |
186 | # LangSmith - Get key at https://smith.langchain.com
187 | LANGCHAIN_TRACING_V2=true
188 | LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
189 | LANGCHAIN_API_KEY="your-api-key"
190 | LANGCHAIN_PROJECT="your-project-name"
191 |
192 | # Pinecone Vector Database - Get key at https://app.pinecone.io
193 | PINECONE_INDEX_NAME="your-project-index"
194 | PINECONE_API_HOST="your-host-url"
195 | PINECONE_API_KEY="your-api-key"
196 |
197 | # Cohere - Get key at https://dashboard.cohere.com/api-keys
198 | COHERE_API_KEY=your-api-key
199 | ```
200 |
201 | ---
202 |
203 | You're now ready to use the project!
204 |
205 | ## Usage
206 |
207 | After setting up the environment and running the notebooks in sequence, you can:
208 |
209 | 1. **Experiment with Retrieval-Augmented Generation**:
210 | Use the foundational setup in `[1]_rag_setup_overview.ipynb` to understand the basics of RAG.
211 |
212 | 2. **Implement Multi-Querying**:
213 | Learn how to improve response relevance by introducing multi-querying techniques in `[2]_rag_with_multi_query.ipynb`.
214 |
215 | ## Star History
216 |
217 |
218 |
219 |
220 |
221 |
222 |
223 |
224 |
225 | ## Upcoming Notebooks
226 |
227 | 👨🏻💻 **[MistralOCR](https://mistral.ai/news/mistral-ocr) + RAG Integration**
228 |
229 | ## Contact
230 | Do you have questions or want to collaborate? Please open an issue or email Taha Ababou at taha@bragai.dev
231 |
232 | `If this project helps you, consider buying me a coffee ☕. Your support helps me keep contributing to the open-source community!`
233 |