Spaces:
Runtime error
Runtime error
PDF Q&A Chatbot Backend
A FastAPI backend for a PDF-based Q&A chatbot. Upload PDF documents, ask questions, and get answers based on document content using LLMs (OpenRouter, Anthropic, etc.).
Table of Contents
- Overview
- Architecture
- Features
- API Endpoints
- Quick Start
- Environment Variables (
.env
) - File Structure
- Security Notes
- Hugging Face Spaces Deployment
- Troubleshooting
- Contribution
- License
Overview
This backend enables a chatbot to answer questions based on the content of uploaded PDF documents. It uses vector search (ChromaDB) to find relevant document chunks and LLMs (OpenRouter, Anthropic) to generate answers. All data is erased on backend restart (demo mode).
Architecture
graph TD;
User["User (Frontend)"] -->|Uploads PDF, Asks Question| API["FastAPI Backend"]
API -->|Stores| DB[("SQLite DB")]
API -->|Stores/Queries| ChromaDB["ChromaDB Vector Store"]
API -->|Calls| LLM["LLM API (OpenRouter/Anthropic)"]
API -->|Serves| User
Features
- Upload up to 3 PDF documents (demo limit, can be changed)
- Ask questions and get AI-generated answers based on your PDFs
- Vector search: Finds the most relevant document chunks for each question
- LLM integration: Uses OpenRouter (OpenAI-compatible) or Anthropic Claude
- All data erased on restart: Ensures stateless demo for reviewers
- Admin endpoint to clear all data manually
- API health check endpoint
API Endpoints
Document Management
POST /api/v1/documents/upload
β Upload a PDF (max 3, 10MB each)GET /api/v1/documents/
β List all uploaded documentsGET /api/v1/documents/{document_id}
β Get a specific documentDELETE /api/v1/documents/{document_id}
β Delete a documentPOST /api/v1/documents/clear_all
β Clear all documents, chat, uploads, and vectors (admin)GET /api/v1/documents/stats/summary
β Get document and vector store stats
Chat
POST /api/v1/chat/
β Ask a question (with session ID, model, and optional document ID)GET /api/v1/chat/history/{session_id}
β Get chat history for a sessionPOST /api/v1/chat/session/new
β Create a new chat sessionGET /api/v1/chat/sessions
β List all chat sessionsDELETE /api/v1/chat/session/{session_id}
β Delete a chat sessionGET /api/v1/chat/models/available
β List available LLM models
Health
GET /health
β Health checkGET /
β API info
Quick Start
- Clone the repo
- Install dependencies:
pip install -r requirements.txt
- Copy
.env.example
to.env
and fill in your API keys:cp .env.example .env # Edit .env and set your keys
- Run the backend:
uvicorn main:app --host 0.0.0.0 --port 8000
Environment Variables (.env
)
- Never commit your real
.env
file to version control! - Example variables:
SECRET_KEY
β Generate withpython -c "import secrets; print(secrets.token_urlsafe(64))"
OPENROUTER_API_KEY
,ANTHROPIC_API_KEY
β Set your API keys hereDATABASE_URL
β Default is SQLite, can be set to PostgreSQL for productionUPLOAD_DIR
,CHROMA_PERSIST_DIRECTORY
β Directories for uploads and vector store
- See
.env.example
for all options
File Structure
backend/
app/
api/endpoints/ # FastAPI route handlers
core/ # Config, DB setup
models/ # SQLAlchemy models
schemas/ # Pydantic schemas
services/ # PDF, AI, vector store logic
chroma_db/ # ChromaDB vector store (excluded by .gitignore)
uploads/ # Uploaded PDFs (excluded by .gitignore)
requirements.txt # Python dependencies
main.py # FastAPI app entrypoint
.env.example # Example environment file
.gitignore # Excludes sensitive/data files
README.md # This file
Security Notes
- Never commit real API keys or secrets to your repo.
- The
SECRET_KEY
should be a long, random string (see above). - For real deployments, remove the auto-wipe logic and use a persistent database.
- Validate all uploaded files (PDF only, max 10MB).
- CORS is restricted to localhost by default; update for production.
Hugging Face Spaces Deployment
- Set all secrets (API keys, secret key) using the Spaces UI, not in
.env
. - The backend will erase all data on restart (stateless demo mode).
- SQLite is used for demo purposes; for production, use PostgreSQL.
- Spaces may restart your app at any time; do not rely on local persistence.
Troubleshooting
- API key issues: Ensure your keys are set in the environment or Spaces UI.
- Vector store empty: Check logs for ChromaDB errors; ensure singleton pattern is used.
- Uploads not working: Check file size/type and that
uploads/
exists and is writable. - CORS errors: Update
BACKEND_CORS_ORIGINS
in.env
or config. - App restarts: All data is wiped on restart by design (demo mode).
Contribution
- Fork the repo and create a feature branch.
- Make your changes (ensure code is clean and tested).
- Submit a pull request with a clear description.
- For major changes, open an issue first to discuss.
License
MIT