File size: 4,563 Bytes
533a593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
# Document-Based RAG AI

This project implements a Retrieval-Augmented Generation (RAG) architecture to extract and retrieve information from uploaded documents and answer user queries using a chat interface. The application uses a Flask-based web interface and a Chroma vector database for document indexing and retrieval.

---

## Problem Statement

Organizations often struggle to manage and query unstructured textual data spread across various documents. This application provides an efficient solution by creating a searchable vector database of document contents, enabling precise query-based retrieval and response generation.

---

## Setup

### 1. Virtual Environment
Set up a Python virtual environment to manage dependencies:
```bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

### 2. Install Dependencies
Install required packages from the `requirements.txt` file:
```bash
pip install -r requirements.txt
```

### 3. Running the Application
Start the Flask application:
```bash
python app.py
```
  
---

## Components Overview

### 1. [LangChain](https://github.com/hwchase17/langchain)
LangChain enables seamless integration of LLMs with retrieval systems like vector databases.

### 2. [Flask](https://flask.palletsprojects.com/)
Flask provides the web framework to build the user interface and RESTful APIs.

### 3. [Chroma Vector Database](https://docs.trychroma.com/)
Chroma is used to store and retrieve document embeddings for similarity-based querying.

### 4. RAG Architecture
Combines retrieval of relevant document chunks with LLMs to generate precise responses based on context.

### 5. Models Used
  - **Embedding Model:** `all-MiniLM-L6-v2` (via HuggingFace)
  - **Chat Model:** `Mistral-7B-Instruct-v0.3`

---

## Application Workflow

### Overview
A typical RAG application has two components:
1. **Indexing**: Processes and indexes documents for searchability.
2. **Retrieval and Generation**: Retrieves relevant document chunks and generates a context-based response.

#### Indexing
1. **Load**: Upload documents using the web interface.
2. **Split**: Break documents into smaller chunks for indexing.
3. **Store**: Save embeddings in a Chroma vector database.

#### Retrieval and Generation
1. **Retrieve**: Search for relevant chunks based on user queries.
2. **Generate**: Produce context-aware answers using the Chat Model.

---

## Application Features

1. **Create Database**
   - Upload documents and generate a searchable vector database.

2. **Update Database**
   - Update the vector database by adding new document.

3. **Remove Database**
   - Remove the vector database.

4. **Delete Documents in Database**
   - Delete any specific document in the vector database.

5. **List Databases**
   - View all available vector databases.

6. **Chat Interface**
   - Select a vector database and interact via queries.

---

## App Tree

```
.
β”œβ”€β”€ app.py                    # Flask application
β”œβ”€β”€ retrival.py               # Data retrieval and vector database management
β”œβ”€β”€ templates/
β”‚   β”œβ”€β”€ home.html             # Home page template
β”‚   β”œβ”€β”€ chat.html             # Chat interface template
β”‚   β”œβ”€β”€ create_db.html        # Upload documents for database creation
β”‚   β”œβ”€β”€ list_dbs.html         # List available vector databases
β”œβ”€β”€ uploads/                  # Uploaded document storage
β”œβ”€β”€ VectorDB/                 # Vector database storage
β”œβ”€β”€ TableDB/                  # Table database storage
β”œβ”€β”€ ImageDB/                  # Image database storage
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ .env                      # Environment variables (e.g., HuggingFace API key)
└── README.md                 # Documentation
```

---

## Example Use Using the flask

1. **Navigate to `/create-db`** to upload documents and generate a vector database (via Flask) 
2. **Navigate to `/list-db`**  to view all available databases.
3. **Select a database** using `/select-db/<db_name>` (Flask).
4. **Query a database** using `/chat` (Flask) to retrieve relevant information and generate a response.
5. **Update a database** using `/update-dbs/<db_name>` (Flask) to update db by adding the files or even whole folder containing the files.
6. **Remove a database** using `/remove-dbs/<db_name>` (Flask) to remove or delete entire database.
7. **Delete document in database** using `/delete-doc/<db_name>` (Flask) to delete a specific document in the database.

---

Happy experimenting! πŸš€