File size: 4,676 Bytes
383ee63 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 6557eac 383ee63 6557eac c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 d143793 c00effc 383ee63 c00effc 383ee63 c00effc 383ee63 8586611 c00effc 383ee63 |
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 |
"""Streamlit UI for Agentic RAG System - Simplified Version"""
import streamlit as st
from pathlib import Path
import sys
import time
# Add src to path
sys.path.append(str(Path(__file__).parent))
from src.config.config import Config
from src.document_ingestion.document_processor import DocumentProcessor
from src.vectorstore.vectorstore import VectorStore
from src.graph_builder.graph_builder import GraphBuilder
# Page configuration
st.set_page_config(
page_title="π€ RAG Search",
page_icon="π",
layout="centered"
)
# Simple CSS
st.markdown("""
<style>
.stButton > button {
width: 100%;
background-color: #4CAF50;
color: white;
font-weight: bold;
}
</style>
""", unsafe_allow_html=True)
def init_session_state():
"""Initialize session state variables"""
if 'rag_system' not in st.session_state:
st.session_state.rag_system = None
if 'initialized' not in st.session_state:
st.session_state.initialized = False
if 'history' not in st.session_state:
st.session_state.history = []
@st.cache_resource
def initialize_rag():
"""Initialize the RAG system (cached)"""
try:
# Initialize components
llm = Config.get_llm()
doc_processor = DocumentProcessor(
chunk_size=Config.CHUNK_SIZE,
chunk_overlap=Config.CHUNK_OVERLAP
)
vector_store = VectorStore()
# Use default URLs
urls = Config.DEFAULT_URLS
# Process documents
documents = doc_processor.process_urls(urls)
# Create vector store
vector_store.create_vectorstore(documents)
# Build graph
graph_builder = GraphBuilder(
retriever=vector_store.get_retriever(),
llm=llm
)
graph_builder.build()
return graph_builder, len(documents)
except Exception as e:
st.error(f"Failed to initialize: {str(e)}")
return None, 0
def main():
"""Main application"""
init_session_state()
# Title
st.title("π RAG Document Search")
st.markdown("Ask questions about the loaded documents")
# Initialize system
if not st.session_state.initialized:
with st.spinner("Loading system..."):
rag_system, num_chunks = initialize_rag()
if rag_system:
st.session_state.rag_system = rag_system
st.session_state.initialized = True
st.success(f"β
System ready! ({num_chunks} document chunks loaded)")
st.markdown("---")
# Search interface
with st.form("search_form"):
question = st.text_input(
"Enter your question:",
placeholder="What would you like to know?"
)
submit = st.form_submit_button("π Search")
# Process search
if submit and question:
if st.session_state.rag_system:
with st.spinner("Searching..."):
start_time = time.time()
# Get answer
result = st.session_state.rag_system.run(question)
elapsed_time = time.time() - start_time
# Add to history
st.session_state.history.append({
'question': question,
'answer': result['answer'],
'time': elapsed_time
})
# Display answer
st.markdown("### π‘ Answer")
st.success(result['answer'])
# Show retrieved docs in expander
with st.expander("π Source Documents"):
for i, doc in enumerate(result['retrieved_docs'], 1):
st.text_area(
f"Document {i}",
doc.page_content[:300] + "...",
height=100,
disabled=True
)
st.caption(f"β±οΈ Response time: {elapsed_time:.2f} seconds")
# Show history
if st.session_state.history:
st.markdown("---")
st.markdown("### π Recent Searches")
for item in reversed(st.session_state.history[-3:]): # Show last 3
with st.container():
st.markdown(f"**Q:** {item['question']}")
st.markdown(f"**A:** {item['answer'][:200]}...")
st.caption(f"Time: {item['time']:.2f}s")
st.markdown("")
if __name__ == "__main__":
main() |