File size: 12,089 Bytes
9916f7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Complete Process Flow - BMS AI Assistant</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
        }

        .header {
            background: linear-gradient(135deg, #D01010 0%, #A00C0C 100%);
            color: white;
            padding: 30px;
            text-align: center;
            border-radius: 10px 10px 0 0;
        }

        .header h1 {
            font-size: 2em;
            margin-bottom: 10px;
        }

        .content {
            padding: 30px;
        }

        .flow-step {
            background: #f8f9fa;
            border-left: 5px solid #D01010;
            padding: 20px;
            margin: 15px 0;
            border-radius: 5px;
        }

        .flow-step h3 {
            color: #D01010;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .step-num {
            background: #D01010;
            color: white;
            width: 35px;
            height: 35px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }

        .arrow {
            text-align: center;
            font-size: 2em;
            color: #D01010;
            margin: 10px 0;
        }

        .code {
            background: #2d2d2d;
            color: #f8f8f2;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
            font-family: monospace;
            font-size: 0.9em;
            overflow-x: auto;
        }

        .branch {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 10px;
            margin: 15px 0;
        }

        .branch-item {
            background: white;
            border: 2px solid #D01010;
            padding: 15px;
            text-align: center;
            border-radius: 5px;
        }

        .branch-item h4 {
            color: #D01010;
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <h1>πŸ”„ Complete Technical Process Flow</h1>
            <p>BMS AI Assistant - User to System to User</p>
        </div>
        <div class="content">

            <div class="flow-step">
                <h3><span class="step-num">1</span> User Input</h3>
                <p><strong>Component:</strong> Frontend UI (index.html)</p>
                <p><strong>Action:</strong> User types query in chat interface</p>
                <p><strong>Example:</strong> "What is the forecast for BMS0015 next month?"</p>
                <div class="code">userInput.addEventListener('keypress', (e) => {
                    if (e.key === 'Enter') sendMessage();
                    });</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">2</span> Client Processing</h3>
                <p><strong>Component:</strong> JavaScript</p>
                <p><strong>Action:</strong> Validate input, display user message, prepare API call</p>
                <div class="code">const text = userInput.value.trim();
                    addMessage(text, 'user');</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">3</span> HTTP POST Request</h3>
                <p><strong>Endpoint:</strong> /api/chat</p>
                <p><strong>Method:</strong> POST</p>
                <p><strong>Payload:</strong> {"message": "user query"}</p>
                <div class="code">fetch('/api/chat', {
                    method: 'POST',
                    headers: {'Content-Type': 'application/json'},
                    body: JSON.stringify({message: text})
                    });</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">4</span> Server Receives Request</h3>
                <p><strong>Component:</strong> FastAPI (main.py)</p>
                <p><strong>Server:</strong> Uvicorn ASGI</p>
                <div class="code">@app.post("/api/chat")
                    async def chat(request: ChatRequest):
                    message = request.message</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">5</span> Intent Parsing</h3>
                <p><strong>Component:</strong> intent_parser.py</p>
                <p><strong>Method:</strong> Regex pattern matching</p>
                <p><strong>Extracts:</strong> Intent, Item code, Quantity, Location, Horizon</p>
                <div class="code">parsed = parser.parse(message)
                    # Returns: {
                    # "intent": "demand_forecast",
                    # "item_code": "BMS0015",
                    # "horizon_days": 30
                    # }</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">6</span> Intent Routing</h3>
                <p><strong>Decision Point:</strong> Route to appropriate handler</p>
                <div class="branch">
                    <div class="branch-item">
                        <h4>Forecast</h4>
                        <p>forecasting.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>Item Details</h4>
                        <p>data_loader.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>Inventory</h4>
                        <p>data_loader.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>Supplier</h4>
                        <p>data_loader.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>Requisition</h4>
                        <p>data_loader.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>Status</h4>
                        <p>data_loader.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>PDF</h4>
                        <p>pdf_generator.py</p>
                    </div>
                    <div class="branch-item">
                        <h4>Chat</h4>
                        <p>llm_engine.py</p>
                    </div>
                </div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">7</span> Business Logic Execution</h3>
                <p><strong>Example:</strong> Demand Forecast</p>
                <p>1. Load demand_history.csv<br>
                    2. Filter by item_code<br>
                    3. Fit ARIMA model<br>
                    4. Generate forecast<br>
                    5. Format as JSON</p>
                <div class="code">forecast_data = forecast_demand(item_code, horizon)
                    # Returns: [{"date": "2025-01-01", "qty": 150}, ...]</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">8</span> Data Retrieval</h3>
                <p><strong>Component:</strong> data_loader.py</p>
                <p><strong>Sources:</strong> items.csv, inventory.csv, suppliers.csv, etc.</p>
                <p><strong>Technology:</strong> Pandas DataFrames</p>
                <div class="code">item = loader.items_df[loader.items_df['item_code'] == 'BMS0015']</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">9</span> LLM Processing (if needed)</h3>
                <p><strong>Component:</strong> llm_engine.py</p>
                <p><strong>Trigger:</strong> Only for general_chat intent</p>
                <p><strong>Model:</strong> TinyLlama 1.1B</p>
                <p><strong>Time:</strong> 5-15 seconds</p>
                <div class="code">response = llm.generate_response(query)
                    # Uses company_context.txt for RAG</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">10</span> Response Formatting</h3>
                <p><strong>Component:</strong> main.py</p>
                <p><strong>Format:</strong> JSON</p>
                <div class="code">return {
                    "intent": "demand_forecast",
                    "answer": "Forecast for BMS0015...",
                    "forecast": [{"date": "2025-01-01", "qty": 150}]
                    }</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">11</span> HTTP Response</h3>
                <p><strong>Status:</strong> 200 OK</p>
                <p><strong>Content-Type:</strong> application/json</p>
                <p><strong>Sent to:</strong> Client browser</p>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">12</span> Client Receives Response</h3>
                <p><strong>Component:</strong> JavaScript Fetch API</p>
                <p><strong>Action:</strong> Parse JSON response</p>
                <div class="code">const data = await response.json();
                    let botHtml = data.answer;</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">13</span> UI Rendering</h3>
                <p><strong>Component:</strong> JavaScript DOM manipulation</p>
                <p><strong>Actions:</strong></p>
                <p>1. Create message bubble<br>
                    2. Add bot icon<br>
                    3. Render forecast table (if present)<br>
                    4. Add download button (if PDF)<br>
                    5. Scroll to bottom</p>
                <div class="code">addMessage(botHtml, 'bot', true);
                    if (data.forecast) {
                    botHtml += renderForecastTable(data.forecast);
                    }</div>
            </div>

            <div class="arrow">↓</div>

            <div class="flow-step">
                <h3><span class="step-num">14</span> User Sees Response</h3>
                <p><strong>Display:</strong> Chat bubble with bot icon</p>
                <p><strong>Features:</strong> Formatted text, tables, download links</p>
                <p><strong>Animation:</strong> Fade-in effect</p>
                <p><strong>Interaction:</strong> User can copy text, download PDFs, ask follow-up</p>
            </div>

            <div style="background: #e8f5e9; padding: 20px; border-radius: 5px; margin-top: 30px;">
                <h3 style="color: #2e7d32; margin-bottom: 10px;">βœ… Flow Complete</h3>
                <p><strong>Total Steps:</strong> 14</p>
                <p><strong>Average Time:</strong> 2-15 seconds (depending on intent)</p>
                <p><strong>Technologies Used:</strong> HTML/JS, FastAPI, Pandas, ARIMA, TinyLlama</p>
            </div>

        </div>
    </div>
</body>

</html>