File size: 12,303 Bytes
99afe26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f4bbb8
99afe26
 
1f4bbb8
 
99afe26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RAG question answer bot</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: black;
            font-family: 'Poppins', sans-serif;
            color: white;
        }
        .chat-container {
            max-width: 800px;
            margin: 50px auto;
            margin-top: 10%;
            padding: 20px;
            background-color: #333;
            border-radius: 10px;
        }
        .chat-heading {
            text-align: center;
            font-size: 2.5em;
            font-weight: 600;
            margin-bottom: 30px;
            color: #ffd700; /* Golden color for the heading */
        }
        .chat-input {
            margin-top: 20px; /* Added margin */
            margin-bottom: 20px;
            height: 100px; /* Increased height */
        }
        .chat-button {
            background-color: green;
            color: white;
            padding: 10px 20px;
            font-size: 1.2em;
        }
        .chat-response {
            background-color: #444;
            padding: 15px;
            border-radius: 5px;
            min-height: 100px; /* Minimum height for the response box */
            margin-top: 20px;
        }
        .accordion {

            background-color: #444;
            border-radius: 5px;
        }
        .accordion-button {
            color: white;
            background-color: #555;
        }
        .accordion-body {
            color: white; /* Improved visibility of text */
        }
        pre {
            white-space:pre-wrap;
        }
        #exampleQueries {
            margin-top: 10px;
            text-align: center;
        }

        #exampleQueries button {
            margin-right: 10px;
            margin-bottom: 5px;
        }


        /* The switch - the box around the slider */
        
        .language-toggle {
           text-align: right;
            margin: 16px;
            position: relative; /* Added position relative to align the label */
        }

        .switch {
        position: relative;
        display: inline-block;
        width: 60px;
        height: 34px;
        }

        /* Hide default HTML checkbox */
        .switch input {
        opacity: 0;
        width: 0;
        height: 0;
        }

        /* The slider */
        .slider {
        position: absolute;
        cursor: pointer;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: #ccc;
        -webkit-transition: .4s;
        transition: .4s;
        }

        .slider:before {
        position: absolute;
        content: "";
        height: 26px;
        width: 26px;
        left: 4px;
        bottom: 4px;
        background-color: white;
        -webkit-transition: .4s;
        transition: .4s;
        }

        input:checked + .slider {
        background-color: #2196F3;
        }

        input:focus + .slider {
        box-shadow: 0 0 1px #2196F3;
        }

        input:checked + .slider:before {
        -webkit-transform: translateX(26px);
        -ms-transform: translateX(26px);
        transform: translateX(26px);
        }

        /* Rounded sliders */
        .slider.round {
        border-radius: 34px;
        }

        .slider.round:before {
        border-radius: 50%;
        }

    </style>

