Architecture
How the Student Affairs Assistant works
A Retrieval-Augmented Generation (RAG) chatbot: it first searches the official Student Affairs knowledge base, then asks a language model to answer only from the passages it found – and shows those passages as sources.

Answer workflow
- 1
Student
Asks a question in the chat widget on the SUC website (English, Bahasa Malaysia or 中文).
- 2
Webhook API
FastAPI POST /chat on Vercel (Singapore). Per-student rate limits and a daily LLM budget guard run first.
- 3
Knowledge retrieval
TF-IDF (from scratch) + BAAI/bge-small-en-v1.5 embeddings → hybrid top-5 chunks. A trilingual glossary lets Malay and English questions reach Chinese passages.
- 4
Relevance gate
If nothing relevant is found, the bot replies with the official “could not find” message – no LLM call, no hallucination.
- 5
Prompt builder
The specification's template: answer only from the supplied knowledge, cite passages as [n], reply in the student's language.
- 6
LiteLLM proxy
claude-haiku-4-5 by default, with DeepSeek / OpenRouter fallbacks. Key budget $1/day, 5 requests/min, every call tagged and logged.
- 7
Format response
Answer + cited sources (document, section, page, link) + related questions. Identical questions are served from cache.
Knowledge ingestion
- 1
Knowledge folder
Student Manual 2025/2026 (bilingual PDF), SAO web pages (EN/中文), convocation guidelines, insurance, internship FAQ, hostel/scholarship/club guides.
- 2
build_kb.py
Extracts PDF (OCR for scanned pages), DOCX, TXT and Markdown; masks personal data; splits into ~500-word chunks with 50-word overlap at chapter and section boundaries.
- 3
Embeddings
Each chunk is embedded with bge-small-en-v1.5 (ONNX, 384 dimensions) and stored as float16.
- 4
knowledge_chunks table
SQLite locally or MySQL on the Ubuntu server (phpMyAdmin). Admins can upload, edit or disable documents live – no redeployment.
Retrieval quality
20 test questions in English, Malay and Chinese, plus 6 off-topic questions that must be rejected. Hybrid search separates them best, so its 0.30 threshold stops off-topic questions before any LLM call.
| Method | Hit@1 | MRR | Lowest answerable score | Highest off-topic score |
|---|---|---|---|---|
| TF-IDF | 1.00 | 1.00 | 0.33 | 0.38 |
| Semantic (bge-small) | 1.00 | 1.00 | 0.66 | 0.27 |
| Hybrid (default) | 1.00 | 1.00 | 0.44 | 0.20 |
Technology
- Frontend
- Next.js 16 (React 19) on Vercel · embeddable widget.js · EN/BM/中文 interface
- API
- FastAPI (Python 3.12) on Vercel Functions, Singapore region · Docker image for Ubuntu VMs
- Retrieval
- TF-IDF + BAAI/bge-small-en-v1.5 (onnxruntime) hybrid search
- LLM gateway
- LiteLLM proxy · claude-haiku-4-5 with fallbacks · budget-capped key
- Database
- SQLite (bundled) · MySQL on Ubuntu with phpMyAdmin · PostgreSQL compatible
- Analytics
- Built-in admin dashboard + optional Streamlit dashboard