Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

46 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 Adaptive RAG β€” Intelligent Document Q&A with Authentication

Python FastAPI LangGraph Groq Streamlit License: MIT

An end-to-end Adaptive Retrieval-Augmented Generation (RAG) system with a full authentication layer. Ask questions against your uploaded documents β€” the system intelligently decides whether to search your knowledge base, answer from general knowledge, or run a live web search.


πŸ“ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Streamlit Frontend                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚  Login Page  β”‚  ────►  β”‚  Chat UI + Document Upload   β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚ JWT Bearer Token
                               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚               Auth Service (FastAPI :8080)                  β”‚
β”‚  POST /auth/register  Β·  POST /auth/login  Β·  GET /user/me  β”‚
β”‚  POST /agent/chat  ──► proxies to RAG backend               β”‚
β”‚  SQLite (users.db) Β· Argon2id Β· JWT (python-jose)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚ Internal HTTP
                               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          Adaptive RAG Backend (FastAPI :8000)               β”‚
β”‚                                                             β”‚
β”‚   POST /rag/query          POST /rag/documents/upload       β”‚
β”‚                                                             β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚   β”‚              LangGraph Workflow                      β”‚  β”‚
β”‚   β”‚                                                      β”‚  β”‚
β”‚   β”‚  Query Classifier ─► [index] ─► FAISS Retriever     β”‚  β”‚
β”‚   β”‚         β”‚                      β”‚                     β”‚  β”‚
β”‚   β”‚         β”‚                      β–Ό                     β”‚  β”‚
β”‚   β”‚         β”‚              Document Grader               β”‚  β”‚
β”‚   β”‚         β”‚              ↙           β†˜                 β”‚  β”‚
β”‚   β”‚         β”‚        Relevant      Irrelevant            β”‚  β”‚
β”‚   β”‚         β”‚            β”‚              β”‚                β”‚  β”‚
β”‚   β”‚         β”œβ”€β–Ί [general]β”‚              β–Ό                β”‚  β”‚
β”‚   β”‚         β”‚   Groq     β”‚       Query Rewriter          β”‚  β”‚
β”‚   β”‚         β”‚   Direct   β”‚              β”‚                β”‚  β”‚
β”‚   β”‚         β”‚            β”‚              β–Ό                β”‚  β”‚
β”‚   β”‚         └─► [search]─┴──────► Web Search (Tavily)   β”‚  β”‚
β”‚   β”‚                                    β”‚                 β”‚  β”‚
β”‚   β”‚                                    β–Ό                 β”‚  β”‚
β”‚   β”‚                        Generate Answer (Groq LLM)    β”‚  β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚  Gemini Embeddings (text-embedding-004) Β· FAISS Β· MongoDB   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Features

Feature Details
πŸ” Authentication JWT Bearer tokens Β· Argon2id password hashing Β· Register/Login
🧠 Adaptive Routing Classifier decides: RAG / General LLM / Web Search
πŸ“„ Document Upload PDF & TXT ingestion β†’ chunking β†’ Gemini embedding β†’ FAISS
🌐 Web Search Fallback Tavily API for real-time information
πŸ’¬ Chat History Per-user session persistence in MongoDB
⚑ Groq-Powered Llama 3.3 70B (Groq) for LLM inference · text-embedding-004 for embeddings
πŸ–₯️ Streamlit UI Glassmorphism dark theme with login, chat, and file upload

πŸ“ Repository Structure

Adaptive-Rag/
β”œβ”€β”€ adaptive_rag/                  # RAG backend + Streamlit frontend
β”‚   β”œβ”€β”€ main.py                    # FastAPI entry point (:8000)
β”‚   β”œβ”€β”€ requirements.txt
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/routes.py          # /rag/query, /rag/documents/upload
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”‚   β”œβ”€β”€ settings.py        # Environment config (Pydantic)
β”‚   β”‚   β”‚   └── prompts.yaml       # LLM prompt templates
β”‚   β”‚   β”œβ”€β”€ llms/groq_llm.py       # Groq LLM client + Gemini embeddings
β”‚   β”‚   β”œβ”€β”€ memory/chat_history_mongo.py
β”‚   β”‚   β”œβ”€β”€ models/                # State, Grade, RouteIdentifier schemas
β”‚   β”‚   └── rag/
β”‚   β”‚       β”œβ”€β”€ graph_builder.py   # Full LangGraph workflow
β”‚   β”‚       β”œβ”€β”€ retriever_setup.py # FAISS vector store
β”‚   β”‚       └── document_upload.py # File ingestion pipeline
β”‚   └── streamlit_app/
β”‚       β”œβ”€β”€ Home.py                # Login / Register page
β”‚       β”œβ”€β”€ pages/chat.py          # Protected chat UI
β”‚       └── utils/api_client.py    # HTTP client (JWT Bearer)
β”‚
β”œβ”€β”€ auth_service/                  # Authentication BFF (migrated from Rust)
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py                # FastAPI entry point (:8080)
β”‚   β”‚   β”œβ”€β”€ auth.py                # JWT + Argon2id
β”‚   β”‚   β”œβ”€β”€ database.py            # Async SQLite (aiosqlite)
β”‚   β”‚   β”œβ”€β”€ models.py              # SQLAlchemy User model
β”‚   β”‚   β”œβ”€β”€ schemas.py             # Pydantic request/response schemas
β”‚   β”‚   β”œβ”€β”€ dependencies.py        # JWT bearer dependency
β”‚   β”‚   └── routers/
β”‚   β”‚       β”œβ”€β”€ auth.py            # POST /auth/register, /auth/login
β”‚   β”‚       β”œβ”€β”€ user.py            # GET /user/me
β”‚   β”‚       └── agent.py           # POST /agent/chat (proxy)
β”‚   └── requirements.txt
β”‚
β”œβ”€β”€ .env.example                   # Environment variables template
β”œβ”€β”€ .gitignore
└── README.md

