Repository navigation
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
420 lines (356 loc) · 14.8 KB
/
Copy pathstreamlit_app.py
File metadata and controls
420 lines (356 loc) · 14.8 KB
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
# ============================================
# ADVANCED RAG — STREAMLIT UI
# ============================================
import time
import streamlit as st
# ============================================
# PAGE CONFIG (must be first Streamlit call)
# ============================================
st.set_page_config(
page_title="Advanced RAG",
page_icon="◈",
layout="wide",
initial_sidebar_state="expanded",
)
# ============================================
# CUSTOM CSS
# ============================================
st.markdown(
"""
<style>
/* ─── Global ─────────────────────────────── */
[data-testid="stAppViewContainer"] { background: #0f1117; }
[data-testid="stSidebar"] {
background: #161b27;
border-right: 1px solid #1f2637;
}
/* ─── Typography ──────────────────────────── */
h1 { font-size: 1.5rem !important; font-weight: 700 !important; letter-spacing: -0.5px; }
h3 { font-size: 0.9rem !important; font-weight: 600 !important; color: #8b92a5 !important;
text-transform: uppercase; letter-spacing: 1px; margin-bottom: 0.5rem; }
/* ─── Chip row ────────────────────────────── */
.chip-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.chip {
display: inline-flex; align-items: center; gap: 4px;
padding: 3px 10px; border-radius: 20px; font-size: 11px; font-weight: 600;
letter-spacing: 0.3px;
}
.chip-on { background: #1a3a5c; color: #60b4ff; border: 1px solid #1e4f80; }
.chip-off { background: #1e1e2e; color: #555a6e; border: 1px solid #2a2d3e; }
.chip-hybrid { background: #1a3a2c; color: #4cde96; border: 1px solid #1e5040; }
.chip-vector { background: #2a2240; color: #b97bff; border: 1px solid #3d2e60; }
.chip-bm25 { background: #3a2c14; color: #ffb84d; border: 1px solid #5a4020; }
/* ─── Source card ─────────────────────────── */
.src-card {
background: #161b27; border: 1px solid #1f2637; border-radius: 8px;
padding: 12px 14px; margin-bottom: 8px;
}
.src-header { display: flex; justify-content: space-between; align-items: center;
margin-bottom: 6px; }
.src-title { font-size: 12px; font-weight: 600; color: #c0c8e0; }
.src-score { font-size: 11px; color: #4cde96; font-variant-numeric: tabular-nums; }
.src-meta { font-size: 11px; color: #555a6e; margin-bottom: 6px; }
.src-body { font-size: 12px; color: #8b92a5; line-height: 1.6;
max-height: 120px; overflow-y: auto;
border-top: 1px solid #1f2637; padding-top: 8px; margin-top: 4px; }
/* ─── Latency row ─────────────────────────── */
.lat-row { display: flex; gap: 8px; flex-wrap: wrap; }
.lat-badge {
background: #161b27; border: 1px solid #1f2637; border-radius: 6px;
padding: 4px 10px; font-size: 11px; color: #8b92a5;
}
.lat-badge b { color: #c0c8e0; }
/* ─── Divider ─────────────────────────────── */
.thin-hr { border: none; border-top: 1px solid #1f2637; margin: 10px 0; }
/* ─── Sidebar labels ──────────────────────── */
label[data-testid="stWidgetLabel"] > div {
font-size: 12px !important; color: #8b92a5 !important;
}
</style>
""",
unsafe_allow_html=True,
)
# ============================================
# LAZY IMPORT RAG PIPELINE
# ============================================
@st.cache_resource(show_spinner="Initialising RAG pipeline...")
def load_pipeline():
from rag.retrieval import (
retrieve_and_build_context,
generate_answer_stream,
generate_direct_answer_stream,
classify_query,
reranker,
)
return retrieve_and_build_context, generate_answer_stream, generate_direct_answer_stream, classify_query, reranker
# ============================================
# SESSION STATE
# ============================================
if "messages" not in st.session_state:
st.session_state.messages = []
if "pending_query" not in st.session_state:
st.session_state.pending_query = ""
# ============================================
# SIDEBAR
# ============================================
with st.sidebar:
st.markdown("## ◈ Advanced RAG")
st.markdown("<hr class='thin-hr'>", unsafe_allow_html=True)
st.markdown("### Retrieval")
retrieval_mode = st.radio(
"Mode",
options=["hybrid", "vector", "bm25"],
format_func=lambda x: {"hybrid": "Hybrid (Vector + BM25)",
"vector": "Vector Only",
"bm25": "BM25 Only"}[x],
index=0,
label_visibility="collapsed",
)
col_rrf, col_rerank = st.columns(2)
with col_rrf:
use_rrf = st.toggle(
"RRF Fusion",
value=True,
disabled=(retrieval_mode != "hybrid"),
help="Reciprocal Rank Fusion merges vector and BM25 rankings. Active in Hybrid mode only.",
)
with col_rerank:
use_reranker = st.toggle(
"Reranker",
value=False,
help="Cross-encoder reranker. Slow on CPU; enable only with GPU.",
)
st.markdown("<hr class='thin-hr'>", unsafe_allow_html=True)
st.markdown("### Parameters")
top_k = st.slider("Top-K results", 1, 20, 5)
candidate_k = st.slider("Candidate pool", 5, 50, 20)
with st.expander("Advanced thresholds & weights"):
vector_threshold = st.slider(
"Vector similarity threshold", 0.0, 1.0, 0.35, 0.01,
disabled=(retrieval_mode == "bm25"),
)
bm25_threshold = st.slider(
"BM25 score threshold", 0.0, 1.0, 0.15, 0.01,
disabled=(retrieval_mode == "vector"),
)
vector_weight = st.slider(
"Vector weight (RRF)", 0.0, 2.0, 1.0, 0.05,
disabled=(retrieval_mode != "hybrid"),
)
bm25_weight = st.slider(
"BM25 weight (RRF)", 0.0, 2.0, 0.7, 0.05,
disabled=(retrieval_mode != "hybrid"),
)
st.markdown("<hr class='thin-hr'>", unsafe_allow_html=True)
st.markdown("### Metadata Filters")
st.caption("Constrain ChromaDB retrieval by metadata fields.")
chunking_filter = st.selectbox(
"Chunking strategy",
["any", "semantic", "recursive"],
format_func=lambda x: "Any" if x == "any" else x.capitalize(),
)
source_filter = st.text_input(
"Source document (exact match)",
placeholder="e.g. paper_001",
)
st.markdown("<hr class='thin-hr'>", unsafe_allow_html=True)
if st.button("Clear chat", use_container_width=True):
st.session_state.messages = []
st.rerun()
# ============================================
# METADATA FILTER BUILDER
# ============================================
def build_metadata_filter(chunking, source):
conditions = []
if chunking != "any":
conditions.append({"chunking_strategy": {"$eq": chunking}})
if source.strip():
conditions.append({"source": {"$eq": source.strip()}})
if len(conditions) == 0:
return None
if len(conditions) == 1:
return conditions[0]
return {"$and": conditions}
# ============================================
# HEADER + STATUS CHIPS
# ============================================
st.markdown("# ◈ Advanced RAG Assistant")
mode_cls = {"hybrid": "chip-hybrid", "vector": "chip-vector", "bm25": "chip-bm25"}[retrieval_mode]
mode_icon = {"hybrid": "⚡", "vector": "●", "bm25": "◎"}[retrieval_mode]
rrf_active = use_rrf and retrieval_mode == "hybrid"
rrf_cls = "chip-on" if rrf_active else "chip-off"
rerank_cls = "chip-on" if use_reranker else "chip-off"
filter_chips = ""
if chunking_filter != "any":
filter_chips += f'<span class="chip chip-on">chunk:{chunking_filter}</span> '
if source_filter.strip():
filter_chips += f'<span class="chip chip-on">src:{source_filter.strip()}</span> '
st.markdown(
f"""<div class="chip-row">
<span class="chip {mode_cls}">{mode_icon} {retrieval_mode.upper()}</span>
<span class="chip {rrf_cls}">RRF {"ON" if rrf_active else "OFF"}</span>
<span class="chip {rerank_cls}">Reranker {"ON" if use_reranker else "OFF"}</span>
<span class="chip chip-off">top-{top_k} / pool-{candidate_k}</span>
{filter_chips}
</div>""",
unsafe_allow_html=True,
)
st.markdown("")
# ============================================
# RENDER HELPERS
# ============================================
def render_sources(sources):
if not sources:
st.caption("No sources returned.")
return
for src in sources:
score_str = f"{src.rerank_score:.4f}"
title_str = src.section_title or "—"
source_str = src.source or "unknown"
snippet = src.content[:420].replace("\n", " ")
if len(src.content) > 420:
snippet += " …"
st.markdown(
f"""<div class="src-card">
<div class="src-header">
<span class="src-title">{source_str}</span>
<span class="src-score">{score_str}</span>
</div>
<div class="src-meta">§ {title_str}</div>
<div class="src-body">{snippet}</div>
</div>""",
unsafe_allow_html=True,
)
def render_latency(latency_ms: dict):
labels = {
"vector_search_ms": "Vector",
"bm25_search_ms": "BM25",
"fusion_ms": "Fusion",
"reranker_ms": "Reranker",
"total_retrieval_ms": "Total",
}
badges = []
for key, label in labels.items():
val = latency_ms.get(key, 0.0)
if val > 0 or key == "total_retrieval_ms":
badges.append(
f'<span class="lat-badge"><b>{label}</b> {val:.0f} ms</span>'
)
st.markdown(
f'<div class="lat-row">{"".join(badges)}</div>',
unsafe_allow_html=True,
)
# ============================================
# CHAT HISTORY
# ============================================
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
if msg["role"] == "assistant":
sources = msg.get("sources", [])
latency = msg.get("latency_ms", {})
if sources:
with st.expander(f"📄 {len(sources)} source(s)"):
render_sources(sources)
if latency:
with st.expander("⏱ Latency"):
render_latency(latency)
# ============================================
# SAMPLE QUESTIONS
# ============================================
SAMPLE_QUESTIONS = [
"Which review dataset do they use?",
"Is there any ethical consideration in the research?",
"Which language is divided into six dialects in the task mentioned in the paper?",
"How is state to learn and complete tasks represented via natural language?",
"What out of domain datasets authors used for coarse-tuning stage?",
]
st.markdown("**Try a sample question:**")
cols = st.columns(len(SAMPLE_QUESTIONS))
for col, sample in zip(cols, SAMPLE_QUESTIONS):
with col:
if st.button(sample, use_container_width=True, key=f"sample_{sample[:20]}"):
st.session_state.pending_query = sample
st.rerun()
st.markdown("")
# ============================================
# QUERY INPUT
# ============================================
_prefill = st.session_state.pop("pending_query", "") if st.session_state.get("pending_query") else ""
query = st.chat_input("Ask a question about your documents…", key="chat_input")
# Use sample button value if set
if not query and _prefill:
query = _prefill
if query:
# User message
st.session_state.messages.append({"role": "user", "content": query})
with st.chat_message("user"):
st.markdown(query)
# Load pipeline (cached after first call)
retrieve_fn, stream_fn, direct_stream_fn, classify_fn, reranker_obj = load_pipeline()
if use_reranker and reranker_obj is None:
st.warning(
"Reranker model was not loaded at startup (reranker_enabled is false in "
"config.yaml). Running without reranker.",
icon="⚠️",
)
# ── Query routing ─────────────────────────
with st.status("Routing query…", expanded=False) as route_status:
route = classify_fn(query)
route_status.update(
label=f"Route: {'💬 conversational' if route == 'direct' else '🔍 retrieval'}",
state="complete",
)
if route == "direct":
# ── Direct conversational answer ───────
with st.chat_message("assistant"):
full_answer = st.write_stream(direct_stream_fn(query=query))
st.session_state.messages.append({
"role": "assistant",
"content": full_answer,
})
else:
# ── RAG: retrieve then generate ────────
with st.status("Retrieving context…", expanded=False) as status:
t0 = time.time()
try:
meta_filter = build_metadata_filter(chunking_filter, source_filter)
result = retrieve_fn(
query=query,
top_k=top_k,
candidate_k=candidate_k,
vector_threshold=vector_threshold,
bm25_threshold=bm25_threshold,
vector_weight=vector_weight,
bm25_weight=bm25_weight,
retrieval_mode_override=retrieval_mode,
use_rrf_override=rrf_active,
reranker_override=use_reranker,
metadata_filter=meta_filter,
)
elapsed = (time.time() - t0) * 1000
n_src = len(result.get("sources", []))
status.update(
label=f"Retrieved {n_src} chunk(s) in {elapsed:.0f} ms",
state="complete",
)
except Exception as exc:
status.update(label="Retrieval failed", state="error")
st.error(f"Retrieval error: {exc}")
st.stop()
with st.chat_message("assistant"):
full_answer = st.write_stream(
stream_fn(query=query, context=result["context"])
)
sources = result.get("sources", [])
latency = result.get("latency_ms", {})
with st.expander(f"📄 {len(sources)} source(s)"):
render_sources(sources)
with st.expander("⏱ Latency"):
render_latency(latency)
st.session_state.messages.append({
"role": "assistant",
"content": full_answer,
"sources": sources,
"latency_ms": latency,
})