AI-powered business analytics app that converts plain English questions into safe SQL, executes them against connected data sources, and returns tables, charts, and insights.
This project is a full-stack analytics workspace built with:
FastAPIbackendNext.js+Tailwind CSSfrontendSQLitelocal analytics warehouse by defaultSQLAlchemy,Pandas, andOpenPyXLfor ingestion and querying- Optional LLM support with
OpenAI-compatible APIs orOllama
Users can:
- Upload
CSVandExcelfiles - Connect API payloads
- Import tables from external SQL databases
- Ask business questions in natural language
- Review generated SQL with read-only safety guardrails
- View results as tables and charts
- Save query history and dashboards
- Define reusable business rules like
revenueandprofit
- Multi-source ingestion
- Schema profiling with column aliases and sample rows
- Natural-language-to-SQL generation
- SQL validation and read-only safety checks
- Query history and saved dashboards
- Role-aware business rules
- Optional local LLM mode with Ollama
- Automated backend test coverage
.
|-- backend/
| |-- core/
| |-- db/
| |-- routes/
| |-- schemas/
| `-- services/
|-- database/
|-- frontend/
| `-- src/
|-- main.py
|-- req.txt
`-- test_cases.py
The backend is organized into modular layers:
backend/main.py: FastAPI app creation and router registrationbackend/routes/: API endpointsbackend/services/: business logic for ingestion, schema retrieval, SQL generation, safety, and executionbackend/db/: SQLAlchemy models, session setup, and bootstrap logicbackend/schemas/: request and response models
Core pipeline:
- User uploads or connects a data source
- Data is normalized into local SQL tables
- Schema metadata is profiled and stored
- Relevant tables are retrieved for a question
- SQL is generated with heuristic logic or configured LLM provider
- SQL is validated as read-only
- Query executes and returns rows, chart suggestions, and insights
/login/dashboard/upload/workspace/history/admin-rules
- Python
3.11+recommended - Virtual environment
- Node.js
18+ - npm
Create and activate a virtual environment, then install dependencies:
python -m venv venv
.\venv\Scripts\activate
pip install -r req.txtStart the FastAPI backend:
.\venv\Scripts\python.exe main.pyThe backend runs on:
http://localhost:8000- health check:
http://localhost:8000/health - API prefix:
http://localhost:8000/api
Install frontend dependencies:
cd frontend
npm installRun the Next.js app:
npm run devFrontend default URL:
http://localhost:3000
The frontend expects the backend at:
NEXT_PUBLIC_API_BASE=http://localhost:8000/api
The backend supports these environment variables:
| Variable | Default | Description |
|---|---|---|
APP_NAME |
Natural Language SQL Generator |
FastAPI app name |
API_PREFIX |
/api |
API route prefix |
DATABASE_URL |
local SQLite DB | SQLAlchemy connection string |
JWT_SECRET |
change-me-in-production |
Token signing secret |
ACCESS_TOKEN_EXPIRY_MINUTES |
720 |
Auth token lifetime |
DEFAULT_ROW_LIMIT |
100 |
Default row limit |
MAX_ROW_LIMIT |
500 |
Maximum row limit |
QUERY_TIMEOUT_SECONDS |
10 |
Query timeout |
LLM_PROVIDER |
heuristic |
heuristic, openai, or ollama |
OPENAI_BASE_URL |
OpenAI API URL | Base URL for OpenAI-compatible API |
OPENAI_API_KEY |
empty | API key for remote LLM |
OPENAI_MODEL |
gpt-4o-mini |
Model name |
OLLAMA_BASE_URL |
http://localhost:11434 |
Ollama server URL |
OLLAMA_MODEL |
llama3.1 |
Ollama model name |
CORS_ORIGINS |
localhost URLs | Allowed frontend origins |
Default mode. No external model required.
$env:LLM_PROVIDER="heuristic"$env:LLM_PROVIDER="openai"
$env:OPENAI_API_KEY="your_api_key"
$env:OPENAI_MODEL="gpt-4o-mini"$env:LLM_PROVIDER="ollama"
$env:OLLAMA_MODEL="llama3.1"The first registered user becomes the admin user automatically.
Admin users can:
- create business rules
- manage admin-only features
POST /api/auth/registerPOST /api/auth/loginGET /api/auth/me
POST /api/uploadPOST /api/connect-apiPOST /api/connect-databaseGET /api/datasources
GET /api/schemaGET /api/rulesPOST /api/rulesPOST /api/query
GET /api/historyGET /api/dashboardsPOST /api/dashboards
Run the backend test suite:
.\venv\Scripts\python.exe -m pytest test_cases.pyCurrent automated tests cover:
- registration and login
- file upload and schema discovery
- natural language query generation
- repeat customer analysis
- declining revenue analysis
- business rule creation
- API ingestion
- external database ingestion
- dashboard creation
- you can also test yourself using the test cases files in the root folder
Before pushing:
- Make sure secrets are stored in environment variables, not code.
- Do not commit local SQLite databases, caches, or virtual environments.
- Install frontend dependencies locally so
package-lock.jsoncan be committed if you want reproducible installs.
- Replace the default
JWT_SECRET - Use PostgreSQL or another managed database instead of local SQLite
- Put FastAPI behind a production ASGI server and reverse proxy
- Configure real authentication, HTTPS, and rate limiting
- Add background jobs for scheduled reports and refresh workflows