-
Notifications
You must be signed in to change notification settings - Fork 23
/
modal_configuration.py
506 lines (419 loc) · 17.8 KB
/
modal_configuration.py
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
from datetime import datetime, timedelta, timezone
from typing import AsyncIterator
import panel as pn
import param
from ragna.deploy import _schemas as schemas
from . import js
from . import styles as ui
from .components.metadata_filters_builder import NO_CORPUS_KEY, MetadataFiltersBuilder
USE_CORPUS_LABEL = "Use existing corpus"
USE_UPLOAD_LABEL = "Upload new documents"
def get_default_chat_name(timezone_offset=None):
if timezone_offset is None:
return f"Chat {datetime.now():%m/%d/%Y %I:%M %p}"
else:
tz = timezone(offset=timedelta(minutes=timezone_offset))
return f"Chat {datetime.now().astimezone(tz=tz):%m/%d/%Y %I:%M %p}"
class ChatConfig(param.Parameterized):
allowed_documents = param.List(default=["TXT"])
source_storage_name = param.Selector()
assistant_name = param.Selector()
chunk_size = param.Integer(
default=500,
step=100,
bounds=(100, 1_000),
)
chunk_overlap = param.Integer(
default=250,
step=50,
bounds=(50, 500),
)
max_context_tokens = param.Integer(
step=500,
bounds=(1, 8000),
default=2000,
doc=(
"Maximum number of context tokens and in turn the number of document chunks "
"pulled out of the vector database."
),
)
max_new_tokens = param.Integer(
default=1_000,
step=100,
bounds=(100, 10_000),
)
def is_assistant_disabled(self):
return "Ragna/Demo" in self.assistant_name
def is_source_storage_disabled(self):
return "Ragna/Demo" in self.source_storage_name
def to_params_dict(self):
result = {}
if not self.is_assistant_disabled():
result["max_new_tokens"] = self.max_new_tokens
if not self.is_source_storage_disabled():
result["chunk_overlap"] = self.chunk_overlap
result["chunk_size"] = self.chunk_size
result["num_tokens"] = self.max_context_tokens
return result
class ModalConfiguration(pn.viewable.Viewer):
chat_name = param.String()
config = param.ClassSelector(class_=ChatConfig, default=None)
new_chat_ready_callback = param.Callable()
cancel_button_callback = param.Callable()
advanced_config_collapsed = param.Boolean(default=True)
corpus_or_upload = param.Selector(
objects=[USE_CORPUS_LABEL, USE_UPLOAD_LABEL], default=USE_CORPUS_LABEL
)
error = param.Boolean(default=False)
def __init__(
self, api_wrapper, components, corpus_names, corpus_metadata, **params
):
super().__init__(chat_name=get_default_chat_name(), **params)
self.api_wrapper = api_wrapper
self.corpus_names = corpus_names
self.corpus_metadata = corpus_metadata
self.chat_name_input = pn.widgets.TextInput.from_param(
self.param.chat_name, name=""
)
self.document_uploader = pn.widgets.FileInput(
multiple=True,
css_classes=["file-input"],
accept=",".join(self.api_wrapper.get_components().documents),
)
# Most widgets (including those that use from_param) should be placed after the super init call
self.cancel_button = pn.widgets.Button(
name="Cancel", button_type="default", min_width=375
)
self.cancel_button.on_click(self.cancel_button_callback)
self.corpus_name_input = pn.widgets.TextInput(
name="",
value="default",
placeholder="Enter name of corpus to upload data to (optional)",
width=335,
)
self.start_chat_button = pn.widgets.Button(
name="Start Conversation", button_type="primary", min_width=375
)
self.start_chat_button.on_click(self.did_click_on_start_chat_button)
self.upload_files_label = pn.pane.HTML()
self.upload_row = pn.Row(
self.document_uploader,
sizing_mode="stretch_width",
css_classes=["modal_configuration_upload_row"],
)
self.got_timezone = False
self.corpus_or_upload_radiobutton = pn.widgets.RadioButtonGroup.from_param(
self.param.corpus_or_upload,
button_style="outline",
button_type="primary",
)
self.metadata_filter_rows_title = pn.pane.HTML(
"<b>Available Corpuses</b> (required)"
)
self.metadata_filter_rows = None
self.change_upload_files_label()
self.create_config(components)
async def did_click_on_start_chat_button(self, event):
if self.corpus_or_upload == USE_UPLOAD_LABEL:
if not self.document_uploader.value:
self.change_upload_files_label("missing_file")
return
self.start_chat_button.disabled = True
documents = self.api_wrapper._engine.register_documents(
user=self.api_wrapper._user,
document_registrations=[
schemas.DocumentRegistration(name=name)
for name in self.document_uploader.filename
],
)
if self.api_wrapper._engine.supports_store_documents:
def make_content_stream(data: bytes) -> AsyncIterator[bytes]:
async def content_stream() -> AsyncIterator[bytes]:
yield data
return content_stream()
await self.api_wrapper._engine.store_documents(
user=self.api_wrapper._user,
ids_and_streams=[
(document.id, make_content_stream(data))
for document, data in zip(
documents, self.document_uploader.value
)
],
)
input = [str(document.id) for document in documents]
corpus_name = self.corpus_name_input.value
else: # self.corpus_or_upload == USE_CORPUS_LABEL:
if not self.metadata_filter_rows:
return
if self.metadata_filter_rows.corpus_names_select.value == NO_CORPUS_KEY:
self.change_upload_files_label("no_corpus_available")
return
self.start_chat_button.disabled = True
input = self.metadata_filter_rows.construct_metadata_filters()
corpus_name = self.metadata_filter_rows.corpus_names_select.value
await self.did_finish_upload(input, corpus_name)
async def did_finish_upload(self, input, corpus_name=None):
if corpus_name is None:
corpus_name = self.corpus_name_input.value
try:
new_chat_id = await self.api_wrapper.start_and_prepare(
name=self.chat_name,
input=input,
corpus_name=corpus_name,
source_storage=self.config.source_storage_name,
assistant=self.config.assistant_name,
params=self.config.to_params_dict(),
)
self.start_chat_button.disabled = False
if self.new_chat_ready_callback is not None:
await self.new_chat_ready_callback(new_chat_id)
except Exception as exc:
import traceback
print(
"".join(traceback.format_exception(type(exc), exc, exc.__traceback__))
)
self.change_upload_files_label("upload_error")
self.document_uploader.loading = False
self.start_chat_button.disabled = False
def change_upload_files_label(self, mode="normal"):
self.error = False
if mode == "upload_error":
self.upload_files_label.object = "<b>Upload files</b> (required)<span style='color:red;padding-left:100px;'><b>An error occured. Please try again or contact your administrator.</b></span>"
self.error = True
elif mode == "missing_file":
self.upload_files_label.object = (
"<span style='color:red;'><b>Upload files</b> (required)</span>"
)
self.error = True
elif mode == "no_corpus_available":
self.error = True
else:
self.upload_files_label.object = "<b>Upload files</b> (required)"
def create_config(self, components):
if self.config is None:
# Retrieve the components from the API and build a config object
components = self.api_wrapper.get_components()
# TODO : use the components to set up the default values for the various params
config = ChatConfig()
config.allowed_documents = components.documents
assistants = [assistant["title"] for assistant in components.assistants]
config.param.assistant_name.objects = assistants
config.assistant_name = assistants[0]
source_storages = [
source_storage["title"] for source_storage in components.source_storages
]
config.param.source_storage_name.objects = source_storages
config.source_storage_name = source_storages[0]
# Now that the config object is set, we can assign it to the param.
# This will trigger the update of the advanced_config_ui section
self.config = config
def model_section(self):
return pn.Row(
pn.Column(
pn.pane.HTML("<b>Assistants</b>"),
pn.widgets.Select.from_param(
self.config.param.assistant_name,
name="",
),
),
pn.Column(
pn.pane.HTML("<b>Source storage</b>"),
pn.widgets.Select.from_param(
self.config.param.source_storage_name,
name="",
),
),
)
def make_corpus_card(self):
disabled_assistant = self.config.is_assistant_disabled()
assistant_css_classes = [
"modal_configuration_int_slider",
*(["disabled"] if disabled_assistant else []),
]
return pn.Card(
pn.widgets.IntSlider.from_param(
self.config.param.max_new_tokens,
bar_color=ui.MAIN_COLOR,
css_classes=assistant_css_classes,
disabled=disabled_assistant,
margin=(0, 0, 0, 11),
),
collapsed=self.advanced_config_collapsed,
collapsible=True,
hide_header=True,
css_classes=["modal_configuration_advanced_card"],
)
def make_documents_card(self):
disabled_assistant = self.config.is_assistant_disabled()
disabled_source_storage = self.config.is_source_storage_disabled()
source_storage_css_classes = [
"modal_configuration_int_slider",
*(["disabled"] if disabled_source_storage else []),
]
assistant_css_classes = [
"modal_configuration_int_slider",
*(["disabled"] if disabled_assistant else []),
]
return pn.Card(
pn.Row(
pn.Column(
pn.widgets.IntSlider.from_param(
self.config.param.chunk_size,
name="Chunk Size",
bar_color=ui.MAIN_COLOR,
css_classes=source_storage_css_classes,
width_policy="max",
disabled=disabled_source_storage,
),
pn.widgets.IntSlider.from_param(
self.config.param.chunk_overlap,
name="Chunk Overlap",
bar_color=ui.MAIN_COLOR,
css_classes=source_storage_css_classes,
width_policy="max",
disabled=disabled_source_storage,
),
width_policy="max",
),
pn.Column(
pn.widgets.IntSlider.from_param(
self.config.param.max_context_tokens,
bar_color=ui.MAIN_COLOR,
css_classes=source_storage_css_classes,
width_policy="max",
disabled=disabled_source_storage,
),
pn.widgets.IntSlider.from_param(
self.config.param.max_new_tokens,
bar_color=ui.MAIN_COLOR,
css_classes=assistant_css_classes,
width_policy="max",
disabled=disabled_assistant,
),
width_policy="max",
height_policy="max",
styles={
"border-left": "1px solid var(--neutral-stroke-divider-rest)"
},
),
width=ui.CONFIG_MODAL_WIDTH,
),
collapsed=self.advanced_config_collapsed,
collapsible=True,
hide_header=True,
css_classes=["modal_configuration_advanced_card"],
)
def advanced_config(self, is_corpus=False):
card = self.make_corpus_card() if is_corpus else self.make_documents_card()
if is_corpus:
toggle_button = pn.widgets.Button(
name="Advanced Configuration of Assistants ▶",
button_type="light",
css_classes=["modal_configuration_toggle_button"],
)
else:
toggle_button = pn.widgets.Button(
name="Advanced Configuration of Assistants and Source Storages ▶",
button_type="light",
css_classes=["modal_configuration_toggle_button"],
)
if card.collapsed:
toggle_button.name = toggle_button.name.replace("▼", "▶")
else:
toggle_button.name = toggle_button.name.replace("▶", "▼")
def toggle_card(event):
if event.old < event.new:
# This callback is triggered when the card is rerendered,
# after changing the assistant, for example.
# This test prevents collapsing the card when it is not needed
card.collapsed = not card.collapsed
self.advanced_config_collapsed = card.collapsed
toggle_button = event.obj
if card.collapsed:
toggle_button.name = toggle_button.name.replace("▼", "▶")
else:
toggle_button.name = toggle_button.name.replace("▶", "▼")
toggle_button.on_click(toggle_card)
return pn.Column(toggle_button, card)
@pn.depends(
"corpus_or_upload",
"config",
"config.assistant_name",
"config.source_storage_name",
)
def corpus_or_upload_config(self):
if self.corpus_or_upload == USE_CORPUS_LABEL:
return self.advanced_config(is_corpus=True)
else:
return self.advanced_config(is_corpus=False)
@pn.depends("advanced_config_collapsed", watch=True)
def shrink_upload_container_height(self):
if self.advanced_config_collapsed:
self.document_uploader.height_upload_container = ui.FILE_CONTAINER_HEIGHT
else:
self.document_uploader.height_upload_container = (
ui.FILE_CONTAINER_HEIGHT_REDUCED
)
@pn.depends("error", watch=True)
def add_error_message(self):
if self.error:
text = "<b>Available Corpuses</b> (required)<span style='color:red;padding-left:100px;'><b>There are no available corpuses to chat with.</b></span>"
self.metadata_filter_rows_title.object = text
else:
self.metadata_filter_rows_title.object = (
"<b>Available Corpuses</b> (required)"
)
@pn.depends(
"corpus_or_upload",
"config.source_storage_name",
)
def corpus_or_upload_row(self):
if self.corpus_or_upload == USE_CORPUS_LABEL:
if self.config.source_storage_name in self.corpus_names:
corpus_names = self.corpus_names[self.config.source_storage_name]
else:
corpus_names = []
if self.config.source_storage_name in self.corpus_metadata:
corpus_metadata = self.corpus_metadata[self.config.source_storage_name]
else:
corpus_metadata = {}
self.metadata_filter_rows = MetadataFiltersBuilder(
corpus_names=corpus_names, corpus_metadata=corpus_metadata
)
if len(corpus_names) > 0:
data = pn.Column(
self.metadata_filter_rows_title, self.metadata_filter_rows
)
else:
data = pn.Column(self.metadata_filter_rows)
self.error = False
return data
else:
return pn.Column(
pn.pane.HTML("<b>Corpus Name</b>"),
self.corpus_name_input,
self.upload_files_label,
self.upload_row,
)
def __panel__(self):
return pn.Column(
pn.pane.HTML(
f"""<h2 style="margin-bottom: 5px;">Start a new chat</h2>
<script>{js.set_modal_size(ui.CONFIG_MODAL_WIDTH, ui.CONFIG_MODAL_HEIGHT)}</script>
""",
),
self.corpus_or_upload_radiobutton,
ui.divider(),
pn.pane.HTML("<b>Chat name</b>"),
self.chat_name_input,
ui.divider(),
self.model_section,
ui.divider(),
self.corpus_or_upload_config,
ui.divider(),
self.corpus_or_upload_row,
pn.Row(self.cancel_button, self.start_chat_button),
min_height=ui.CONFIG_MODAL_HEIGHT,
min_width=ui.CONFIG_MODAL_WIDTH,
styles={"overflow-y": "hidden"},
)