Note: rename src/llms/gemini.py β†’ src/llms/groq_llm.py (or keep the filename and just swap internals) to match β€” update if you named it differently.


πŸš€ Quick Start (Local)

Prerequisites

1. Clone the repository

git clone https://github.com/itsmehotpants/Adaptive-Rag.git
cd Adaptive-Rag

2. Set up environment variables

cp .env.example .env
# Edit .env and fill in your GROQ_API_KEY, GEMINI_API_KEY (embeddings), and TAVILY_API_KEY

3. Start the Auth Service

cd auth_service
python -m venv venv
venv\Scripts\activate          # Windows
# source venv/bin/activate     # macOS/Linux

pip install -r requirements.txt
uvicorn app.main:app --host 0.0.0.0 --port 8080 --reload

Auth service will be available at: http://localhost:8080 API docs: http://localhost:8080/docs

4. Start the RAG Backend

cd adaptive_rag
python -m venv venv
venv\Scripts\activate

pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 8000 --reload

RAG backend at: http://localhost:8000 API docs: http://localhost:8000/docs

5. Start the Streamlit Frontend

cd adaptive_rag/streamlit_app
streamlit run Home.py

Frontend at: http://localhost:8501


πŸ”‘ Authentication Flow

1. Register:  POST /auth/register  { username, password }
2. Login:     POST /auth/login     { username, password }  β†’  { access_token }
3. Use token: Authorization: Bearer <access_token>
4. Chat:      POST /agent/chat     { query }  (proxied to RAG with session_id)

🌐 Deployment

Option A: Streamlit Community Cloud (Frontend)

Note: Streamlit Community Cloud runs only the Streamlit app. You need the backend services deployed separately.

  1. Push your code to GitHub (already done).
  2. Go to share.streamlit.io.
  3. Click New app β†’ Select itsmehotpants/Adaptive-Rag.
  4. Set Main file path: adaptive_rag/streamlit_app/Home.py
  5. Under Advanced β†’ Secrets, add:
AUTH_SERVICE_URL = "https://your-auth-service.onrender.com"
RAG_SERVICE_URL = "https://your-rag-backend.onrender.com"
  1. Click Deploy.

Option B: Render.com (Backend Services β€” Free Tier)

Deploy Auth Service

  1. Go to render.com β†’ New Web Service.
  2. Connect GitHub repo β†’ Select itsmehotpants/Adaptive-Rag.
  3. Settings:
  • Root Directory: auth_service
  • Build Command: pip install -r requirements.txt
  • Start Command: uvicorn app.main:app --host 0.0.0.0 --port $PORT
  1. Add environment variables: JWT_SECRET, ACCESS_TOKEN_EXPIRE_MINUTES=60

Deploy RAG Backend

  1. New Web Service β†’ same repo.
  2. Settings:
  • Root Directory: adaptive_rag
  • Build Command: pip install -r requirements.txt
  • Start Command: uvicorn main:app --host 0.0.0.0 --port $PORT
  1. Add environment variables: GROQ_API_KEY, GEMINI_API_KEY, TAVILY_API_KEY, MONGODB_URL

Option C: Railway.app

# Install Railway CLI
npm install -g @railway/cli
railway login

# Deploy auth service
cd auth_service
railway up

# Deploy RAG backend
cd ../adaptive_rag
railway up

πŸ”§ API Reference

Auth Service (:8080)

Method Endpoint Auth Description
POST /auth/register ❌ Create new user account
POST /auth/login ❌ Login and receive JWT token
GET /user/me βœ… JWT Get current user profile
POST /agent/chat βœ… JWT Proxy chat to RAG backend
GET /health ❌ Health check

RAG Backend (:8000)

Method Endpoint Description
POST /rag/query Run adaptive RAG query
POST /rag/documents/upload Upload & index document
GET / Health check

πŸ› οΈ Tech Stack

Layer Technology
LLM Groq β€” llama-3.3-70b-versatile
Embeddings Google text-embedding-004
RAG Orchestration LangGraph 0.5
Vector Store FAISS (in-memory)
Web Search Tavily API
Chat History MongoDB
Auth Backend FastAPI + Argon2id + JWT
Auth Database SQLite (aiosqlite)
RAG Backend FastAPI + Uvicorn
Frontend Streamlit

πŸ“ License

MIT Β© 2024 itsmehotpants

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages