-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (102 loc) · 3.76 KB
/
main.py
File metadata and controls
129 lines (102 loc) · 3.76 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
"""
Main Entry Point for Chat Application
This module serves as the entry point for the chat application framework.
It reads the configuration from .config.yml to determine which chatbot
interface to load and run.
Supported Chatbot Types:
- basic: Simple chat interface without memory
- basic_memory: Chat interface with long-term memory
- agentic: Advanced chat with memory and agent capabilities
- semantic: Chat with semantic search and retrieval
- langgraph: Chat using LangGraph framework
Configuration:
Set the chatbot type in .config.yml under chatbot.type
Usage:
streamlit run main.py
The application will automatically load the appropriate chatbot interface
based on the configuration setting.
"""
import importlib
import streamlit as st
import yaml
from box import Box
from pyprojroot import here
# Import authentication utilities
from utils.auth_wrapper import require_auth, init_auth_session_state
def load_config() -> Box:
"""
Load configuration from the .config.yml file.
Returns:
Box: Configuration object with easy dot notation access
Raises:
FileNotFoundError: If .config.yml is not found
yaml.YAMLError: If the YAML file is malformed
"""
config_path = here('.config.yml')
try:
with open(config_path, 'r') as file:
config = Box(yaml.safe_load(file))
return config
except FileNotFoundError:
st.error("❌ Configuration file '.config.yml' not found in project root")
st.stop()
except yaml.YAMLError as e:
st.error(f"❌ Error parsing configuration file: {e}")
st.stop()
def load_chatbot_module(chatbot_type: str):
"""
Dynamically load a chatbot module based on the type.
Args:
chatbot_type (str): The type of chatbot to load
Returns:
module: The loaded chatbot module
Raises:
ImportError: If the chatbot module cannot be loaded
"""
# Map chatbot types to their module paths
chatbot_modules = {
"basic": "chatbots.basic_ui_chat",
"basic_memory": "chatbots.basic_ui_chat_with_memory",
"agentic": "chatbots.agentic_ui_chat",
"semantic": "chatbots.semantic_ui_chat",
"langgraph": "chatbots.langgraph_ui_chat",
"letta": "chatbots.letta_ui_chat"
}
if chatbot_type not in chatbot_modules:
st.error(f"❌ Unsupported chatbot type: {chatbot_type}")
st.error(f"Supported types: {', '.join(chatbot_modules.keys())}")
st.stop()
try:
# Import the module dynamically
module_name = chatbot_modules[chatbot_type]
module = importlib.import_module(module_name)
return module
except ImportError as e:
st.error(f"❌ Failed to load chatbot module '{chatbot_type}': {e}")
st.stop()
@require_auth
def main():
"""
Main entry point that loads the appropriate chatbot based on configuration.
"""
try:
# Load configuration
config = load_config()
# Get the chatbot type from config
chatbot_type = config.chatbot.type
# Load and run the appropriate chatbot module
chatbot_module = load_chatbot_module(chatbot_type)
# Run the chatbot's main function
if hasattr(chatbot_module, 'main'):
chatbot_module.main()
else:
st.error(f"❌ Chatbot module '{chatbot_type}' does not have a main() function")
st.stop()
except Exception as e:
st.error(f"❌ Application error: {e}")
st.error("Please check your configuration and try again")
if __name__ == "__main__":
# Initialize authentication session state
init_auth_session_state()
# Run the main application
main()