import sys import os import time import streamlit as st import plotly.express as px from datetime import datetime from dotenv import load_dotenv # Import the LLM Integration Model from src.cag.generation_model import LLMIntegration # Load environment variables and secrets load_dotenv() class CAGLLM: def __init__(self,query,response): self.query = query self.response = response def process_cag_llm(self): # Initialize LLM Integration with API Key llm_system = LLMIntegration() # Cache statistics and tracking initialization if "cache_hits" not in st.session_state: st.session_state.cache_hits = 0 st.session_state.cache_misses = 0 st.session_state.response_times = [] st.session_state.query_timestamps = [] st.session_state.history = [] # st.set_page_config( # page_title="CAG Chatbot", # layout="wide", # page_icon="๐Ÿง€", # initial_sidebar_state="expanded" # ) # CSS for Styling Graph st.markdown( """ """, unsafe_allow_html=True ) # Page Title and Description st.title("๐Ÿ’ก Cache Augmented Generation (CAG) Chatbot") st.write("**A chatbot with enhanced responses powered by smart caching.**") # Layout Columns: Configurator | Chat | Statistics col1, col2, col3 = st.columns([1.2, 2, 1.2]) # ๐Ÿ› ๏ธ **Configurator Section (Left Panel)** with col1: st.header("โš™๏ธ Configurator") cache_size = st.slider("๐Ÿ—„๏ธ Cache Size", min_value=50, max_value=500, value=100) similarity_threshold = st.slider("๐Ÿ“ˆ Similarity Threshold", min_value=0.5, max_value=1.0, value=0.8) clear_cache = st.button("๐Ÿงน Clear Cache") if clear_cache: llm_system.cache_manager.clear_cache() st.session_state.cache_hits = 0 st.session_state.cache_misses = 0 st.session_state.response_times = [] st.session_state.query_timestamps = [] st.session_state.history = [] st.success("โœ… Cache cleared successfully!") # ๐Ÿ“ฆ **Cache Content Section** with st.expander("๐Ÿ“ฆ **View Cache Content**"): if llm_system.cache_manager.cache: for key, value in llm_system.cache_manager.cache.items(): st.write(f"**Query:** {key}") st.write(f"**Response:** {value['response']}") st.write(f"**Timestamp:** {datetime.fromtimestamp(value['timestamp']).strftime('%Y-%m-%d %H:%M:%S')}") st.write("---") else: st.write("๐Ÿ—‘๏ธ Cache is currently empty.") # ๐Ÿ’ฌ **Chat Interaction Section (Middle Panel)** with col2: st.header("๐Ÿ’ฌ Chat with CAG") query = self.query if self.query: start_time = time.time() # Step 1: Check Cache st.info("โณ Checking Cache...") cached_response = llm_system.cache_manager.get_from_cache(llm_system.cache_manager.normalize_key(query)) if cached_response: # Step 2: If Cache Hit, Return st.success("โœ… Cache Hit! Returning cached response.") response = cached_response st.session_state.cache_hits += 1 else: # Step 3: If Cache Miss, Query LLM st.warning("โŒ Cache Miss. Fetching from LLM...") response = llm_system.generate_response(query,self.response) st.session_state.cache_misses += 1 # Response Time and Save Data response_time = time.time() - start_time st.session_state.response_times.append(response_time) st.session_state.query_timestamps.append(datetime.now().strftime('%H:%M:%S')) st.session_state.history.append({"query": query, "response": response, "time": response_time}) # ๐ŸŽฏ Chat Response st.success(f"**๐Ÿ—จ๏ธ {response}**") st.info(f"โฑ๏ธ **Response Time:** {response_time:.2f} seconds") # ๐Ÿ“œ **Query History Section** with st.expander("๐Ÿ•ฐ๏ธ **Query History**"): for entry in st.session_state.history[-10:]: st.write(f"**Query:** {entry['query']}") st.write(f"**Response:** {entry['response']}") st.write(f"โฑ๏ธ **Time Taken:** {entry['time']:.2f} seconds") st.write("---") # ๐Ÿ“Š **Cache Statistics Section (Right Panel)** with col3: st.header("๐Ÿ“Š Cache Statistics") # Real-Time Metrics col1_stat, col2_stat, col3_stat = st.columns(3) col1_stat.metric("โœ… Hits", st.session_state.cache_hits) col2_stat.metric("โŒ Misses", st.session_state.cache_misses) col3_stat.metric("๐Ÿ“ฆ Cache Size", len(llm_system.cache_manager.cache)) # Cache Hit/Miss Ratio total_queries = st.session_state.cache_hits + st.session_state.cache_misses hit_ratio = (st.session_state.cache_hits / total_queries) * 100 if total_queries > 0 else 0 miss_ratio = (st.session_state.cache_misses / total_queries) * 100 if total_queries > 0 else 0 st.progress(hit_ratio / 100, text=f"โœ… Cache Hit Ratio: {hit_ratio:.2f}%") st.progress(miss_ratio / 100, text=f"โŒ Cache Miss Ratio: {miss_ratio:.2f}%") # ๐Ÿ“ˆ **Response Time Graph** if st.session_state.response_times: st.markdown('
', unsafe_allow_html=True) fig = px.line( x=st.session_state.query_timestamps, y=st.session_state.response_times, title="๐Ÿ“ˆ Response Time Trend", labels={"x": "Timestamp", "y": "Response Time (s)"} ) st.plotly_chart(fig, use_container_width=True) st.markdown('
', unsafe_allow_html=True)