</head>
<body>
    <div class="container chat-container">
        <h1 class="chat-heading">RAG Search</h1>


        <!-- Tab Navigation -->
        <ul class="nav nav-tabs" id="myTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="retriever-tab" data-bs-toggle="tab" data-bs-target="#retriever" type="button" role="tab" aria-controls="retriever" aria-selected="true">Retriever</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="ingest-tab" data-bs-toggle="tab" data-bs-target="#ingest" type="button" role="tab" aria-controls="ingest" aria-selected="false">Ingest</button>
            </li>
        </ul>


        <!-- Language Toggle Switch -->
        <!-- Tab Content -->
        <div class="tab-content" id="myTabContent">
            <!-- Retriever Tab Pane -->
            <div class="tab-pane fade show active" id="retriever" role="tabpanel" aria-labelledby="retriever-tab">
                <!-- Language Toggle Switch -->
                <div class="language-toggle">
                    <label class="switch">
                        <input type="checkbox" class="languageCheckbox"> <!-- Changed to class -->
                        <span class="slider round"></span>
                    </label>
                    <span class="languageLabel">English</span> <!-- Changed to class -->
                </div>
                
        <div class="accordion" id="appDescriptionAccordion">
            <div class="accordion-item">
                <h2 class="accordion-header" id="descriptionHeading">
                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseDescription" aria-expanded="true" aria-controls="collapseDescription">
                        About This App
                    </button>
                </h2>

                <div id="collapseDescription" class="accordion-collapse collapse" aria-labelledby="descriptionHeading" data-bs-parent="#appDescriptionAccordion">
                    <div class="accordion-body text-dark">
                        This is a RAG implementation using Open Source stack. Intel's Neural Chat has been used to build this app along with BGE Embeddings as an embedding model, Chroma DB as a vector store, and Langchain & CTransformers as an orchestration frameworks.
                    </div>
                </div>
            </div>
        </div>
        <!-- Example Queries Section -->
        <div id="exampleQueries" class="mb-3">
            <h2 class="h5">Try Example Queries:</h2>
            <button class="btn btn-sm btn-secondary example-query">What cable do I use to hang a 1.5kg heavy luminaire on?</button>
            <button class="btn btn-sm btn-secondary example-query">What is the minimal gauge of live wires ?</button>
            <button class="btn btn-sm btn-secondary example-query">What flamability fequirements do plastic enclosure have to meet ?</button>
            <button class="btn btn-sm btn-secondary example-query">Jaké parametry musí splňovat kovový kryt živých částí ?</button>
        
        </div>

        <div class="row">
            <div class="col">
                <textarea id="userInput" class="form-control chat-input" placeholder="Type your query here..."></textarea>
                <button id="submitBtn" class="btn chat-button">Submit</button>
                <div id="response" class="chat-response"></div>
            </div>
        </div>
    </div>



                <!-- Ingest Tab Pane -->
                <div class="tab-pane fade" id="ingest" role="tabpanel" aria-labelledby="ingest-tab">
                        <!-- Language Toggle Switch -->
                        <div class="language-toggle">
                            <label class="switch">
                                <input type="checkbox" class="languageCheckbox"> <!-- Changed to class -->
                                <span class="slider round"></span>
                            </label>
                            <span class="languageLabel">English</span> <!-- Changed to class -->
                        </div>

                    <!-- Ingest Content -->
                    <h2>Document Ingestion</h2>
                    <input type="text" id="folderPath" class="form-control my-2" placeholder="Enter the path to your data folder" />
                    <button id="ingestBtn" class="btn chat-button">Ingest</button>
                    <div id="ingestResponse" class="chat-response"></div>
                </div>
            </div>
        </div>




    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Function to update language labels and synchronize toggle states
        function updateLanguageTogglesAndLabels(checked) {
            document.querySelectorAll('.languageCheckbox').forEach(function(checkbox) {
                checkbox.checked = checked; // Synchronize toggle state
            });
            document.querySelectorAll('.languageLabel').forEach(function(label) {
                label.textContent = checked ? "Czech" : "English"; // Update all labels
            });
        }

        // Attach event listeners to all language toggle switches
        document.querySelectorAll('.languageCheckbox').forEach(function(checkbox) {
            checkbox.addEventListener('change', function() {
                updateLanguageTogglesAndLabels(this.checked);
            });
        });

        document.getElementById('submitBtn').addEventListener('click', async function() {
            var userInput = document.getElementById('userInput').value;
            
            var languageCheckbox = document.querySelector('.languageCheckbox').checked;   
            var language = languageCheckbox ? "czech" : "english";
            document.getElementById('response').innerHTML = '<p>Processing...</p>';
            const formData = new FormData();
            formData.append('query', userInput);
            formData.append('language', language);

            try {
                const response = await fetch('/get_response', {
                    method: 'POST',
                    body: formData
                });

                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }

                const data = await response.json();
                document.getElementById('response').innerHTML = `<p>${data.answer}</p><br><pre><b>Context: </b> ${data.source_document}</pre><br><pre><b>Source Document: </b> ${data.doc}</pre>`;
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('response').innerHTML = '<p>Error processing your request</p>';
            }
        });

        // Add event listeners to example queries
        document.querySelectorAll('.example-query').forEach(item => {
            item.addEventListener('click', function() {
                document.getElementById('userInput').value = this.textContent; // Insert clicked query into textarea
            });
        });

        // Ingest data
        document.getElementById('ingestBtn').addEventListener('click', async function() {
            var folderPath = document.getElementById('folderPath').value;
            var languageCheckbox = document.querySelector('.languageCheckbox').checked;
            var language = languageCheckbox ? "czech" : "english"; // Determine the language based on the checkbox
            document.getElementById('ingestResponse').innerHTML = '<p>Starting ingestion...</p>';
            try {
                const response = await fetch('/ingest_data', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                    },
                    body: `folderPath=${folderPath}&language=${language}` // Include the language in the request body
                });
        
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
        
                const data = await response.json();
                document.getElementById('ingestResponse').innerHTML = `<p>${data.message}</p>`;
            } catch (error) {
                console.error('Error:', error);
                document.getElementById('ingestResponse').innerHTML = '<p>Error during ingestion process</p>';
            }
        });
    </script>
</body>
</html>