Skip to content

vedantCE/Docsy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

33 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

HackNexus AI Document Assistant - Backend

Production-ready FastAPI backend for document Q&A using RAG (Retrieval-Augmented Generation) with Gemini AI.

Features

  • πŸ“„ Document Upload: PDF and DOCX support with validation
  • πŸ” Semantic Search: FAISS vector database for fast similarity search
  • πŸ€– AI-Powered Q&A: Gemini 2.5 Flash for accurate, grounded answers
  • 🎯 Source Citations: Returns page numbers and snippets
  • πŸ”’ Error Handling: Comprehensive validation and error responses
  • 🌐 CORS Enabled: Ready for frontend integration

Tech Stack

  • Framework: FastAPI + Uvicorn
  • AI: Google Gemini 2.0 Flash (via google-genai SDK)
  • Vector DB: FAISS for embedding storage
  • Embeddings: Sentence Transformers (all-MiniLM-L6-v2)
  • Document Parsing: PyPDF2, python-docx

Setup Instructions

1. Install Dependencies

cd backend
pip install -r requirements.txt

2. Configure Environment

Create a .env file in the backend directory:

GEMINI_API_KEY=your_gemini_api_key_here

Get your API key from: https://aistudio.google.com/apikey

3. Run the Server

uvicorn app.main:app --reload --env-file .env

The API will be available at: http://localhost:8000

API Endpoints

Health Check

GET /

Response:

{
  "status": "Backend running successfully",
  "team": "HackNexus",
  "service": "AI Document Assistant"
}

Upload Document

POST /upload/
Content-Type: multipart/form-data

Request:

  • file: PDF or DOCX file (max 20MB)

Response:

{
  "message": "Document uploaded and indexed successfully",
  "total_chunks": 42
}

Ask Question

POST /query/
Content-Type: application/json

Request:

{
  "question": "What is the refund policy?"
}

Response:

{
  "answer": "Customers can request a refund within 30 days of purchase.",
  "confidence": 0.82,
  "sources": [
    {
      "page": 3,
      "snippet": "Refunds accepted within 30 days of purchase..."
    }
  ]
}

Project Structure

backend/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py              # FastAPI app with CORS
β”‚   β”œβ”€β”€ config.py            # Environment & settings
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── schemas.py       # Pydantic models
β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   β”œβ”€β”€ upload.py        # Document upload endpoint
β”‚   β”‚   └── query.py         # Q&A endpoint
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ document_loader.py   # PDF/DOCX parsing
β”‚   β”‚   β”œβ”€β”€ vector_store.py      # FAISS operations
β”‚   β”‚   └── qa_engine.py         # Gemini RAG pipeline
β”‚   └── utils/
β”‚       └── references.py    # Helper functions
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ uploads/             # Temporary file storage
β”‚   └── vector_db/           # FAISS index storage
β”œβ”€β”€ .env                     # Environment variables
β”œβ”€β”€ requirements.txt         # Python dependencies
└── README.md               # This file

Key Features Explained

1. CORS Configuration

  • Allows frontend connections from multiple origins
  • Handles preflight requests
  • Returns proper headers even on errors

2. Document Processing

  • Validates file types and sizes
  • Extracts text page-by-page
  • Creates overlapping chunks for better context

3. Vector Storage

  • Generates 384-dim embeddings
  • Stores in FAISS for fast retrieval
  • Persists to disk automatically

4. RAG Pipeline

  1. Question β†’ Embedding
  2. Similarity search β†’ Top 3 chunks
  3. Build context with page numbers
  4. Gemini generates grounded answer
  5. Return with confidence & sources

Troubleshooting

CORS Errors

Make sure your frontend URL is listed in main.py CORS middleware.

Gemini API Errors

  • Check your API key in .env
  • Verify internet connection
  • Check quota limits at Google AI Studio

Upload Fails

  • Check file size (<20MB)
  • Verify file extension (.pdf or .docx)
  • Check backend logs for errors

No Documents Found

  • Upload a document first
  • Check if FAISS index was created in data/vector_db/

Environment Variables

Variable Required Description
GEMINI_API_KEY βœ… Yes Google Gemini API key

Development

Run in Debug Mode

uvicorn app.main:app --reload --env-file .env --log-level debug

View Logs

All operations print to console with emoji indicators:

  • βœ… Success
  • ❌ Error
  • ⚠️ Warning
  • πŸ”„ Processing
  • πŸ“¦ Loading

Production Deployment

For production, consider:

  1. Use proper secrets management
  2. Add authentication
  3. Implement rate limiting
  4. Add request logging
  5. Use PostgreSQL for metadata
  6. Deploy with Docker

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors