Architecture¶
System Diagram¶
flowchart TD
DB[(SQL Server\nmaster DB)] -->|nightly sync| SW[sync_worker.py]
SW -->|writes| CACHE[cache/SKI_context.json\nper club]
CACHE -->|read on startup\nand per request| API[FastAPI app.py\n127.0.0.1:8000]
API -->|POST /api/chat| GEMINI[Google Gemini API\ngemini-2.5-flash]
GEMINI -->|AI response| API
BROWSER[Browser / Guest] -->|HTTPS| IIS[IIS\nreverse proxy]
IIS -->|POST /api/chat| API
API -->|JSON response| IIS
IIS -->|HTTPS| BROWSER
CMS[ai_chatbot_loader.asp\nper tenant] -->|includes| WIDGET[ai_chat_widget.asp]
WIDGET -->|JavaScript| BROWSER
CMS -->|reads| DB
Component Descriptions¶
API/python/app/app.py – FastAPI backend¶
The core of the solution. Exposes two endpoints:
| Endpoint | Method | Description |
|---|---|---|
/api/chat |
POST | Receives message + history, returns AI reply |
/health |
GET | Health status for monitoring |
Request format (POST /api/chat):
{
"tenant_id": "SKI",
"message": "When does the driving range open?",
"history": [
{"role": "user", "content": "Hi"},
{"role": "assistant", "content": "Hi! How can I help you?"}
]
}
The system prompt is built by build_system_prompt(tenant_id) which reads cache/{tenant_id}_context.json.
API/chatbot/sync_worker.py – Data sync¶
Runs as a scheduled Windows task (nightly). Reads the Clubs table in the master database for all clubs with boolChatbot = 1, queries 44 allowed tables per club, and writes the result to cache/{clubFolder}_context.json.
SKI/common-front/ai_chatbot_loader.asp – ASP loader¶
Runs server-side on every page view. Reads the AI_Chatbot_Settings table from the club's database and writes CSS variables and JS constants to the page (BOT_NAME, THEME_COLOR, WELCOME_MSG, BOT_LOGO_URL, TENANT_ID). Includes ai_chat_widget.asp dynamically if IsActive = 1.
SKI/assets/js/chatbot.js – Frontend widget¶
Vanilla JavaScript. Sends POST /api/chat with tenant_id, message and conversation history. TENANT_ID is injected from the ASP loader via <%=ROOT_FOLDER%>.
Deployment Diagram (IIS)¶
Internet
│
▼
IIS Site (port 443)
├── URL Rewrite: /api/chat → http://127.0.0.1:8000/api/chat
├── URL Rewrite: /health → http://127.0.0.1:8000/health
└── Classic ASP pages (ClubsiteCMS)
└── ai_chatbot_loader.asp (included in all pages)
Folder Structure (relevant files)¶
API/
├── chatbot/
│ ├── .env.example ← Environment variable template (no real values)
│ ├── requirements.txt ← Python dependencies
│ ├── sync_worker.py ← Data sync
│ ├── cache/ ← Runtime JSON cache (not in repo)
│ └── logs/ ← Runtime JSONL logs (not in repo)
├── python/
│ └── app/
│ └── app.py ← FastAPI main file
├── INSTALL_CHATBOT_2.ps1 ← Installation script
└── START_CHATBOT_v2.ps1 ← Startup script
SKI/
├── ai_chat_widget.asp ← Widget HTML
├── common-front/
│ └── ai_chatbot_loader.asp ← Server-side loader
├── assets/
│ └── js/
│ └── chatbot.js ← Client-side JS
└── CSADMIN/
└── files_setup/
├── cChatbot.asp ← Admin settings UI
└── scr_chatbot.asp ← Save settings (UPSERT)