File size: 8,712 Bytes
76b2481
dac03af
 
 
 
 
 
76b2481
dac03af
 
 
 
 
 
 
76b2481
 
dac03af
76b2481
dac03af
76b2481
dac03af
 
 
76b2481
dac03af
 
 
 
 
76b2481
 
 
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b2481
 
dac03af
76b2481
dac03af
76b2481
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b2481
dac03af
76b2481
dac03af
76b2481
dac03af
 
 
76b2481
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b2481
 
dac03af
 
 
 
 
76b2481
dac03af
 
 
76b2481
dac03af
76b2481
dac03af
76b2481
dac03af
76b2481
dac03af
 
 
76b2481
dac03af
 
76b2481
dac03af
76b2481
dac03af
76b2481
dac03af
76b2481
dac03af
 
 
 
 
 
76b2481
dac03af
76b2481
dac03af
76b2481
dac03af
 
76b2481
dac03af
 
 
76b2481
 
dac03af
 
 
 
 
 
76b2481
 
dac03af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76b2481
dac03af
 
 
76b2481
dac03af
 
 
 
 
 
76b2481
dac03af
 
 
 
 
76b2481
dac03af
 
 
 
 
 
 
 
 
 
 
76b2481
dac03af
 
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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
title: CortexRAG Medical RAG API
emoji: 🩺
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 8000
tags:
- rag
- medical-ai
- sentence-transformers
- faiss
- fastapi
- cross-encoder
- groq
---

# 🩺 CortexRAG - Advanced Medical RAG API

**CortexRAG** is a high-performance, domain-specific Retrieval-Augmented Generation (RAG) system engineered for medical and clinical question answering. It combines semantic vector search (FAISS + Sentence Transformers), cross-encoder re-ranking, and high-speed LLM inference (Groq / Llama) wrapped in a lightweight **FastAPI** REST interface.

---

## 🌟 Features

- **Semantic Embedding Engine**: `all-MiniLM-L6-v2` dense vector retrieval using FAISS indexing.
- **Precision Re-ranking**: Cross-encoder scoring (`cross-encoder/ms-marco-MiniLM-L-6-v2`) for optimal document relevance.
- **Medical Synonym Expansion**: Context-aware synonym mapping for expanded search recall.
- **Ultra-Fast REST API**: Built on FastAPI with asynchronous request handling and Pydantic validation.
- **Cloudflare Tunnel Ready**: Zero-trust public exposure without complex firewall configuration.

---

## πŸ— System Architecture

```
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚     Client Request        β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚ POST /query
                                             β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚   FastAPI Web Server      β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚                                           β”‚
                       β–Ό                                           β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚  Query Vectorization      β”‚               β”‚ Medical Synonym Expansion β”‚
         β”‚  (all-MiniLM-L6-v2)       β”‚               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                             β”‚
                       β”‚                                           β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚
                                             β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚     FAISS Vector Index    β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚ Top-N Candidate Docs
                                             β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚   Cross-Encoder Reranker  β”‚
                               β”‚  (ms-marco-MiniLM-L-6-v2) β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚ Top-K Ranked Context
                                             β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚   LLM Synthesis (Groq)    β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                             β”‚
                                             β–Ό
                               β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                               β”‚    JSON API Response      β”‚
                               β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

---

## πŸ“ Repository Structure

```
CortexRAG/
β”œβ”€β”€ API_DEPLOYMENT_PLAN.md      # Step-by-step API & tunnel setup documentation
β”œβ”€β”€ README.md                   # Hugging Face & GitHub Project Card
β”œβ”€β”€ question_embeddings.npy     # Pre-computed dense embeddings matrix
β”œβ”€β”€ questions.index             # Binary FAISS vector search index
β”œβ”€β”€ notebooks/                  # Experimental notebooks & cleaning scripts
β”‚   β”œβ”€β”€ Medical_RAG_Sytem.ipynb
β”‚   └── rag_data_cleaning.ipynb
└── rag_model/                  # Core RAG engine configurations & resources
    β”œβ”€β”€ rag_config.json         # Search, score & model parameters
    β”œβ”€β”€ requirements.txt        # Python dependency specifications
    └── models/                 # Synonyms & model metadata
        └── medical_synonyms.json
```

---

## ⚑ Quick Start & Installation

### 1. Prerequisites
- Python 3.9+
- Pip package manager

### 2. Environment Setup
```bash
# Clone repository
git clone https://huggingface.co/spaces/YOUR_USERNAME/CortexRAG
cd CortexRAG

# Create virtual environment
python -m venv venv
# Activate on Windows:
venv\Scripts\activate
# Activate on Linux/macOS:
source venv/bin/activate

# Install dependencies
pip install -r rag_model/requirements.txt fastapi uvicorn pydantic
```

### 3. Environment Variables
Set your Groq API Key (or other LLM provider keys):
```bash
# Windows PowerShell
$env:GROQ_API_KEY="your_groq_api_key_here"

# Linux/macOS
export GROQ_API_KEY="your_groq_api_key_here"
```

---

## πŸš€ Running the Local API

Start the server using `uvicorn`:

```bash
uvicorn app:app --host 127.0.0.1 --port 8000 --reload
```

Interactive API Documentation (Swagger UI) is available at:
πŸ‘‰ **`http://127.0.0.1:8000/docs`**

---

## 🌐 Exposing Publicly via Cloudflare Tunnel

To expose your local FastAPI server securely to the internet without port forwarding:

1. Download [cloudflared](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/).
2. Run the tunnel pointing to your local port:
   ```bash
   cloudflared tunnel --url http://127.0.0.1:8000
   ```
3. Use the generated URL (e.g. `https://xxx.trycloudflare.com`) as your public API endpoint.

---

## πŸ”Œ API Reference & Integration Guide

### Endpoint
`POST /query`

### Request Headers
```http
Content-Type: application/json
```

### Request Payload Example
```json
{
  "question": "What are the first-line treatments for type 2 diabetes?",
  "top_k": 6
}
```

### Response Payload Example
```json
{
  "status": "success",
  "question": "What are the first-line treatments for type 2 diabetes?",
  "answer": "First-line pharmacological management for type 2 diabetes typically includes Metformin alongside lifestyle modifications...",
  "retrieved_context": [
    {
      "doc_id": 42,
      "text": "Metformin remains the initial drug of choice for monotherapy...",
      "rerank_score": 4.85
    }
  ],
  "execution_time_sec": 0.38
}
```

### Python Integration Example
```python
import requests

url = "https://your-cloudflare-url.trycloudflare.com/query"
payload = {
    "question": "What are the common causes of chest pain?",
    "top_k": 5
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)
print(response.json())
```

---

## πŸ›  Configuration Parameters (`rag_config.json`)

| Parameter | Default | Description |
| :--- | :--- | :--- |
| `embedding_model` | `all-MiniLM-L6-v2` | SentenceTransformer embedding model |
| `reranker_model` | `cross-encoder/ms-marco-MiniLM-L-6-v2` | Precision reranking cross-encoder model |
| `retrieve_top_n` | `20` | Initial FAISS vector retrieval candidate count |
| `rerank_top_k` | `6` | Number of context snippets passed to LLM |
| `min_similarity_floor` | `0.4` | Cosine similarity threshold |

---

## πŸ“œ License
This project is released under the **MIT License**.