An advanced Retrieval-Augmented Generation (RAG) system built with FastAPI, LangGraph, and Next.js. This application allows users to upload documents and chat with them using a sophisticated graph-based orchestration engine that ensures high-quality answers through grading, hallucination checks, and query rewriting.
- Graph-Based RAG: Uses
LangGraphto orchestrate a corrective RAG flow:- Retrieval: Fetches relevant context from Qdrant.
- Grading: Evaluates document relevance before generation.
- Generation: Produces answers using OpenAI LLMs.
- Hallucination Check: Verifies if the answer is grounded in documents.
- Answer Grading: Checks if the answer actually resolves the user's question.
- Query Rewriting: Optimizes queries if initial retrieval is poor.
- Dual-LLM Strategy: Uses a faster model for routing/grading and a smarter model for generation.
- Document Ingestion: Supports uploading PDF, TXT, and MD files with automatic chunking and embedding.
- Secure Authentication: JWT-based authentication with role-based access control (User vs. Admin).
- Modern Frontend: Built with Next.js 16, TypeScript, Tailwind CSS, and Shadcn/UI.
- Framework: FastAPI
- Orchestration: LangGraph, LangChain
- LLM: OpenAI (GPT-3.5/4o)
- Vector Store: Qdrant
- Database: PostgreSQL (for user data)
- Dependency Manager: Poetry
- Framework: Next.js 16 (App Router)
- UI Library: Shadcn/UI, Tailwind CSS
- State/Fetching: React Server Components, Axios
- Form Handling: React Hook Form (implied)
graph TD
Start([Start]) --> Retrieve
Retrieve --> GradeDocuments
GradeDocuments -->|Relevance High| Generate
GradeDocuments -->|Relevance Low| RewriteQuery
RewriteQuery --> Retrieve
Generate --> HallucinationCheck
HallucinationCheck -->|Grounded| AnswerCheck
HallucinationCheck -->|Hallucination| Generate
AnswerCheck -->|Useful| End([End])
AnswerCheck -->|Not Useful| RewriteQuery
- Python 3.11+
- Node.js 18+
- Docker (optional, for running Qdrant locally)
- OpenAI API Key
- Qdrant Cloud Cluster or Local Instance
-
Navigate to the backend directory:
cd backend -
Install dependencies using Poetry:
poetry install
-
Configure Environment Variables: Create a
.envfile in thebackenddirectory:OPENAI_API_KEY=sk-... QDRANT_URL=your-qdrant-url QDRANT_API_KEY=your-qdrant-key QDRANT_COLLECTION_NAME=rag_collection LLM_MODEL_FAST=gpt-3.5-turbo LLM_MODEL_SMART=gpt-4o SECRET_KEY=your_jwt_secret_key ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30 DATABASE_URL=postgresql://user:password@localhost/dbname
-
Run the Server:
poetry run uvicorn app.server:app --reload
-
Navigate to the frontend directory:
cd frontend -
Install dependencies:
npm install
-
Run the Development Server:
npm run dev
-
Access the App: Open http://localhost:3000 in your browser.
- GET /: Health check.
- POST /auth/token: Login and get JWT token.
- POST /chat: Send a message and get a RAG-based response (Protected).
- POST /ingest: Ingest raw text (Admin only).
- POST /ingest/file: Upload and ingest a file (Admin only).
This project is licensed under the Apache License 2.0.
Created by EternalKnight002 — Built to learn, optimized for scale.