RAG Flask Starter

December 14, 2025 ยท View on GitHub

A Retrieval-Augmented Generation (RAG) chatbot API built with Flask, LlamaIndex, Pinecone, and MongoDB. This project provides a production-ready foundation for building AI-powered conversational applications with document retrieval capabilities.

๐Ÿš€ Features

  • RAG-powered Chat: Contextual responses using document retrieval with LlamaIndex
  • Multiple Source Types: Support for PDF, CSV, and Q&A pairs as knowledge sources
  • Vector Search: Pinecone integration for efficient similarity search
  • Persistent Storage: MongoDB for chat history, user management, and index storage
  • Streaming Responses: Real-time token streaming for chat responses
  • Rate Limiting: Built-in rate limiting with Flask-Limiter
  • Authentication: JWT-based authentication with admin and user roles
  • reCAPTCHA Support: Google reCAPTCHA validation middleware
  • CORS Enabled: Cross-Origin Resource Sharing support
  • Production Ready: Gunicorn (Linux) and Waitress (Windows) server support

๐Ÿ“‹ Prerequisites

  • Python 3.11+
  • MongoDB instance
  • Pinecone account and API key
  • Perplexity API key (for LLM)
  • (Optional) Google reCAPTCHA keys

๐Ÿ› ๏ธ Installation

  1. Clone the repository

    git clone https://github.com/marco-bertelli/rag.flask-start.git
    cd rag.flask-start
    
  2. Create a virtual environment

    python -m venv venv
    
    # Windows
    venv\Scripts\activate
    
    # Linux/Mac
    source venv/bin/activate
    
  3. Install dependencies

    pip install -r requirements.txt
    
  4. Configure environment variables

    Create a .env file in the root directory:

    # MongoDB
    MONGODB_URI=mongodb+srv://your-connection-string
    MONGODB_DATABASE=your-database-name
    
    # Security
    SECRET_KEY=your-jwt-secret-key
    
    # Pinecone
    PINECONE_API_KEY=your-pinecone-api-key
    PINECONE_ENV=your-pinecone-environment
    
    # Perplexity (LLM)
    PERPLEXITY_API_KEY=your-perplexity-api-key
    
    # OpenAI (optional)
    OPENAI_API_KEY=your-openai-api-key
    
    # reCAPTCHA (optional)
    RECAPTCHA_SECRET_KEY=your-recaptcha-secret-key
    

๐Ÿš€ Running the Application

Development (Windows)

python windows_waitress_start.py

Production (Linux/Heroku)

gunicorn --preload --max-requests 500 --max-requests-jitter 5 -t 3 --worker-class gthread --timeout 120 index:app

The server will start on port 8080.

๐Ÿ“š API Endpoints

Chat Endpoints

MethodEndpointDescriptionAuth
GET/chats/meGet current user's chatUser Token
GET/chats/me/historyGet chat historyUser Token
GET/chats/guestCreate a guest chat sessionNone
GET/chats/<chatId>/answer?answer=<query>Query the chatbot (streaming)None
PUT/chats/message/<messageId>/feedbackSet message feedback (good/bad)None

Source Management Endpoints (Admin Only)

MethodEndpointDescriptionAuth
POST/index/source/<sourceType>Add a new source to the indexAdmin Token
DELETE/index/source/<sourceId>Remove a source from the indexAdmin Token

Source Types

  • qa: Question-Answer pairs
    { "question": "What is RAG?", "answer": "RAG stands for..." }
    
  • csv: CSV file with questions and answers columns
    { "path": "https://example.com/data.csv" }
    
  • pdf: PDF document
    { "path": "https://example.com/document.pdf" }
    

๐Ÿ—๏ธ Project Structure

rag.flask-start/
โ”œโ”€โ”€ app.py                 # Flask app configuration
โ”œโ”€โ”€ index.py               # Application entry point
โ”œโ”€โ”€ index_manager.py       # LlamaIndex setup and management
โ”œโ”€โ”€ conf.py                # Environment configuration loader
โ”œโ”€โ”€ windows_waitress_start.py  # Windows server startup
โ”œโ”€โ”€ Procfile               # Heroku/Gunicorn configuration
โ”œโ”€โ”€ requirements.txt       # Python dependencies
โ”œโ”€โ”€ data/                  # Sample data files
โ”‚   โ””โ”€โ”€ rules.pdf          # Initial document for indexing
โ”œโ”€โ”€ apis/
โ”‚   โ”œโ”€โ”€ chats.py           # Chat API endpoints
โ”‚   โ””โ”€โ”€ sources.py         # Source management endpoints
โ”œโ”€โ”€ middlewares/
โ”‚   โ”œโ”€โ”€ auth_middleware.py # JWT authentication
โ”‚   โ””โ”€โ”€ re_captcha.py      # reCAPTCHA validation
โ”œโ”€โ”€ mongodb/
โ”‚   โ””โ”€โ”€ index.py           # MongoDB operations
โ””โ”€โ”€ utils/
    โ”œโ”€โ”€ chat_history_parser.py  # Chat history formatting
    โ”œโ”€โ”€ mongo_parsers.py        # MongoDB JSON encoder
    โ”œโ”€โ”€ parsers.py              # Document parsing utilities
    โ”œโ”€โ”€ validators.py           # Input validation
    โ””โ”€โ”€ vector_database.py      # Pinecone/MongoDB vector store setup

โš™๏ธ Configuration

LLM Settings

The project uses Perplexity's mixtral-8x7b-instruct model by default. Configuration is in index_manager.py:

llm = Perplexity(
    api_key=os.getenv("PERPLEXITY_API_KEY"), 
    model="mixtral-8x7b-instruct", 
    temperature=0.2
)

Embedding Model

Uses the local HuggingFace model BAAI/bge-small-en-v1.5 for embeddings:

Settings.embed_model = "local:BAAI/bge-small-en-v1.5"

Rate Limits

Rate limits are configured in apis/chats.py:

  • /chats/me: 5 requests per minute
  • /chats/me/history: 15 requests per minute
  • /chats/<chatId>/answer: 10 requests per minute

๐Ÿ” Authentication

The API uses JWT tokens for authentication. Include the token in the Authorization header:

Authorization: Bearer <your-jwt-token>

User Roles

  • User: Can access chat features
  • Admin: Can manage knowledge sources (add/delete)
  • Guest: Limited access with temporary chat sessions

๐Ÿ“ฆ Dependencies

Key dependencies include:

  • Flask: Web framework
  • LlamaIndex: RAG framework
  • Pinecone: Vector database
  • PyMongo: MongoDB driver
  • Flask-Limiter: Rate limiting
  • Flask-CORS: CORS support
  • PyJWT: JWT authentication
  • llmsherpa: PDF parsing
  • Transformers & PyTorch: ML models

๐Ÿšข Deployment

Heroku

The project includes a Procfile for Heroku deployment:

web: gunicorn --preload --max-requests 500 --max-requests-jitter 5 -t 3 --worker-class gthread --timeout 120 index:app

Docker (Custom)

Create a Dockerfile:

FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["gunicorn", "--preload", "-t", "120", "index:app"]

๐Ÿ“„ License

This project is open source and available under the MIT License.

๐Ÿ‘ค Author

Marco Bertelli

๐Ÿค Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page.


โญ Star this repository if you find it helpful!