🩺 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

# 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):

# 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:

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.
  2. Run the tunnel pointing to your local port:
    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

Content-Type: application/json

Request Payload Example

{
  "question": "What are the first-line treatments for type 2 diabetes?",
  "top_k": 6
}

Response Payload Example

{
  "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

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.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support