Hereβs an updated README.md for your MAAS project, reflecting the expanded functionality and project structure that now includes the RAG-based chat system in addition to PageSpeed insights and Gemini-based analysis.
MAAS API (Metrics & AI-Assisted Suggestions)
A professional FastAPI application that offers two core services:
- PageSpeed Performance Reports β Using Google's PageSpeed Insights and Gemini AI for analysis and recommendations.
- RAG-Powered Chat System β Retrieval-Augmented Generation (RAG) chat sessions with document ingestion, vectorstore indexing (FAISS), and persistent chat history (MongoDB).
β¨ Features
- π PageSpeed Insights integration for web performance metrics
- π€ Gemini AIβpowered optimization report generation
- π Document ingestion and chunked embedding with FAISS
- π¬ RAG-based conversational system per user and chat session
- π Clean modular FastAPI architecture
- π οΈ Configuration via environment variables
- π Secure, with input validation and API key protection
- π Built-in health check, detailed logging, and auto-generated API docs
π Project Structure
MAAS/
βββ app/
β βββ rag/ # RAG module for document ingestion and chat
β β βββ db.py
β β βββ embedding.py
β β βββ routes.py # RAG API endpoints
β β βββ schemas.py
β β βββ utils.py
β β βββ chat_history.py # chat history persistence
β βββ config.py # Environment & settings
β βββ main.py # FastAPI app instance & routers
β βββ models.py # Pydantic models
β βββ run_server.py # Server runner
β βββ services.py # PageSpeed + Gemini logic
βββ Dockerfile # Optional containerization
βββ requirements.txt # Dependencies
βββ README.md # You're reading it
π Getting Started
1. Install Dependencies
pip install -r requirements.txt
2. Create a .env file
PAGESPEED_API_KEY=your_pagespeed_api_key_here
GEMINI_API_KEY=your_gemini_api_key_here
MONGO_URI=mongodb://localhost:27017
HOST=0.0.0.0
PORT=8000
DEBUG=True
2. Create & Activate Virtual Environment
Windows (PowerShell)
# Create venv folder named `.venv`
python -m venv .venv
# Activate it
.venv\Scripts\Activate.ps1
Linux / macOS (bash/zsh)
# Create venv folder named .venv
python3 -m venv .venv
# Activate it
source .venv/bin/activate
Tip: On macOS you may need to run
chmod +x .venv/bin/activatefirst if you get a permission error.
3. Install Dependencies
pip install --upgrade pip
pip install -r requirements.txt
4. Run the Application
# Option 1: Using the script
python run_server.py
# Option 2: Directly with uvicorn
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
π API Overview
π General
| Method | Endpoint | Description |
|---|---|---|
| GET | / |
Welcome + links to docs/health |
| GET | /health |
Health check and uptime |
π§ PageSpeed + Gemini Endpoints
| Method | Endpoint | Description |
|---|---|---|
| POST | /pagespeed |
Fetch raw PageSpeed Insights JSON |
| POST | /generate-report |
Generate AI optimization report |
| POST | /generate-priorities |
Rank optimizations by priority |
π RAG Chat System Endpoints
| Method | Endpoint | Description |
|---|---|---|
| POST | /rag/ingest/{user_id} |
Ingest documents and store FAISS index |
| POST | /rag/chat/create/{user_id} |
Start a new chat session (returns chat ID) |
| POST | /rag/chat/{user_id}/{chat_id} |
Ask a question in an existing chat session |
π RAG Workflow
Ingest Documents
- POST
/rag/ingest/{user_id} - Body:
{"documents": ["doc 1 text", "doc 2 text", ...]}
- POST
Create Chat
- POST
/rag/chat/create/{user_id} - Response:
chat_id
- POST
Ask Questions
- POST
/rag/chat/{user_id}/{chat_id} - Body:
{"question": "What does the document say about X?"}
- POST
π Example Usage (Python)
import requests
# Ingest docs
requests.post("http://localhost:8000/rag/ingest/user123", json={
"documents": ["The capital of France is Paris.", "Python is a programming language."]
})
# Create chat
res = requests.post("http://localhost:8000/rag/chat/create/user123")
chat_id = res.json()["chat_id"]
# Chat
requests.post(f"http://localhost:8000/rag/chat/user123/{chat_id}", json={
"question": "What is the capital of France?"
})
π API Docs
Once the app is running:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
π‘οΈ Error Handling
400 Bad Request: Invalid input404 Not Found: Unknown endpoint or missing user/chat/doc500 Internal Server Error: API or service errors
π§ͺ Development Tips
- Use
DEBUG=Truein.envfor auto-reload and verbose logs - Modify
CORSpolicy inmain.pybefore production - Use
loggercalls to trace errors or logic flows
π API Key Setup
PageSpeed Insights
- Google Cloud Console
- Enable the API, generate a key
Gemini AI
- Google AI Studio
- Create API Key
Add both to your .env.
π¦ Docker Support
Basic Dockerfile is included. To build and run:
docker build -t maas-api .
docker run -p 8000:8000 --env-file .env maas-api
π€ Contributing
- Follow existing modular structure
- Document all new endpoints clearly
- Test edge cases (e.g., malformed docs or bad chat IDs)
- Use logging for traceability
- Create clear, typed Pydantic schemas
π Repository
https://github.com/Hammadwakeel/MAAS