A production-grade Retrieval-Augmented Generation (RAG) platform engineered for grounded querying across multi-page PDF documents and live web pages. The platform pairs a Next.js 15 frontend with a Python FastAPI retrieval backend, utilizing Google Gemini 2.5 Flash, Pinecone serverless vector storage, real-time security guardrails, and automated RAG Triad evaluation metrics.
Standard large language models struggle with dense factual domain documents, often producing hallucinations or unsupported inferences. PDF AI implements a multi-stage retrieval architecture with context spotlighting, document grading, and automated output verification, ensuring that every generated answer is strictly anchored to uploaded document content with precise page and chunk citations.
To protect against adversarial inputs and unintended data exposure, the platform features a multi-tiered guardrails layer. Input guardrails actively detect and intercept prompt injection and jailbreak attempts before database or LLM execution, while output guardrails verify factual alignment against retrieved passages and redact sensitive PII patterns.
Rather than relying on subjective review, each query response computes objective RAG Triad evaluation metrics in real time:
- Faithfulness (Groundedness): Quantifies the percentage of claims directly backed by document context.
- Answer Relevancy: Evaluates how concisely and directly the response answers the specific prompt.
- Context Precision: Measures the signal-to-noise ratio in retrieved vector chunks.
Users can ingest multiple dense PDF files and live web documentation simultaneously. By vectorizing content into isolated namespace partitions, the system allows complex cross-document queries, comparative analysis, and instant knowledge synthesis across hundreds of pages in sub-second response times.
Enterprise and research workflows demand stringent confidentiality. PDF AI features a client-side API Key Vault that stores personal Pinecone and Google Gemini keys in local browser storage. Keys are transmitted only in ephemeral request headers directly to processing services, ensuring zero persistent server-side credential storage or third-party telemetry.
flowchart TD
subgraph INGESTION["1. Document and Web Ingestion"]
PDF["Multi-Page PDFs"] --> PyPDF["PyPDF Parser"]
WEB["Live Web URLs"] --> Traf["Trafilatura and BeautifulSoup4"]
PyPDF --> Chunk["RecursiveCharacterTextSplitter (1000 chars, 150 overlap)"]
Traf --> Chunk
end
subgraph VECTOR_STORE["2. Vector Embedding and Storage"]
Chunk --> BatchEmb["Gemini 768-dim Batch Embeddings (gemini-embedding-001)"]
BatchEmb --> Pinecone[("Pinecone Serverless Vector Database (Namespaces: col-*, web-*)")]
end
subgraph GUARDRAILS_INPUT["3. Input Security Guardrails"]
UserQuery["User Query"] --> GuardIn["Input Guardrail: Jailbreak, Prompt Injection and PII Audit"]
end
subgraph SOTA_PIPELINE["4. 6-Stage Advanced Retrieval Engine"]
GuardIn --> HyDE["Stage 01: HyDE Expansion (Hypothetical Passage)"]
HyDE --> Dense["Stage 02A: Dense 768-dim Search"]
GuardIn --> Sparse["Stage 02B: Lexical BM25 Scoring"]
Dense & Sparse --> RRF["Stage 03: Reciprocal Rank Fusion (k=60)"]
RRF --> CrossEnc["Stage 04: Cross-Encoder Re-Ranking"]
CrossEnc --> CRAG["Stage 05: CRAG Document Grader"]
CRAG --> Spotlight["Stage 06: L8 Context Spotlighting"]
end
subgraph SYNTHESIS["5. Grounded Synthesis and Verification"]
Spotlight --> Gemini["Google Gemini 2.5 Flash (Multi-Model Failover)"]
Gemini --> GuardOut["Output Guardrail: Hallucination and Safety Audit"]
GuardOut --> Eval["RAG Triad Evaluator: Faithfulness, Relevance, Precision"]
Eval --> Response["Grounded Response with Inline Citations and Quality Badge"]
end
- Input Security Guardrail: Scans queries for prompt injection patterns, system prompt overrides, and PII before executing vector retrieval.
- HyDE (Hypothetical Document Embeddings): Generates a speculative expert passage to capture semantic intent, eliminating vocabulary mismatches between user queries and raw document text.
- Hybrid Retrieval (Dense + Sparse): Concurrently computes 768-dimensional cosine similarity vectors and BM25 lexical term scores to balance semantic depth with exact keyword matching.
-
Reciprocal Rank Fusion (RRF
$k=60$ ): Synthesizes multiple rank positions into a single unified score using the formula:$$RRF(d) = \sum_{m \in M} \frac{1}{60 + r_m(d)}$$ - Cross-Encoder Semantic Re-Ranking: Performs deep cross-attention evaluations over top candidate pairs to re-score relevance with high precision.
-
Corrective RAG (CRAG) Document Grader: Classifies passages into
CORRECT,AMBIGUOUS, orINCORRECT, discarding non-relevant chunks before synthesis. - L8 Context Spotlighting: Encloses verified factual chunks in strict spotlight boundaries, guiding the generator to cite source files, page numbers, and exact quotes.
- Output Guardrails and Triad Evaluation: Performs post-generation hallucination checks and calculates real-time Faithfulness, Answer Relevancy, and Context Precision scores.
| Layer | Technology | Purpose |
|---|---|---|
| Frontend Framework | Next.js 15 (App Router), React 19 | Server and client rendering, route handlers, dynamic layout architecture |
| Language | TypeScript, Python 3.10+ | End-to-end type safety and backend computational efficiency |
| Styling & UI | Tailwind CSS v4, Radix UI Primitives, Lucide Icons | Responsive glassmorphic interface, dark/light theme tokens |
| Backend Framework | FastAPI, Uvicorn, Pydantic | High-performance asynchronous REST endpoints and Server-Sent Events |
| Vector Database | Pinecone Serverless (768 dimensions) | Scalable vector indexing with isolated namespace partitioning |
| LLM & Embeddings | Google Gemini 2.5 Flash, Gemini Embedding | Sub-second generative answers, semantic embeddings, quota failover |
| Guardrails & Evaluation | Custom Security Shield, RAG Triad Evaluator | Real-time jailbreak defense, hallucination interception, triad metrics |
| Parsing & Ingestion | PyPDF, Trafilatura, BeautifulSoup4 | Multi-page PDF extraction and live web scraping |
| Authentication | NextAuth.js, Google OAuth 2.0, Credentials | Secure JWT sessions, PBKDF2 SHA-512 password hashing |
| State & Data | TanStack React Query, Sonner | Optimistic mutations, real-time toast notifications, cache management |
| Deployment | Vercel (Frontend), Render (Backend), Docker | Automated continuous integration and containerized hosting |
- Node.js:
v20.xorv22.x - Python:
3.10or higher - Package Managers:
npmandpip - API Keys: Google AI Studio (Gemini) and Pinecone
git clone https://github.com/Pradeepks01/PDF-AI.git
cd PDF-AIPINECONE_API_KEY=pcsk_your_pinecone_api_key
PINECONE_INDEX_NAME=pdf-web-rag
GEMINI_API_KEY=your_google_gemini_api_key
PORT=8000
HOST=0.0.0.0NEXT_PUBLIC_API_URL=http://localhost:8000
NEXTAUTH_SECRET=your_nextauth_secret_key_minimum_32_characters
NEXTAUTH_URL=http://localhost:3000
GOOGLE_CLIENT_ID=your_google_oauth_client_id
GOOGLE_CLIENT_SECRET=your_google_oauth_client_secret
DATABASE_URL=file:./dev.db- Navigate to the backend directory:
cd python_backend - Create and activate a virtual environment:
# Windows python -m venv venv .\venv\Scripts\activate # macOS / Linux python3 -m venv venv source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Start the development server:
The backend API will be available at
python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload
http://127.0.0.1:8000(Interactive Swagger Docs:http://127.0.0.1:8000/docs).
- Open a new terminal and navigate to the frontend directory:
cd frontend - Install dependencies:
npm install
- Generate Prisma client:
npx prisma generate
- Start the development server:
The application will be accessible at
npm run dev
http://localhost:3000.
- Import the repository into Vercel.
- Set the Root Directory to
frontend. - Configure the build settings:
- Framework Preset: Next.js
- Build Command:
prisma generate && next build - Output Directory:
.next
- Add the production environment variables in the Vercel dashboard:
NEXT_PUBLIC_API_URL(URL of the deployed FastAPI backend, e.g.,https://pdf-ai-xckr.onrender.com)NEXTAUTH_URL(Production frontend URL, e.g.,https://pdf-ai-puce.vercel.app)NEXTAUTH_SECRETGOOGLE_CLIENT_IDGOOGLE_CLIENT_SECRET
- Click Deploy.
- Create a new Web Service on Render linked to the repository.
- Set the Root Directory to
python_backend. - Configure runtime settings:
- Environment: Python 3
- Build Command:
pip install -r requirements.txt - Start Command:
uvicorn main:app --host 0.0.0.0 --port $PORT
- Add environment variables:
PINECONE_API_KEYPINECONE_INDEX_NAMEGEMINI_API_KEYPYTHON_VERSION=3.11.0
- Deploy the service and note the public URL.
Run the complete stack locally using Docker Compose:
docker-compose up --buildTo run in detached mode:
docker-compose up -d --build| Method | Endpoint | Description |
|---|---|---|
GET |
/api/health |
Verifies service health, active vector database, and primary LLM status. |
POST |
/api/upload |
Ingests PDF documents, extracts text, generates 768-dim embeddings, and upserts vectors into Pinecone. |
POST |
/api/web/crawl |
Crawls target URL, extracts markdown, embeds text chunks, and indexes into Pinecone. |
POST |
/api/chat |
Executes the 6-stage retrieval pipeline with guardrails and returns a grounded answer with citations and triad scores. |
POST |
/api/chat/stream |
Streams AI response tokens in real-time via Server-Sent Events (SSE). |
DELETE |
/api/namespace/{ns} |
Purges all indexed vector embeddings within the specified namespace. |
| Method | Endpoint | Description |
|---|---|---|
POST |
/api/auth/[...nextauth] |
Handles Google OAuth and Credentials session management. |
POST |
/api/auth/reset-password |
Resets user password using PBKDF2 SHA-512 salted hashing. |
GET |
/api/python-health |
Server-side proxy check for Python backend availability. |
This project is licensed under the MIT License.