-
Notifications
You must be signed in to change notification settings - Fork 22
/
central_view.py
676 lines (564 loc) · 24.8 KB
/
central_view.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import uuid
import panel as pn
import param
from panel.reactive import ReactiveHTML
from . import styles as ui
# TODO : move all the CSS rules in a dedicated file
chat_entry_stylesheets = [
"""
:host .right, :host .center, :host .chat-entry {
width:100% !important;
}
""",
"""
:host div.bk-panel-models-layout-Column:not(.left) {
width:100% !important;
}
""",
"""
:host .message {
width: calc(100% - 15px);
box-shadow: unset;
}
""",
"""
:host .chat-entry-user {
background-color: rgba(243, 243, 243);
border: 1px solid rgb(238, 238, 238);
margin-bottom: 20px;
}
""",
# The padding bottom is used to give some space for the copy and source info buttons
"""
:host .chat-entry-ragna, :host .chat-entry-system, :host .chat-entry-assistant{
background-color: white;
border: 1px solid rgb(234, 234, 234);
padding-bottom: 30px;
margin-bottom: 20px;
}
""",
"""
:host .avatar {
margin-top:0px;
box-shadow: unset;
}
""",
"""
:host .left {
height: unset !important;
min-height: unset !important;
}
""",
"""
:host .right {
}
""",
]
pn.chat.ChatMessage._stylesheets = (
pn.chat.ChatMessage._stylesheets + chat_entry_stylesheets
)
markdown_table_stylesheet = """
/* Better rendering of the markdown tables */
table {
margin-top:10px;
margin-bottom:10px;
}
"""
class RagnaChatCopyIcon(ReactiveHTML):
title = param.String(default=None, doc="The title of the button ")
value = param.String(default=None, doc="The text to copy to the clipboard.")
_template = """
<div type="button"
id="copy-button"
onclick="${script('copy_to_clipboard')}"
class="container"
style="cursor: pointer;"
>
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clipboard" width="16" height="16"
viewBox="0 0 24 24" stroke-width="2" stroke="gray" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M9 5h-2a2 2 0 0 0 -2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-12a2 2 0 0 0 -2 -2h-2" />
<path d="M9 3m0 2a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v0a2 2 0 0 1 -2 2h-2a2 2 0 0 1 -2 -2z" />
</svg>
<span>${title}</span>
</div>
"""
_scripts = {
"copy_to_clipboard": """navigator.clipboard.writeText(`${data.value}`);"""
}
_stylesheets = [
""" div.container {
display:flex;
flex-direction: row;
margin: 7px 10px;
color:gray;
}
svg {
margin-right:5px;
}
"""
]
class RagnaChatMessage(pn.chat.ChatMessage):
msg_data = param.Dict(default={})
on_click_source_info_callback = param.Callable(default=None)
def __init__(self, msg_data, user, on_click_source_info_callback=None, **kwargs):
self.role = msg_data["role"]
params = {
"msg_data": msg_data,
# user is the name of the assistant (eg 'Ragna/DemoAssistant')
# or the name of the user, depending on the role
"user": user,
"on_click_source_info_callback": on_click_source_info_callback,
"object": msg_data["content"],
"renderers": [
lambda txt: RagnaChatMessage.chat_entry_value_renderer(
txt, role=self.role
)
],
"show_timestamp": False,
"show_reaction_icons": False,
"show_copy_icon": False,
"show_user": False,
}
params["avatar"] = RagnaChatMessage.get_avatar(self.role, user)
super().__init__(**(params | kwargs))
self.update_css_classes()
self.chat_copy_icon.visible = False
if self.role == "assistant":
source_info_button = pn.widgets.Button(
name="Source Info",
icon="info-circle",
stylesheets=[
ui.CHAT_INTERFACE_CUSTOM_BUTTON,
],
)
source_info_button.on_click(self.trigger_on_click_source_info_callback)
copy_button = RagnaChatCopyIcon(
value=self.object,
title="Copy",
stylesheets=[
ui.CHAT_INTERFACE_CUSTOM_BUTTON,
],
)
self._composite[1].append(pn.Row(copy_button, source_info_button, height=0))
def trigger_on_click_source_info_callback(self, event):
if self.on_click_source_info_callback is not None:
self.on_click_source_info_callback(event, self)
def update_css_classes(self):
role = self.msg_data["role"] if "role" in self.msg_data else None
self.css_classes = ["chat-entry", f"chat-entry-{role}"]
@classmethod
def get_avatar(cls, role, user) -> str:
if role == "system":
return "imgs/ragna_logo.svg"
elif role == "user":
# FIXME: user needs to be dynamic based on the username that was logged in with
return "👤"
elif role == "assistant":
# FIXME: This needs to represent the assistant somehow
if user == "Ragna/DemoAssistant":
return "imgs/ragna_logo.svg"
elif user.startswith("OpenAI/gpt-3.5"):
return pn.chat.message.GPT_3_LOGO
elif user == "OpenAI/gpt-4":
return pn.chat.message.GPT_4_LOGO
return "🤖"
# should never happen
return "?"
@classmethod
def chat_entry_value_renderer(cls, txt, role):
markdown_css_classes = []
if role is not None:
markdown_css_classes = [
f"chat-entry-{role}",
]
return pn.pane.Markdown(
txt,
css_classes=markdown_css_classes,
stylesheets=[markdown_table_stylesheet],
)
class RagnaChatInterface(pn.chat.ChatInterface):
def __init__(self, *objects, **params):
super().__init__(*objects, **params)
@param.depends("placeholder_text", watch=True, on_init=True)
def _update_placeholder(self):
loading_avatar = RagnaChatMessage.get_avatar("system", None)
self._placeholder = RagnaChatMessage(
{
"role": "system",
"content": ui.message_loading_indicator,
},
user=" ",
show_timestamp=False,
avatar=loading_avatar,
reaction_icons={},
show_copy_icon=False,
)
class CentralView(pn.viewable.Viewer):
current_chat = param.ClassSelector(class_=dict, default=None)
trigger_scroll_to_latest = param.Integer(default=0)
def __init__(self, api_wrapper, **params):
super().__init__(**params)
self.api_wrapper = api_wrapper
self.chat_info_button = pn.widgets.Button(
# The name will be filled at runtime in self.header
name="",
on_click=self.on_click_chat_info_wrapper,
button_style="outline",
icon="info-circle",
stylesheets=[":host { margin-top:10px; }"],
)
self.on_click_chat_info = None
def on_click_chat_info_wrapper(self, event):
if self.on_click_chat_info is not None:
pills = "".join(
[
f"""<div class='chat_document_pill'>{d['name']}</div>"""
for d in self.current_chat["metadata"]["documents"]
]
)
grid_height = len(self.current_chat["metadata"]["documents"]) // 3
advanced_config_md = "\n".join(
f"- **{key.replace('_', ' ').title()}**: {value}"
for key, value in self.current_chat["metadata"]["params"].items()
)
markdown = [
"To change configurations, start a new chat.\n",
"**Uploaded Files**",
f"<div class='pills_list'>{pills}</div><br />\n\n",
"----",
"**Source Storage**",
f"""<span>{self.current_chat['metadata']['source_storage']}</span>\n""",
"----",
"**Assistant**",
f"""<span>{self.current_chat['metadata']['assistant']}</span>\n""",
"**Advanced configuration**",
advanced_config_md,
]
markdown = "\n".join(markdown)
self.on_click_chat_info(
event,
"Chat Config",
[
pn.pane.Markdown(
markdown,
dedent=True,
# debug
# pn.pane.Markdown(f"Chat ID: {self.current_chat['id']}"),
stylesheets=ui.stylesheets(
(":host", {"width": "100%"}),
(
".pills_list",
{
# "background-color": "gold",
"display": "grid",
"grid-auto-flow": "row",
"row-gap": "10px",
"grid-template": f"repeat({grid_height}, 1fr) / repeat(3, 1fr)",
"max-height": "200px",
"overflow": "scroll",
},
),
(
".chat_document_pill",
{
"background-color": "rgb(241,241,241)",
"margin-left": "5px",
"margin-right": "5px",
"padding": "5px 15px",
"border-radius": "10px",
"color": "var(--accent-color)",
"width": "fit-content",
"grid-column": "span 1",
},
),
("ul", {"list-style-type": "none"}),
),
),
],
)
def on_click_source_info_wrapper(self, event, msg):
if self.on_click_chat_info is not None:
markdown = "This response was generated using the following data from the uploaded files: <br />\n"
for i in range(len(msg.msg_data["sources"])):
source = msg.msg_data["sources"][i]
location = ""
if source["location"] != "":
location = f": {source['location']}"
markdown += (
f"""{(i+1)}. **{source['document']['name']}** {location}\n"""
)
markdown += "----\n"
self.on_click_chat_info(
event,
"Source Info",
[
pn.pane.Markdown(
markdown,
dedent=True,
stylesheets=[""" hr { width: 94%; height:1px; } """],
),
],
)
def set_current_chat(self, chat):
self.current_chat = chat
async def chat_callback(
self, contents: str, user: str, instance: pn.chat.ChatInterface
):
self.current_chat["messages"].append({"role": "user", "content": contents})
try:
answer = await self.api_wrapper.answer(self.current_chat["id"], contents)
self.current_chat["messages"].append(answer)
yield {
"user": "Ragna",
"avatar": "🤖",
"value": answer["content"],
}
except Exception as e:
print(e)
yield {
"user": "Ragna",
"avatar": RagnaChatMessage.get_avatar("system", None),
"value": "Sorry, something went wrong. If this problem persists, please contact your administrator.",
}
def get_chat_messages(self):
chat_entries = []
if self.current_chat is not None:
assistant = self.current_chat["metadata"]["assistant"]
# FIXME: user needs to be dynamic based on the username that was logged in with
username = "User"
for m in self.current_chat["messages"]:
chat_entry = RagnaChatMessage(
m,
username if m["role"] == "user" else assistant,
on_click_source_info_callback=self.on_click_source_info_wrapper,
)
chat_entries.append(chat_entry)
return chat_entries
@pn.depends("current_chat")
def chat_interface(self):
if self.current_chat is None:
return
chat_interface = RagnaChatInterface(
callback=self.chat_callback,
callback_user="Ragna",
show_rerun=False,
show_undo=False,
show_clear=False,
show_button_name=False,
view_latest=True,
sizing_mode="stretch_width",
auto_send_types=[],
widgets=[
pn.widgets.TextInput(
placeholder="Ask Ragna...",
stylesheets=[
""":host input[type="text"] {
border:none !important;
box-shadow: 0px 0px 6px 0px rgba(0, 0, 0, 0.2);
padding: 10px 10px 10px 15px;
}
:host input[type="text"]:focus {
box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.3);
}
"""
],
)
],
renderers=[
lambda txt: RagnaChatMessage.chat_entry_value_renderer(txt, role=None)
],
message_params={
"show_reaction_icons": False,
"show_user": False,
"show_copy_icon": False,
"show_timestamp": False,
# the proper avatar for the assistant is not when replacing the default ChatMessage objects
# with RagnaChatMessage objects.
"avatar_lookup": lambda user: "👤" if user == "User" else None,
},
)
chat_interface._card.stylesheets += [
"""
:host {
border:none !important;
}
.chat-feed-log {
padding-right: 18%;
margin-left: 18% ;
padding-top:25px !important;
}
.chat-interface-input-container {
margin-left:19%;
margin-right:20%;
margin-bottom: 20px;
}
"""
]
"""
By default, each new message is a ChatMessage object.
But for new messages from the AI, we want to have a RagnaChatMessage, that contains the msg data, the sources, etc.
I haven't found a better way than to watch for the `objects` param of chat_interface,
and replace the ChatMessage objects with RagnaChatMessage object.
We do it only for the new messages from the rag, not for the existing messages, neither for the messages from the user.
"""
def messages_changed(event):
if len(chat_interface.objects) != len(self.current_chat["messages"]):
return
assistant = self.current_chat["metadata"]["assistant"]
# FIXME: user needs to be dynamic based on the username that was logged in with
username = "User"
needs_refresh = False
for i in range(len(chat_interface.objects)):
msg = chat_interface.objects[i]
if not isinstance(msg, RagnaChatMessage) and msg.user != "User":
chat_interface.objects[i] = RagnaChatMessage(
self.current_chat["messages"][i],
username
if self.current_chat["messages"][i]["role"] == "user"
else assistant,
on_click_source_info_callback=self.on_click_source_info_wrapper,
)
msg = chat_interface.objects[i]
needs_refresh = True
if needs_refresh:
chat_interface._chat_log.param.trigger("objects")
chat_interface.param.watch(
messages_changed,
["objects"],
)
# Here, we build a list of RagnaChatMessages from the existing messages of this chat,
# and set them as the content of the chat interface
chat_interface.objects = self.get_chat_messages()
# Now that setting all the objects is done, we can watch the change of objects,
# ie new messages being appended to the chat. When that happens,
# make sure we scroll to the latest msg.
chat_interface.param.watch(
lambda event: self.param.trigger("trigger_scroll_to_latest"),
["objects"],
)
return chat_interface
@pn.depends("current_chat", "trigger_scroll_to_latest")
def scroll_to_latest_fix(self):
"""
This snippet needs to be re-rendered many times so the scroll-to-latest happens:
- each time the current chat changes, hence the pn.depends on current_chat
- each time a message is appended to the chat, hence the pn.depends on trigger_scroll_to_latest.
trigger_scroll_to_latest is triggered in the chat_interface method, when chat_interface.objects changes.
Twist : the HTML script node needs to have a different ID each time it is rendered,
otherwise the browser doesn't re-render it / doesn't execute the JS part.
Hence the random ID.
"""
random_id = str(uuid.uuid4())
return pn.pane.HTML(
"""<script id="{{RANDOM_ID}}" type="text/javascript">
setTimeout(() => {
var chatbox_scrolldiv = $$$('.chat-feed-log')[0];
chatbox_scrolldiv.scrollTop = chatbox_scrolldiv.scrollHeight;
}, 150);
</script>""".replace("{{RANDOM_ID}}", random_id)
)
@pn.depends("current_chat")
def header(self):
if self.current_chat is None:
return
current_chat_name = ""
if self.current_chat is not None:
current_chat_name = self.current_chat["metadata"]["name"]
chat_name_header = pn.pane.HTML(
f"<p>{current_chat_name}</p>",
sizing_mode="stretch_width",
stylesheets=[
"""
:host p {
max-width: 50%;
height:100%;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
margin: 0px 0px 0px 10px;
font-size:20px;
text-decoration: underline;
text-underline-offset: 4px;
/* I don't understand why this is necessary to vertically align the text ... */
line-height:250%;
}
"""
],
)
chat_documents_pills = []
if (
self.current_chat is not None
and "metadata" in self.current_chat
and "documents" in self.current_chat["metadata"]
):
doc_names = [d["name"] for d in self.current_chat["metadata"]["documents"]]
# FIXME: Instead of setting a hard limit of 20 documents here, this should
# scale automatically with the width of page
# See https://github.com/Quansight/ragna/issues/224
for doc_name in doc_names[:20]:
pill = pn.pane.HTML(
f"""<div class="chat_document_pill">{doc_name}</div>""",
stylesheets=[
"""
:host {
background-color: rgb(241,241,241);
margin-top: 15px;
margin-left: 5px;
margin-right: 5px;
padding: 5px 15px;
border-radius: 10px;
color:var(--accent-color);
}
"""
],
)
chat_documents_pills.append(pill)
self.chat_info_button.name = f"{self.current_chat['metadata']['assistant']} | {self.current_chat['metadata']['source_storage']}"
return pn.Row(
chat_name_header,
*chat_documents_pills,
self.chat_info_button,
stylesheets=[
""":host {
background-color: #F9F9F9;
border-bottom: 1px solid #EEEEEE;
width: 100% !important;
margin:0px;
height:54px;
}
:host div {
vertical-align: middle;
}
"""
],
)
def set_loading(self, is_loading):
self.main_column.loading = is_loading
def __panel__(self):
"""
The ChatInterface.view_latest option doesn't seem to work.
So to scroll to the latest message, we use some JS trick.
There might be a more elegant solution than running this after a timeout of 200ms,
but without it, the $$$ function isn't available yet.
And even if I add the $$$ here, the fix itself doesn't work and the chat doesn't scroll
to the bottom.
"""
self.main_column = pn.Column(
self.header,
self.chat_interface,
self.scroll_to_latest_fix,
sizing_mode="stretch_width",
stylesheets=[
""" :host {
background-color: #F9F9F9;
height:100%;
max-width: 100%;
margin-left: min(15px, 2%);
border-left: 1px solid #EEEEEE;
border-right: 1px solid #EEEEEE;
}
"""
],
)
return self.main_column