-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
796 lines (659 loc) · 30.4 KB
/
app.py
File metadata and controls
796 lines (659 loc) · 30.4 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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
"""
Semantic Search Engine - Streamlit Application
A modular RAG (Retrieval Augmented Generation) application that enables semantic search
over PDF documents using ChromaDB vector store and OpenAI embeddings.
Features:
- PDF document upload and processing
- Text chunking with configurable parameters
- Vector embeddings using OpenAI text-embedding-3-large
- Persistent ChromaDB vector store
- Hybrid retrieval (BM25 + semantic search)
- Re-ranking with Cohere/Jina
- Conversation history with follow-up optimization
- A/B testing framework for retrieval methods
- Question answering with GPT-4o-mini
- Context display for transparency
Environment Variables Required:
OPENAI_API_KEY: OpenAI API key for embeddings and chat
COHERE_API_KEY: (Optional) Cohere API key for re-ranking
Usage:
streamlit run app.py
Author: Harsh
Repository: https://github.com/shrimpy8/semantic-serach
"""
import time
import streamlit as st
import logging
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
from config_loader import load_config
from core import (
DocumentProcessor,
VectorStoreManager,
QAChain,
HybridRetriever,
RetrievalMethod,
create_hybrid_retriever,
ConversationManager,
ABTestingManager,
TestVariant
)
from utils import add_documents_with_retry, stream_llm_with_retry
from ui import (
apply_page_styles,
render_sidebar_header,
render_retrieval_settings,
render_configuration_display,
)
# Load configuration
config = load_config()
# Configure structured logging
logging_config = config.get_logging_config()
logging.basicConfig(
level=getattr(logging, logging_config["level"]),
format=logging_config["format"],
handlers=[
logging.FileHandler(logging_config["file"]),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def initialize_session_state():
"""Initialize all session state variables."""
if 'vector_store_manager' not in st.session_state:
st.session_state.vector_store_manager = VectorStoreManager(
embedding_model_name=config.get_embedding_model(),
collection_name=config.get_collection_name(),
persist_directory=config.get_persist_directory(),
use_docker=config.use_chroma_docker(),
chroma_host=config.get_chroma_host(),
chroma_port=config.get_chroma_port()
)
if 'qa_chain' not in st.session_state:
retriever = st.session_state.vector_store_manager.get_retriever(
search_type=config.get_search_type(),
search_k=config.get_search_k()
)
st.session_state.qa_chain = QAChain(
model_name=config.get_chat_model(),
temperature=config.get_chat_temperature(),
retriever=retriever,
system_prompt=config.get_qa_system_prompt()
)
if 'hybrid_retriever' not in st.session_state:
st.session_state.hybrid_retriever = None
if 'documents' not in st.session_state:
st.session_state.documents = []
# Flag for triggering sidebar refresh after upload
if 'needs_sidebar_refresh' not in st.session_state:
st.session_state.needs_sidebar_refresh = False
# Check for existing documents and restore state on restart
if 'processed_file' not in st.session_state:
existing_doc_count = st.session_state.vector_store_manager.get_non_collection_count()
if existing_doc_count > 0:
logger.info(f"Found {existing_doc_count} existing documents in vector store, restoring state")
st.session_state.processed_file = True
# Initialize hybrid retriever for existing documents
if st.session_state.hybrid_retriever is None:
semantic_retriever = st.session_state.vector_store_manager.get_retriever(
search_k=config.get_search_k() * config.get_fetch_k_multiplier()
)
# Get existing documents for BM25
existing_docs = st.session_state.vector_store_manager.get_all_documents()
st.session_state.hybrid_retriever = create_hybrid_retriever(
semantic_retriever=semantic_retriever,
documents=existing_docs,
enable_reranker=config.is_reranking_enabled(),
reranker_provider=config.get_reranker_provider(),
alpha=config.get_hybrid_alpha(),
rrf_k=config.get_rrf_k(),
bm25_k1=config.get_bm25_k1(),
bm25_b=config.get_bm25_b()
)
else:
st.session_state.processed_file = False
if 'conversation_manager' not in st.session_state:
st.session_state.conversation_manager = ConversationManager(
storage_dir=config.get_conversation_storage_dir(),
max_history=config.get_max_conversation_history()
)
if 'ab_testing_manager' not in st.session_state:
st.session_state.ab_testing_manager = ABTestingManager(
storage_dir=config.get_ab_testing_storage_dir()
)
if 'current_retrieval_method' not in st.session_state:
st.session_state.current_retrieval_method = config.get_default_retrieval_method()
def render_sidebar():
"""Render sidebar with configuration and management options."""
# Branding and navigation (shared component)
render_sidebar_header()
# Initialize preset in session state
if 'current_preset' not in st.session_state:
st.session_state.current_preset = config.get_default_preset()
# Retrieval settings (shared component)
render_retrieval_settings(config)
# Configuration display (shared component)
render_configuration_display(config)
@st.dialog("Clear All Documents")
def confirm_clear_documents():
"""Confirmation dialog for clearing non-collection documents.
This only clears documents uploaded directly to the home page.
Collection documents are managed separately on the Collections page.
"""
non_collection_count = st.session_state.vector_store_manager.get_non_collection_count()
if non_collection_count == 0:
st.info("No documents to clear. The database has no non-collection documents.")
if st.button("Close", use_container_width=True):
st.rerun()
return
st.warning(
f"⚠️ This will permanently delete **{non_collection_count}** document chunks "
"uploaded directly to this page."
)
st.caption("Note: Documents in collections are not affected. Manage them on the Collections page.")
st.markdown("Are you sure you want to continue?")
col1, col2 = st.columns(2)
with col1:
if st.button("Yes, Clear All", type="primary", use_container_width=True):
try:
deleted = st.session_state.vector_store_manager.clear_non_collection_documents()
st.session_state.processed_file = False
st.session_state.documents = []
st.session_state.hybrid_retriever = None
logger.info(f"Cleared {deleted} non-collection documents by user")
st.rerun()
except Exception as e:
st.error(f"Error clearing database: {str(e)}")
logger.error(f"Error clearing vector store: {e}", exc_info=True)
with col2:
if st.button("Cancel", use_container_width=True):
st.rerun()
def render_database_management():
"""Render database management section at bottom of sidebar.
Shows count of non-collection documents only. Collection documents
are managed separately through the Collections page.
"""
st.sidebar.markdown("---")
st.sidebar.markdown("### Database Management")
try:
# Show only non-collection document count
non_collection_count = st.session_state.vector_store_manager.get_non_collection_count()
if non_collection_count > 0:
st.sidebar.success(f"**{non_collection_count}** document chunks indexed")
else:
st.sidebar.info("No documents indexed. Upload a document to begin.")
except Exception as e:
st.sidebar.warning(f"Could not check database status: {str(e)}")
logger.error(f"Error checking database status: {e}")
if st.sidebar.button("Clear All Documents"):
confirm_clear_documents()
@st.dialog("Clear Conversation History")
def confirm_clear_history():
"""Confirmation dialog for clearing conversation history."""
st.warning("⚠️ This will delete the current conversation history.")
st.markdown("Are you sure you want to continue?")
col1, col2 = st.columns(2)
with col1:
if st.button("Yes, Clear", type="primary", use_container_width=True):
if st.session_state.conversation_manager.current_session:
st.session_state.conversation_manager.delete_session(
st.session_state.conversation_manager.current_session.session_id
)
st.session_state.conversation_manager.start_session()
logger.info("Conversation history cleared by user")
st.rerun()
with col2:
if st.button("Cancel", use_container_width=True):
st.rerun()
def render_conversation_history():
"""Render conversation history panel."""
st.sidebar.markdown("---")
st.sidebar.markdown("### Conversation History")
if st.session_state.conversation_manager.current_session:
history = st.session_state.conversation_manager.get_query_history(n=5)
if history:
with st.sidebar.expander(f"Recent Queries ({len(history)})", expanded=False):
for i, item in enumerate(reversed(history)):
st.markdown(f"**Q{len(history)-i}:** {item['query'][:50]}...")
st.caption(f"Method: {item['retrieval_method']}")
st.markdown("---")
# Session management
col1, col2 = st.sidebar.columns(2)
with col1:
if st.button("New Session"):
st.session_state.conversation_manager.start_session(
document_name=getattr(st.session_state, 'current_doc_name', None)
)
st.rerun()
with col2:
if st.button("Clear History"):
confirm_clear_history()
def render_documents_panel():
"""Render documents panel showing stats and document list."""
chunk_count = st.session_state.vector_store_manager.get_non_collection_count()
if chunk_count == 0:
return
with st.expander(f"📄 Documents ({chunk_count} chunks)", expanded=False):
# Stats row
stat_cols = st.columns(4)
with stat_cols[0]:
# Count unique documents by source
docs = st.session_state.vector_store_manager.get_all_documents()
unique_sources = set(doc.metadata.get("source", "Unknown") for doc in docs)
st.metric("Documents", len(unique_sources))
with stat_cols[1]:
st.metric("Chunks", chunk_count)
with stat_cols[2]:
st.metric("Chunk Size", config.get_chunk_size())
with stat_cols[3]:
st.metric("Overlap", config.get_chunk_overlap())
st.divider()
# Document list
if docs:
# Group chunks by source document
doc_stats = {}
for doc in docs:
source = doc.metadata.get("source", "Unknown")
if source not in doc_stats:
doc_stats[source] = {"chunks": 0, "pages": set()}
doc_stats[source]["chunks"] += 1
if doc.metadata.get("page"):
doc_stats[source]["pages"].add(doc.metadata.get("page"))
st.markdown("**Document List:**")
for source, stats in doc_stats.items():
# Extract just filename from path
filename = source.split("/")[-1] if "/" in source else source
page_info = f", {len(stats['pages'])} pages" if stats['pages'] else ""
st.markdown(f"📄 **{filename}** — {stats['chunks']} chunks{page_info}")
else:
st.info("No documents uploaded yet.")
def render_ab_testing_panel():
"""Render A/B testing panel."""
if not config.is_ab_testing_enabled():
return
with st.expander("A/B Testing", expanded=False):
st.markdown("### Compare Retrieval Methods")
if st.session_state.processed_file and st.session_state.hybrid_retriever:
test_query = st.text_input(
"Test Query",
placeholder="Enter a query to compare methods...",
key="ab_test_query"
)
if st.button("Run Comparison") and test_query:
with st.spinner("Running A/B comparison..."):
run_ab_comparison(test_query)
# Show comparison results
if st.session_state.ab_testing_manager.current_experiment:
summary = st.session_state.ab_testing_manager.get_comparison_summary()
if summary.get("total_tests", 0) > 0:
st.markdown("### Results")
# Create comparison table
cols = st.columns(len(summary.get("variants", {})))
for i, (variant, stats) in enumerate(summary.get("variants", {}).items()):
if stats:
with cols[i]:
st.metric(
label=variant.upper(),
value=f"{stats.get('avg_score', {}).get('mean', 0):.3f}",
delta=f"{stats.get('latency', {}).get('mean', 0):.0f}ms"
)
if summary.get("recommendation", {}).get("best_variant"):
st.success(
f"Recommended: **{summary['recommendation']['best_variant']}** "
f"(avg score: {summary['recommendation']['best_avg_score']:.3f})"
)
# Export button
if st.button("Export Results (CSV)"):
csv_data = st.session_state.ab_testing_manager.export_results("csv")
if csv_data:
st.download_button(
label="Download CSV",
data=csv_data,
file_name="ab_test_results.csv",
mime="text/csv"
)
else:
st.info("Upload a document to enable A/B testing.")
def run_ab_comparison(query: str):
"""Run A/B comparison test for a query."""
if not st.session_state.ab_testing_manager.current_experiment:
st.session_state.ab_testing_manager.create_experiment(
name=f"Comparison - {query[:30]}",
description="Automated A/B comparison"
)
def retriever_func(q, method, k):
method_map = {
"semantic": RetrievalMethod.SEMANTIC,
"bm25": RetrievalMethod.BM25,
"hybrid": RetrievalMethod.HYBRID,
"hybrid_rerank": RetrievalMethod.HYBRID
}
use_rerank = method == "hybrid_rerank"
return st.session_state.hybrid_retriever.retrieve(
q, k=k,
method=method_map.get(method, RetrievalMethod.HYBRID),
use_reranker=use_rerank
)
variants = [TestVariant.CONTROL, TestVariant.VARIANT_A, TestVariant.VARIANT_B]
if st.session_state.hybrid_retriever.reranker:
variants.append(TestVariant.VARIANT_C)
st.session_state.ab_testing_manager.run_comparison(
query=query,
retriever_func=retriever_func,
variants=variants,
k=st.session_state.search_k
)
def process_uploaded_file(uploaded_file, force_reindex: bool = False):
"""Process an uploaded PDF file.
Args:
uploaded_file: Streamlit uploaded file object
force_reindex: If True, delete existing chunks before re-indexing
"""
if not uploaded_file.name.lower().endswith('.pdf'):
st.error("Only PDF files are currently supported.")
logger.warning(f"Invalid file type uploaded: {uploaded_file.name}")
return
# Delete existing chunks if force re-indexing
if force_reindex:
with st.spinner("Removing old document data..."):
deleted = st.session_state.vector_store_manager.delete_by_source(uploaded_file.name)
if deleted > 0:
logger.info(f"Deleted {deleted} existing chunks for {uploaded_file.name}")
with st.spinner("Processing document..."):
try:
# Initialize document processor
doc_processor = DocumentProcessor(
chunk_size=config.get_chunk_size(),
chunk_overlap=config.get_chunk_overlap(),
add_start_index=config.get_add_start_index()
)
# Process the uploaded file
st.info(f"Processing: **{uploaded_file.name}**")
chunks = doc_processor.process_uploaded_file(uploaded_file)
# Store documents for BM25
st.session_state.documents = chunks
st.session_state.current_doc_name = uploaded_file.name
# Index embeddings with retry logic
with st.spinner("Creating embeddings and indexing..."):
chroma_ids = add_documents_with_retry(
st.session_state.vector_store_manager.vector_store,
chunks
)
# Display processing results in a clean horizontal layout
st.success(f"✅ Document processed: **{len(chunks)}** chunks created, **{len(chroma_ids)}** embeddings indexed")
# Chunk details in a separate expander (not in narrow column)
chunk_info = doc_processor.get_chunk_info(chunks)
with st.expander("View chunk details", expanded=False):
for info in chunk_info[:5]: # Show first 5
st.write(f"Chunk {info['index']}: {info['size']} chars")
if len(chunk_info) > 5:
st.write(f"... and {len(chunk_info) - 5} more")
# Initialize hybrid retriever
semantic_retriever = st.session_state.vector_store_manager.get_retriever(
search_k=st.session_state.search_k * config.get_fetch_k_multiplier()
)
st.session_state.hybrid_retriever = create_hybrid_retriever(
semantic_retriever=semantic_retriever,
documents=chunks,
enable_reranker=config.is_reranking_enabled(),
reranker_provider=config.get_reranker_provider(),
alpha=config.get_hybrid_alpha(),
rrf_k=config.get_rrf_k(),
bm25_k1=config.get_bm25_k1(),
bm25_b=config.get_bm25_b()
)
# Start conversation session
st.session_state.conversation_manager.start_session(
document_name=uploaded_file.name
)
# Update session state
st.session_state.processed_file = True
st.session_state.needs_sidebar_refresh = True
logger.info(f"File processing complete: {uploaded_file.name}")
except ValueError as e:
st.error(f"Validation error: {str(e)}")
logger.error(f"Validation error: {e}")
except Exception as e:
st.error(f"Error processing file: {str(e)}")
logger.error(f"Error processing file {uploaded_file.name}: {e}", exc_info=True)
def handle_question(prompt: str):
"""Handle a user question and generate response."""
if not st.session_state.processed_file:
st.error("Please upload a document before asking questions.")
return
# Display user question
st.markdown(f"**Question:** {prompt}")
# Optimize query for follow-ups if enabled
optimized_query = prompt
if config.is_follow_up_optimization_enabled():
optimized_query = st.session_state.conversation_manager.optimize_follow_up_query(
prompt,
include_context=True
)
with st.spinner("Searching for answer..."):
try:
start_time = time.perf_counter()
# Use hybrid retriever if available, otherwise fall back to QA chain
if st.session_state.hybrid_retriever:
method_map = {
"semantic": RetrievalMethod.SEMANTIC,
"bm25": RetrievalMethod.BM25,
"hybrid": RetrievalMethod.HYBRID
}
method = method_map.get(
st.session_state.current_retrieval_method,
RetrievalMethod.HYBRID
)
# Get alpha from session state for hybrid mode
alpha = getattr(st.session_state, 'hybrid_alpha', config.get_hybrid_alpha())
results = st.session_state.hybrid_retriever.retrieve(
optimized_query,
k=st.session_state.search_k,
method=method,
alpha=alpha,
use_reranker=st.session_state.use_reranking
)
retrieval_time = (time.perf_counter() - start_time) * 1000
if not results:
logger.warning("No relevant information found for query")
st.warning("No relevant information found. Try rephrasing your question.")
return
docs_retrieved = [r.document for r in results]
scores = [r.final_score for r in results]
# Display retrieved context with detailed scores
with st.expander("Context used for answering", expanded=False):
# Summary header
preset_info = f"Profile: **{st.session_state.current_preset}**" if st.session_state.current_preset != "custom" else "Profile: **Custom**"
st.caption(f"{preset_info} | Method: **{method.value}** | Time: {retrieval_time:.0f}ms")
for i, result in enumerate(results):
# Create score breakdown
score_parts = []
if result.semantic_score is not None:
score_parts.append(f"Semantic: {result.semantic_score:.3f}")
if result.bm25_score is not None:
score_parts.append(f"BM25: {result.bm25_score:.3f}")
if result.rerank_score is not None:
score_parts.append(f"Rerank: {result.rerank_score:.3f}")
# Display chunk with score breakdown
st.markdown(f"**Chunk {i+1}** (Final: {result.final_score:.4f})")
# Show score breakdown in a compact format
if score_parts:
st.caption(" | ".join(score_parts))
# Show content
st.code(result.document.page_content, language=None)
else:
# Fall back to original QA chain
docs_retrieved = st.session_state.qa_chain.retrieve_context(prompt)
scores = [1.0 / (i + 1) for i in range(len(docs_retrieved))]
retrieval_time = 0
if not docs_retrieved:
logger.warning("No relevant information found for query")
st.warning("No relevant information found. Try rephrasing your question.")
return
with st.expander("Context used for answering", expanded=False):
for i, doc in enumerate(docs_retrieved):
st.markdown(f"**Chunk {i+1}:**\n```\n{doc.page_content}\n```")
# Format context
context = st.session_state.qa_chain.format_context(docs_retrieved)
# Generate and stream answer
st.subheader("Answer:")
answer_placeholder = st.empty()
full_answer = ""
for chunk in stream_llm_with_retry(
st.session_state.qa_chain.llm_model,
st.session_state.qa_chain.prompt_template.invoke({
"question": prompt,
"document": context
})
):
full_answer += chunk.content
answer_placeholder.write(full_answer)
# Record in conversation history
st.session_state.conversation_manager.add_query(
query=prompt,
answer=full_answer,
retrieved_docs=docs_retrieved,
scores=scores,
retrieval_method=st.session_state.current_retrieval_method
)
logger.info(f"Answer generated: {len(full_answer)} characters")
except Exception as e:
st.error(f"Error generating answer: {str(e)}")
logger.error(f"Error generating answer: {e}", exc_info=True)
def render_help_section():
"""Render help section with 2-column layout and learn more link."""
with st.expander("How this app works", expanded=False):
col1, col2 = st.columns(2)
with col1:
st.markdown("""
### RAG Pipeline Overview
1. **Upload PDF**: The app processes your PDF and splits it into smaller chunks.
2. **Document Indexing**: Each chunk is converted into a vector embedding and stored in ChromaDB.
3. **Hybrid Search**: When you ask a question, the app finds relevant chunks using:
- **Semantic search**: Finds conceptually similar content
- **BM25 (keyword)**: Finds exact term matches
- **Re-ranking**: Optionally re-scores results for better accuracy
4. **Answer Generation**: GPT-4o-mini generates an answer based only on the retrieved chunks.
---
### Retrieval Methods
- **Semantic Only**: Uses embedding similarity (best for conceptual questions)
- **BM25 Only**: Uses keyword matching (best for exact terms)
- **Hybrid**: Combines both methods (recommended for general use)
""")
with col2:
st.markdown("""
### Tips for Better Results
- Ask specific questions that might be answered in the document
- If you get "I can't answer" responses, try rephrasing
- Use the alpha slider to tune the balance between semantic and keyword search
- Check the context expander to see what information was used
- Follow-up questions automatically use conversation context
---
### Technology Stack
- **Embeddings**: OpenAI text-embedding-3-large
- **Vector Store**: ChromaDB (persistent)
- **LLM**: GPT-4o-mini
- **Re-ranking**: Cohere / Jina (optional)
- **Framework**: LangChain + Streamlit
""")
# Learn more link
st.markdown("---")
st.page_link(
"pages/1_How_It_Works.py",
label="📚 Learn more about optimizing your semantic search",
icon="🔗"
)
def main():
"""Main application entry point."""
# Initialize session state
initialize_session_state()
# Page configuration
st.set_page_config(
page_title="Semantic Search Engine",
page_icon="magnifying_glass_tilted_left:",
layout="wide",
initial_sidebar_state="expanded"
)
# Check if sidebar needs refresh (after document upload)
if st.session_state.get('needs_sidebar_refresh', False):
st.session_state.needs_sidebar_refresh = False
st.rerun()
# Apply shared page styles (hide nav + base styles)
apply_page_styles()
# Render sidebar
render_sidebar()
render_conversation_history()
render_database_management()
# Main content
st.title("Search Documents")
st.markdown("Upload a PDF document and ask questions using hybrid semantic search.")
# Help Section (moved to top for visibility)
render_help_section()
# Always visible "Learn more" link outside the expander
st.page_link(
"pages/1_How_It_Works.py",
label="📚 Learn more about optimizing your semantic search →",
icon=None
)
# Advanced Search - Collections link
st.page_link(
"pages/2_Collections.py",
label="📁 Advanced Search: Organize your documents into searchable collections →",
icon=None
)
# File upload
st.markdown("---")
uploaded_file = st.file_uploader(
"Select a PDF file",
type=['pdf'],
help="Upload a PDF document to enable semantic search"
)
if uploaded_file is not None:
# Check if file already exists in database
if st.session_state.vector_store_manager.document_exists(uploaded_file.name):
st.info(
f"ℹ️ **'{uploaded_file.name}'** is already indexed and ready to search! "
"Enter a question below to search this document."
)
# Offer option to re-index if they really want to
with st.expander("Re-index this document?"):
st.caption("Only needed if the file content has changed.")
if st.button("Re-index Document", type="secondary"):
process_uploaded_file(uploaded_file, force_reindex=True)
else:
process_uploaded_file(uploaded_file)
# Documents Panel (shows stats and document list)
render_documents_panel()
# A/B Testing Panel
render_ab_testing_panel()
# Question Answering Interface
st.markdown("---")
st.subheader("Ask Questions About Your Document")
st.caption("Search includes all uploaded documents and collections.")
# Inline question input (directly under header)
col1, col2 = st.columns([5, 1])
with col1:
question_input = st.text_input(
"Your question",
placeholder="Type your question here...",
label_visibility="collapsed",
key="question_input"
)
with col2:
ask_button = st.button("Ask", type="primary", use_container_width=True)
if ask_button and question_input:
handle_question(question_input)
elif ask_button and not question_input:
st.warning("Please enter a question.")
# Footer
st.markdown("---")
st.markdown(
"<div style='text-align: center; color: gray;'>"
"Built with LangChain, ChromaDB, and OpenAI | "
f"<a href='https://github.com/shrimpy8/semantic-serach'>GitHub</a>"
"</div>",
unsafe_allow_html=True
)
if __name__ == "__main__":
main()