standalone.html 10.7 KB
Newer Older
Joe Chen's avatar
Joe Chen committed
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Customer Support Q&A</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8f9fa;
            padding: 20px;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
        }
        .question-form {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }
        .answer-container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
            display: none;
        }
        .source-results {
            margin-top: 30px;
        }
        .result-card {
            margin-bottom: 15px;
            border-left: 4px solid #0d6efd;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        .spinner-border {
            width: 3rem;
            height: 3rem;
        }
        h1 {
            color: #0d6efd;
            margin-bottom: 25px;
        }
        .answer-text {
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            border-left: 4px solid #198754;
        }
        .error-message {
            color: #dc3545;
            margin-top: 15px;
            display: none;
        }
        .api-settings {
            margin-top: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 5px;
            display: none;
        }
        .settings-toggle {
            cursor: pointer;
            color: #0d6efd;
            text-decoration: underline;
            font-size: 0.9rem;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="text-center">Customer Support Q&A</h1>
        
        <div class="question-form">
            <form id="questionForm">
                <div class="mb-3">
                    <label for="questionInput" class="form-label">Ask a question:</label>
                    <input type="text" class="form-control form-control-lg" id="questionInput" 
                           placeholder="How do I reset my password?">
                </div>
                <button type="submit" class="btn btn-primary btn-lg">Get Answer</button>
                <div class="mt-3">
                    <span class="settings-toggle" id="settingsToggle">⚙️ API Settings</span>
                </div>
            </form>
            
            <div class="api-settings" id="apiSettings">
                <div class="mb-3">
                    <label for="apiEndpoint" class="form-label">API Endpoint:</label>
                    <input type="text" class="form-control" id="apiEndpoint" value="http://localhost:5000/api/ask">
                </div>
                <button class="btn btn-sm btn-outline-secondary" id="saveSettings">Save Settings</button>
            </div>
            
            <div class="error-message" id="errorMessage"></div>
            
            <div class="loading" id="loadingSpinner">
                <div class="spinner-border text-primary" role="status">
                    <span class="visually-hidden">Loading...</span>
                </div>
                <p class="mt-2">Searching for answers...</p>
            </div>
        </div>
        
        <div class="answer-container" id="answerContainer">
            <h2>Answer:</h2>
            <div class="answer-text" id="answerText"></div>
            
            <div class="source-results" id="sourceResults">
                <h3>Source Conversations:</h3>
                <div id="resultsList"></div>
            </div>
        </div>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const questionForm = document.getElementById('questionForm');
            const questionInput = document.getElementById('questionInput');
            const loadingSpinner = document.getElementById('loadingSpinner');
            const answerContainer = document.getElementById('answerContainer');
            const answerText = document.getElementById('answerText');
            const resultsList = document.getElementById('resultsList');
            const errorMessage = document.getElementById('errorMessage');
            const settingsToggle = document.getElementById('settingsToggle');
            const apiSettings = document.getElementById('apiSettings');
            const apiEndpoint = document.getElementById('apiEndpoint');
            const saveSettings = document.getElementById('saveSettings');
            
            // Load saved API endpoint from localStorage
            if (localStorage.getItem('apiEndpoint')) {
                apiEndpoint.value = localStorage.getItem('apiEndpoint');
            }
            
            // Toggle settings panel
            settingsToggle.addEventListener('click', function() {
                apiSettings.style.display = apiSettings.style.display === 'none' ? 'block' : 'none';
            });
            
            // Save settings
            saveSettings.addEventListener('click', function() {
                localStorage.setItem('apiEndpoint', apiEndpoint.value);
                apiSettings.style.display = 'none';
                showError('Settings saved!', 'success');
            });
            
            questionForm.addEventListener('submit', function(e) {
                e.preventDefault();
                
                const question = questionInput.value.trim();
                if (!question) {
                    showError('Please enter a question.');
                    return;
                }
                
                // Get API endpoint from settings
                const endpoint = apiEndpoint.value.trim();
                if (!endpoint) {
                    showError('Please enter a valid API endpoint in settings.');
                    return;
                }
                
                // Reset UI
                errorMessage.style.display = 'none';
                answerContainer.style.display = 'none';
                loadingSpinner.style.display = 'block';
                
                // Send API request
                fetch(endpoint, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ question: question })
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok');
                    }
                    return response.json();
                })
                .then(data => {
                    loadingSpinner.style.display = 'none';
                    
                    // Display answer
                    answerText.innerHTML = data.answer;
                    
                    // Display source results
                    resultsList.innerHTML = '';
                    if (data.results && data.results.length > 0) {
                        data.results.forEach((result, index) => {
                            const resultCard = document.createElement('div');
                            resultCard.className = 'card result-card';
                            
                            const cardBody = document.createElement('div');
                            cardBody.className = 'card-body';
                            
                            const relevanceText = document.createElement('small');
                            relevanceText.className = 'text-muted d-block mb-2';
                            relevanceText.textContent = `Relevance: ${(result.score * 100).toFixed(2)}%`;
                            
                            const questionText = document.createElement('h5');
                            questionText.className = 'card-title';
                            questionText.textContent = result.question;
                            
                            const answerText = document.createElement('p');
                            answerText.className = 'card-text';
                            answerText.textContent = result.answer;
                            
                            const previewTitle = document.createElement('p');
                            previewTitle.className = 'card-text mt-2 mb-1 fw-bold';
                            previewTitle.textContent = 'Conversation preview:';
                            
                            const previewText = document.createElement('p');
                            previewText.className = 'card-text small text-muted';
                            previewText.textContent = result.preview;
                            
                            cardBody.appendChild(relevanceText);
                            cardBody.appendChild(questionText);
                            cardBody.appendChild(answerText);
                            cardBody.appendChild(previewTitle);
                            cardBody.appendChild(previewText);
                            resultCard.appendChild(cardBody);
                            resultsList.appendChild(resultCard);
                        });
                    } else {
                        resultsList.innerHTML = '<p>No source conversations found.</p>';
                    }
                    
                    answerContainer.style.display = 'block';
                })
                .catch(error => {
                    loadingSpinner.style.display = 'none';
                    showError('Error fetching answer: ' + error.message);
                    console.error('Error:', error);
                });
            });
            
            function showError(message, type = 'error') {
                errorMessage.textContent = message;
                errorMessage.style.display = 'block';
                
                if (type === 'success') {
                    errorMessage.style.color = '#198754';
                } else {
                    errorMessage.style.color = '#dc3545';
                }
                
                // Hide after 3 seconds if it's a success message
                if (type === 'success') {
                    setTimeout(() => {
                        errorMessage.style.display = 'none';
                    }, 3000);
                }
            }
        });
    </script>
</body>
</html>