Update README.md
Browse files
README.md
CHANGED
|
@@ -1,247 +1,10 @@
|
|
| 1 |
-
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.
|
| 2 |
-
|
| 3 |
-
---
|
| 4 |
-
|
| 5 |
-
# MAAS API (Metrics & AI-Assisted Suggestions)
|
| 6 |
-
|
| 7 |
-
A professional FastAPI application that offers two core services:
|
| 8 |
-
|
| 9 |
-
1. **PageSpeed Performance Reports** β Using Google's PageSpeed Insights and Gemini AI for analysis and recommendations.
|
| 10 |
-
2. **RAG-Powered Chat System** β Retrieval-Augmented Generation (RAG) chat sessions with document ingestion, vectorstore indexing (FAISS), and persistent chat history (MongoDB).
|
| 11 |
-
|
| 12 |
-
## β¨ Features
|
| 13 |
-
|
| 14 |
-
* π PageSpeed Insights integration for web performance metrics
|
| 15 |
-
* π€ Gemini AIβpowered optimization report generation
|
| 16 |
-
* π Document ingestion and chunked embedding with FAISS
|
| 17 |
-
* π¬ RAG-based conversational system per user and chat session
|
| 18 |
-
* π Clean modular FastAPI architecture
|
| 19 |
-
* π οΈ Configuration via environment variables
|
| 20 |
-
* π Secure, with input validation and API key protection
|
| 21 |
-
* π Built-in health check, detailed logging, and auto-generated API docs
|
| 22 |
-
|
| 23 |
-
---
|
| 24 |
-
|
| 25 |
-
## π Project Structure
|
| 26 |
-
|
| 27 |
-
```
|
| 28 |
-
MAAS/
|
| 29 |
-
βββ app/
|
| 30 |
-
β βββ rag/ # RAG module for document ingestion and chat
|
| 31 |
-
β β βββ db.py
|
| 32 |
-
β β βββ embedding.py
|
| 33 |
-
β β βββ routes.py # RAG API endpoints
|
| 34 |
-
β β βββ schemas.py
|
| 35 |
-
β β βββ utils.py
|
| 36 |
-
β β βββ chat_history.py # chat history persistence
|
| 37 |
-
|
| 38 |
-
β βββ config.py # Environment & settings
|
| 39 |
-
β βββ main.py # FastAPI app instance & routers
|
| 40 |
-
β βββ models.py # Pydantic models
|
| 41 |
-
β βββ run_server.py # Server runner
|
| 42 |
-
β βββ services.py # PageSpeed + Gemini logic
|
| 43 |
-
βββ Dockerfile # Optional containerization
|
| 44 |
-
βββ requirements.txt # Dependencies
|
| 45 |
-
βββ README.md # You're reading it
|
| 46 |
-
```
|
| 47 |
-
|
| 48 |
-
---
|
| 49 |
-
|
| 50 |
-
## π Getting Started
|
| 51 |
-
|
| 52 |
-
### 1. Install Dependencies
|
| 53 |
-
|
| 54 |
-
```bash
|
| 55 |
-
pip install -r requirements.txt
|
| 56 |
-
```
|
| 57 |
-
|
| 58 |
-
### 2. Create a `.env` file
|
| 59 |
-
|
| 60 |
-
```env
|
| 61 |
-
PAGESPEED_API_KEY=your_pagespeed_api_key_here
|
| 62 |
-
GEMINI_API_KEY=your_gemini_api_key_here
|
| 63 |
-
MONGO_URI=mongodb://localhost:27017
|
| 64 |
-
HOST=0.0.0.0
|
| 65 |
-
PORT=8000
|
| 66 |
-
DEBUG=True
|
| 67 |
-
```
|
| 68 |
-
|
| 69 |
-
## 2. Create & Activate Virtual Environment
|
| 70 |
-
|
| 71 |
-
### Windows (PowerShell)
|
| 72 |
-
```powershell
|
| 73 |
-
# Create venv folder named `.venv`
|
| 74 |
-
python -m venv .venv
|
| 75 |
-
|
| 76 |
-
# Activate it
|
| 77 |
-
.venv\Scripts\Activate.ps1
|
| 78 |
-
````
|
| 79 |
-
|
| 80 |
-
### Linux / macOS (bash/zsh)
|
| 81 |
-
|
| 82 |
-
```bash
|
| 83 |
-
# Create venv folder named .venv
|
| 84 |
-
python3 -m venv .venv
|
| 85 |
-
|
| 86 |
-
# Activate it
|
| 87 |
-
source .venv/bin/activate
|
| 88 |
-
```
|
| 89 |
-
|
| 90 |
-
> **Tip:** On macOS you may need to run `chmod +x .venv/bin/activate` first if you get a permission error.
|
| 91 |
-
|
| 92 |
-
### 3. Install Dependencies
|
| 93 |
-
|
| 94 |
-
```bash
|
| 95 |
-
pip install --upgrade pip
|
| 96 |
-
pip install -r requirements.txt
|
| 97 |
-
```
|
| 98 |
-
|
| 99 |
-
## 4. Run the Application
|
| 100 |
-
|
| 101 |
-
```bash
|
| 102 |
-
# Option 1: Using the script
|
| 103 |
-
python run_server.py
|
| 104 |
-
|
| 105 |
-
# Option 2: Directly with uvicorn
|
| 106 |
-
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
|
| 107 |
-
```
|
| 108 |
-
|
| 109 |
-
## π API Overview
|
| 110 |
-
|
| 111 |
-
### π General
|
| 112 |
-
|
| 113 |
-
| Method | Endpoint | Description |
|
| 114 |
-
| ------ | --------- | ------------------------------ |
|
| 115 |
-
| GET | `/` | Welcome + links to docs/health |
|
| 116 |
-
| GET | `/health` | Health check and uptime |
|
| 117 |
-
|
| 118 |
---
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
| POST | `/generate-report` | Generate AI optimization report |
|
| 126 |
-
| POST | `/generate-priorities` | Rank optimizations by priority |
|
| 127 |
-
|
| 128 |
---
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
| Method | Endpoint | Description |
|
| 133 |
-
| ------ | ------------------------------- | ------------------------------------------ |
|
| 134 |
-
| POST | `/rag/ingest/{user_id}` | Ingest documents and store FAISS index |
|
| 135 |
-
| POST | `/rag/chat/create/{user_id}` | Start a new chat session (returns chat ID) |
|
| 136 |
-
| POST | `/rag/chat/{user_id}/{chat_id}` | Ask a question in an existing chat session |
|
| 137 |
-
|
| 138 |
-
---
|
| 139 |
-
|
| 140 |
-
## π RAG Workflow
|
| 141 |
-
|
| 142 |
-
1. **Ingest Documents**
|
| 143 |
-
|
| 144 |
-
* POST `/rag/ingest/{user_id}`
|
| 145 |
-
* Body: `{"documents": ["doc 1 text", "doc 2 text", ...]}`
|
| 146 |
-
|
| 147 |
-
2. **Create Chat**
|
| 148 |
-
|
| 149 |
-
* POST `/rag/chat/create/{user_id}`
|
| 150 |
-
* Response: `chat_id`
|
| 151 |
-
|
| 152 |
-
3. **Ask Questions**
|
| 153 |
-
|
| 154 |
-
* POST `/rag/chat/{user_id}/{chat_id}`
|
| 155 |
-
* Body: `{"question": "What does the document say about X?"}`
|
| 156 |
-
|
| 157 |
-
---
|
| 158 |
-
|
| 159 |
-
## π Example Usage (Python)
|
| 160 |
-
|
| 161 |
-
```python
|
| 162 |
-
import requests
|
| 163 |
-
|
| 164 |
-
# Ingest docs
|
| 165 |
-
requests.post("http://localhost:8000/rag/ingest/user123", json={
|
| 166 |
-
"documents": ["The capital of France is Paris.", "Python is a programming language."]
|
| 167 |
-
})
|
| 168 |
-
|
| 169 |
-
# Create chat
|
| 170 |
-
res = requests.post("http://localhost:8000/rag/chat/create/user123")
|
| 171 |
-
chat_id = res.json()["chat_id"]
|
| 172 |
-
|
| 173 |
-
# Chat
|
| 174 |
-
requests.post(f"http://localhost:8000/rag/chat/user123/{chat_id}", json={
|
| 175 |
-
"question": "What is the capital of France?"
|
| 176 |
-
})
|
| 177 |
-
```
|
| 178 |
-
|
| 179 |
-
---
|
| 180 |
-
|
| 181 |
-
## π API Docs
|
| 182 |
-
|
| 183 |
-
Once the app is running:
|
| 184 |
-
|
| 185 |
-
* Swagger UI: [http://localhost:8000/docs](http://localhost:8000/docs)
|
| 186 |
-
* ReDoc: [http://localhost:8000/redoc](http://localhost:8000/redoc)
|
| 187 |
-
|
| 188 |
-
---
|
| 189 |
-
|
| 190 |
-
## π‘οΈ Error Handling
|
| 191 |
-
|
| 192 |
-
* `400 Bad Request`: Invalid input
|
| 193 |
-
* `404 Not Found`: Unknown endpoint or missing user/chat/doc
|
| 194 |
-
* `500 Internal Server Error`: API or service errors
|
| 195 |
-
|
| 196 |
-
---
|
| 197 |
-
|
| 198 |
-
## π§ͺ Development Tips
|
| 199 |
-
|
| 200 |
-
* Use `DEBUG=True` in `.env` for auto-reload and verbose logs
|
| 201 |
-
* Modify `CORS` policy in `main.py` before production
|
| 202 |
-
* Use `logger` calls to trace errors or logic flows
|
| 203 |
-
|
| 204 |
-
---
|
| 205 |
-
|
| 206 |
-
## π API Key Setup
|
| 207 |
-
|
| 208 |
-
### PageSpeed Insights
|
| 209 |
-
|
| 210 |
-
1. [Google Cloud Console](https://console.cloud.google.com/)
|
| 211 |
-
2. Enable the API, generate a key
|
| 212 |
-
|
| 213 |
-
### Gemini AI
|
| 214 |
-
|
| 215 |
-
1. [Google AI Studio](https://makersuite.google.com/)
|
| 216 |
-
2. Create API Key
|
| 217 |
-
|
| 218 |
-
Add both to your `.env`.
|
| 219 |
-
|
| 220 |
-
---
|
| 221 |
-
|
| 222 |
-
## π¦ Docker Support
|
| 223 |
-
|
| 224 |
-
Basic Dockerfile is included. To build and run:
|
| 225 |
-
|
| 226 |
-
```bash
|
| 227 |
-
docker build -t maas-api .
|
| 228 |
-
docker run -p 8000:8000 --env-file .env maas-api
|
| 229 |
-
```
|
| 230 |
-
|
| 231 |
-
---
|
| 232 |
-
|
| 233 |
-
## π€ Contributing
|
| 234 |
-
|
| 235 |
-
1. Follow existing modular structure
|
| 236 |
-
2. Document all new endpoints clearly
|
| 237 |
-
3. Test edge cases (e.g., malformed docs or bad chat IDs)
|
| 238 |
-
4. Use logging for traceability
|
| 239 |
-
5. Create clear, typed Pydantic schemas
|
| 240 |
-
|
| 241 |
-
---
|
| 242 |
-
|
| 243 |
-
## π Repository
|
| 244 |
-
|
| 245 |
-
[https://github.com/Hammadwakeel/MAAS](https://github.com/Hammadwakeel/MAAS-FastAPI.git)
|
| 246 |
-
|
| 247 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MAAS
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|