shamiimuhammad/NexGen-Support-Hero
0
1import streamlit as st2from src.app import run_agent_stream3from src.database import init_db, save_message, load_messages4from langchain_core.messages import HumanMessage, AIMessage5import os6 7# Check for required environment variables8if not os.getenv("GROQ_API_KEY"):9 st.error("⚠️ **Missing Configuration**")10 st.error("GROQ_API_KEY environment variable is not set")11 st.info("To fix this, add GROQ_API_KEY to your Railway environment variables:")12 st.code("GROQ_API_KEY=your_groq_api_key_here")13 st.stop()14 15# Initialize the local .db file16try:17 init_db()18except Exception as e:19 st.error(f"⚠️ Database initialization failed: {str(e)}")20 st.stop()21 22st.set_page_config(page_title="NexGen Support Hero", page_icon="🤖")23 24# Logic: Local Session Tracking25if "langchain_messages" not in st.session_state:26 # On first load, pull everything from the SQLite file27 st.session_state.langchain_messages = load_messages("default_user")28 29st.title("🤖 NexGen Local Hero")30st.caption("Persistent SQLite Storage Enabled")31 32# Display history33for message in st.session_state.langchain_messages:34 role = "user" if isinstance(message, HumanMessage) else "assistant"35 with st.chat_message(role):36 st.markdown(message.content)37 38# Chat Input39if prompt := st.chat_input("I'll remember this even if you refresh!"):40 # 1. Save to DB and State41 save_message("default_user", "user", prompt)42 st.session_state.langchain_messages.append(HumanMessage(content=prompt))43 44 with st.chat_message("user"):45 st.markdown(prompt)46 47 # 2. Stream Response48 with st.chat_message("assistant"):49 placeholder = st.empty()50 full_response = ""51 # Pass history (minus the current prompt) to the brain52 for chunk in run_agent_stream(prompt, st.session_state.langchain_messages[:-1]):53 full_response += chunk54 placeholder.markdown(full_response + "▌")55 placeholder.markdown(full_response)56 57 # 3. Save AI response to DB and State58 save_message("default_user", "assistant", full_response)59 st.session_state.langchain_messages.append(AIMessage(content=full_response))