🔄 Complete Technical Process Flow

BMS AI Assistant - User to System to User

1 User Input

Component: Frontend UI (index.html)

Action: User types query in chat interface

Example: "What is the forecast for BMS0015 next month?"

userInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') sendMessage(); });
↓

2 Client Processing

Component: JavaScript

Action: Validate input, display user message, prepare API call

const text = userInput.value.trim(); addMessage(text, 'user');
↓

3 HTTP POST Request

Endpoint: /api/chat

Method: POST

Payload: {"message": "user query"}

fetch('/api/chat', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({message: text}) });
↓

4 Server Receives Request

Component: FastAPI (main.py)

Server: Uvicorn ASGI

@app.post("/api/chat") async def chat(request: ChatRequest): message = request.message
↓

5 Intent Parsing

Component: intent_parser.py

Method: Regex pattern matching

Extracts: Intent, Item code, Quantity, Location, Horizon

parsed = parser.parse(message) # Returns: { # "intent": "demand_forecast", # "item_code": "BMS0015", # "horizon_days": 30 # }
↓

6 Intent Routing

Decision Point: Route to appropriate handler

Forecast

forecasting.py

Item Details

data_loader.py

Inventory

data_loader.py

Supplier

data_loader.py

Requisition

data_loader.py

Status

data_loader.py

PDF

pdf_generator.py

Chat

llm_engine.py

↓

7 Business Logic Execution

Example: Demand Forecast

1. Load demand_history.csv
2. Filter by item_code
3. Fit ARIMA model
4. Generate forecast
5. Format as JSON

forecast_data = forecast_demand(item_code, horizon) # Returns: [{"date": "2025-01-01", "qty": 150}, ...]
↓

8 Data Retrieval

Component: data_loader.py

Sources: items.csv, inventory.csv, suppliers.csv, etc.

Technology: Pandas DataFrames

item = loader.items_df[loader.items_df['item_code'] == 'BMS0015']
↓

9 LLM Processing (if needed)

Component: llm_engine.py

Trigger: Only for general_chat intent

Model: TinyLlama 1.1B

Time: 5-15 seconds

response = llm.generate_response(query) # Uses company_context.txt for RAG
↓

10 Response Formatting

Component: main.py

Format: JSON

return { "intent": "demand_forecast", "answer": "Forecast for BMS0015...", "forecast": [{"date": "2025-01-01", "qty": 150}] }
↓

11 HTTP Response

Status: 200 OK

Content-Type: application/json

Sent to: Client browser

↓

12 Client Receives Response

Component: JavaScript Fetch API

Action: Parse JSON response

const data = await response.json(); let botHtml = data.answer;
↓

13 UI Rendering

Component: JavaScript DOM manipulation

Actions:

1. Create message bubble
2. Add bot icon
3. Render forecast table (if present)
4. Add download button (if PDF)
5. Scroll to bottom

addMessage(botHtml, 'bot', true); if (data.forecast) { botHtml += renderForecastTable(data.forecast); }
↓

14 User Sees Response

Display: Chat bubble with bot icon

Features: Formatted text, tables, download links

Animation: Fade-in effect

Interaction: User can copy text, download PDFs, ask follow-up

✅ Flow Complete

Total Steps: 14

Average Time: 2-15 seconds (depending on intent)

Technologies Used: HTML/JS, FastAPI, Pandas, ARIMA, TinyLlama