Skip to content

Commit 3526e4c

Browse files
pmeierpierrotsmnrd
andauthored
return source content from the REST API (#264)
Co-authored-by: Pierre-Olivier Simonard <[email protected]>
1 parent c08a223 commit 3526e4c

File tree

7 files changed

+133
-8
lines changed

7 files changed

+133
-8
lines changed

ragna/deploy/_api/database.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ def _orm_to_schema_chat(chat: orm.Chat) -> schemas.Chat:
113113
id=source.id,
114114
document=_orm_to_schema_document(source.document),
115115
location=source.location,
116+
content=source.content,
117+
num_tokens=source.num_tokens,
116118
)
117119
for source in message.sources
118120
],
@@ -170,6 +172,8 @@ def _schema_to_orm_source(session: Session, source: schemas.Source) -> orm.Sourc
170172
id=source.id,
171173
document_id=source.document.id,
172174
location=source.location,
175+
content=source.content,
176+
num_tokens=source.num_tokens,
173177
)
174178
session.add(orm_source)
175179
session.commit()

ragna/deploy/_api/orm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Source(Base):
8484
document = relationship("Document", back_populates="sources")
8585

8686
location = Column(types.String)
87+
content = Column(types.String)
88+
num_tokens = Column(types.Integer)
8789

8890
messages = relationship(
8991
"Message",

ragna/deploy/_api/schemas.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ class Source(BaseModel):
3737
id: str
3838
document: Document
3939
location: str
40+
content: str
41+
num_tokens: int
4042

4143
@classmethod
4244
def from_core(cls, source: ragna.core.Source) -> Source:
4345
return cls(
4446
id=source.id,
4547
document=Document.from_core(source.document),
4648
location=source.location,
49+
content=source.content,
50+
num_tokens=source.num_tokens,
4751
)
4852

4953

ragna/deploy/_ui/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
class App(param.Parameterized):
3232
def __init__(self, *, url, api_url, origins):
3333
super().__init__()
34+
ui.apply_design_modifiers()
3435
self.url = url
3536
self.api_url = api_url
3637
self.origins = origins

ragna/deploy/_ui/central_view.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,25 +324,32 @@ def on_click_source_info_wrapper(self, event, sources):
324324
if self.on_click_chat_info is None:
325325
return
326326

327-
markdown = [
328-
"This response was generated using the following data from the uploaded files: <br />"
329-
]
327+
source_infos = []
330328
for rank, source in enumerate(sources, 1):
331329
location = source["location"]
332330
if location:
333-
location = f": {location}"
334-
markdown.append(f"{rank}. **{source['document']['name']}**{location}")
335-
markdown.append("----")
331+
location = f": page(s) {location}"
332+
source_infos.append(
333+
(
334+
f"<b>{rank}. {source['document']['name']}</b> {location}",
335+
pn.pane.Markdown(source["content"], css_classes=["source-content"]),
336+
)
337+
)
336338

337339
self.on_click_chat_info(
338340
event,
339341
"Source Info",
340342
[
341343
pn.pane.Markdown(
342-
"\n".join(markdown),
344+
"This response was generated using the following data from the uploaded files: <br />",
343345
dedent=True,
344346
stylesheets=[""" hr { width: 94%; height:1px; } """],
345347
),
348+
pn.layout.Accordion(
349+
*source_infos,
350+
header_background="transparent",
351+
stylesheets=ui.stylesheets((":host", {"width": "100%"})),
352+
),
346353
],
347354
)
348355

ragna/deploy/_ui/right_sidebar.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __panel__(self):
6464
height:100%;
6565
min-width: unset;
6666
width: 0px;
67-
overflow:hidden;
67+
overflow:auto;
6868
6969
margin-left: min(15px, 2%);
7070
border-left: 1px solid #EEEEEE;
@@ -83,6 +83,7 @@ def __panel__(self):
8383
8484
:host(.visible_sidebar) {
8585
animation: 0.25s ease-in forwards show_right_sidebar;
86+
background-color: white;
8687
}
8788
8889
@keyframes show_right_sidebar {

ragna/deploy/_ui/styles.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,112 @@ def divider():
1010
return pn.layout.Divider(styles={"padding": "0em 1em"})
1111

1212

13+
def apply_design_modifiers():
14+
apply_design_modifiers_source_accordion()
15+
# add here calls to other design modifiers,
16+
# group them per UI component
17+
18+
19+
def apply_design_modifiers_source_accordion():
20+
pn.theme.fast.Fast.modifiers[pn.layout.Accordion] = {
21+
"stylesheets": [
22+
""" :host {
23+
height: 100%
24+
}
25+
"""
26+
]
27+
}
28+
29+
pn.theme.fast.Fast.modifiers[pn.layout.Card] = {
30+
"stylesheets": [
31+
"""
32+
33+
/* Define some variables */
34+
:host {
35+
--ragna-accordion-header-height: 50px;
36+
}
37+
38+
/* Resets some existing styles */
39+
:host(.accordion) {
40+
margin-left: 0px;
41+
margin-top: 0px;
42+
outline: none;
43+
}
44+
45+
/* Styles the button itself */
46+
button.accordion-header {
47+
background-color: white !important;
48+
height: var(--ragna-accordion-header-height);
49+
padding-top: 0px;
50+
padding-bottom: 0px;
51+
outline:0px;
52+
margin-left: 15px;
53+
margin-right: 15px;
54+
width: calc(100% - 30px);
55+
border-bottom: 2px solid #D9D9D9;
56+
57+
}
58+
59+
button.accordion-header div.card-button {
60+
font-size: 11px;
61+
padding-top: 5px;
62+
margin-left: 0px;
63+
margin-right: 10px;
64+
}
65+
66+
div.card-header-row {
67+
height: var(--ragna-accordion-header-height);
68+
background-color: unset !important;
69+
}
70+
71+
/* styles the content of the sources content (the expanding areas of the Accordion) */
72+
div.bk-panel-models-markup-HTML.markdown {
73+
margin-left: 15px;
74+
margin-right: 15px;
75+
margin-top:0px;
76+
}
77+
78+
"""
79+
]
80+
}
81+
82+
pn.theme.fast.Fast.modifiers[pn.pane.HTML] = {
83+
"stylesheets": [
84+
""" :host(.card-title) {
85+
height: var(--ragna-accordion-header-height);
86+
margin: 0px;
87+
}
88+
89+
:host(.card-title) div {
90+
height: var(--ragna-accordion-header-height);
91+
92+
display:flex;
93+
flex-direction:row;
94+
align-items:center;
95+
}
96+
97+
98+
:host(.card-title) h3 {
99+
font-weight: normal;
100+
}
101+
"""
102+
]
103+
}
104+
105+
pn.theme.fast.Fast.modifiers[pn.pane.Markdown] = {
106+
"stylesheets": [
107+
""" /* Styles the content of the sources content (the expanding areas of the Accordion).
108+
This fixes a small margin-top that is added by default and that leads to overflowing content
109+
in some cases.
110+
*/
111+
:host(.source-content) p:nth-of-type(1) {
112+
margin-top: 0px;
113+
}
114+
"""
115+
]
116+
}
117+
118+
13119
"""
14120
CSS constants
15121
"""

0 commit comments

Comments
 (0)