Spaces:
Build error
Build error
File size: 5,837 Bytes
cd63bb3 a232867 33890b1 bdabf68 33890b1 cd63bb3 3b1d5fe 17f4ae2 3b1d5fe bdabf68 a232867 f46236f 7b3ba19 f46236f 33890b1 a232867 cd63bb3 bdabf68 cd63bb3 17f4ae2 cd63bb3 17f4ae2 cd63bb3 a232867 33890b1 a232867 17f4ae2 a232867 bdabf68 a232867 33890b1 a232867 90dcfc0 33890b1 17f4ae2 bdabf68 33890b1 17f4ae2 33890b1 bdabf68 33890b1 17f4ae2 a232867 33890b1 bdabf68 a232867 bdabf68 17f4ae2 | 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 | import streamlit as st
import uuid
import logging
import os
from langchain_core.messages import HumanMessage
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
from src.langgraphagenticai.ui.streamlitui.loadui import LoadStreamlitUI
from src.langgraphagenticai.LLMS.groqllm import GroqLLM
from src.langgraphagenticai.LLMS.geminillm import GoogleLLM
from src.langgraphagenticai.LLMS.chatgptllm import OpenaiLLM
from src.langgraphagenticai.graph.graph_builder import GraphBuilder
from src.langgraphagenticai.ui.streamlitui.display_result import DisplayResultStreamlit
logging.basicConfig(
level=logging.INFO, # Set the minimum log level to INFO
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s\n' # Format for log messages
)
logger = logging.getLogger(__name__)
store = {}
def get_session_history(session_id: str) -> BaseChatMessageHistory:
if session_id not in store:
store[session_id] = ChatMessageHistory()
return store[session_id]
def load_langgraph_agenticai_app():
"""
Loads and runs the LangGraph AgenticAI application with Streamlit UI.
Initializes UI, configures LLM, sets up graph, and manages session state.
"""
ui = LoadStreamlitUI()
user_controls = ui.load_streamlit_ui()
if not user_controls:
st.error("Error: Failed to load user controls from the UI.")
return
selected_llm = user_controls.get("selected_llm")
if not selected_llm:
st.info("Please select an LLM in the sidebar to proceed.")
return
tavily_api_key = user_controls.get("TAVILY_API_KEY", st.session_state.get("TAVILY_API_KEY", os.getenv("TAVILY_API_KEY", "")))
if not tavily_api_key and user_controls.get("selected_usecase") in ["Blog Generation", "Chatbot with Tool"]:
st.warning("Tavily API key not found. Web search will be skipped.")
else:
st.session_state["TAVILY_API_KEY"] = tavily_api_key
os.environ["TAVILY_API_KEY"] = tavily_api_key
if selected_llm == "Groq" and not user_controls.get("GROQ_API_KEY"):
st.warning("Please enter your Groq API key in the sidebar.")
return
elif selected_llm == "Google" and not user_controls.get("GOOGLE_API_KEY"):
st.warning("Please enter your Google API key in the sidebar.")
return
elif selected_llm == "OpenAI" and not user_controls.get("OPENAI_API_KEY"):
st.warning("Please enter your OpenAI API key in the sidebar.")
return
# Session state initialization
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())
if "thread_id" not in st.session_state:
st.session_state.thread_id = str(uuid.uuid4())
if "graph_state" not in st.session_state:
st.session_state.graph_state = None
if "waiting_for_feedback" not in st.session_state:
st.session_state.waiting_for_feedback = False
if "blog_requirements_collected" not in st.session_state:
st.session_state.blog_requirements_collected = False
if "current_usecase" not in st.session_state:
st.session_state.current_usecase = None
config = {"configurable": {"session_id": st.session_state.session_id, "thread_id": st.session_state.thread_id, "recursion_limit": 10}}
logger.info(f"Session ID: {st.session_state.session_id}, Thread ID: {st.session_state.thread_id}")
# Load LLM
try:
if selected_llm == "Groq":
llm_config = GroqLLM(user_controls_input=user_controls)
elif selected_llm == "Google":
llm_config = GoogleLLM(user_controls_input=user_controls)
elif selected_llm == "OpenAI":
llm_config = OpenaiLLM(user_controls_input=user_controls)
else:
st.error(f"Error: Unsupported LLM selected: '{selected_llm}'")
return
model = llm_config.get_llm_model()
if not model:
st.error("Error: LLM model could not be initialized.")
return
# Graph setup
usecase = user_controls.get("selected_usecase")
if not usecase:
st.error("Error: No use case selected.")
return
if st.session_state.current_usecase != usecase:
logger.info(f"Use case changed to: {usecase}. Resetting session state.")
st.session_state.waiting_for_feedback = False
st.session_state.blog_requirements_collected = False
st.session_state.current_usecase = usecase
get_session_history(st.session_state.session_id).clear()
if "graph" in st.session_state:
del st.session_state.graph
if "with_message_history" in st.session_state:
del st.session_state.with_message_history
if "graph" not in st.session_state:
graph_builder = GraphBuilder(model)
graph = graph_builder.setup_graph(usecase)
with_message_history = RunnableWithMessageHistory(
graph,
get_session_history,
input_messages_key="messages",
history_messages_key="messages"
)
st.session_state.graph = graph
st.session_state.with_message_history = with_message_history
# Display chat history and process input
display = DisplayResultStreamlit(st.session_state.graph, st.session_state.with_message_history, config, usecase)
display.display_chat_history()
display.process_user_input()
except Exception as e:
logger.error(f"Error initializing application: {e}")
st.error(f"Failed to initialize application: {e}")
if __name__ == "__main__":
load_langgraph_agenticai_app() |