import streamlit as st import time from openai import OpenAI import os from dotenv import load_dotenv def run_interface(config=None): """ Run the FAQ interface with dynamic configuration Args: config (dict): Configuration dictionary containing all UI settings and parameters """ # Load environment variables load_dotenv() # Setup OpenAI client openai_api_key = os.environ.get('OPENAI_API_KEY') if not openai_api_key: st.error('OpenAI API key not found. Please add it to your environment variables.') st.stop() client = OpenAI(api_key=openai_api_key) def ensure_single_thread_id(): if "thread_id" not in st.session_state: thread = client.beta.threads.create() st.session_state.thread_id = thread.id return st.session_state.thread_id def get_avatar(role): return config.get('AVATAR_CONFIG', {}).get(role) def get_assistant_response(prompt, thread_id): message_placeholder = st.empty() i = 0 client.beta.threads.messages.create( thread_id=thread_id, role="user", content=prompt ) stream = client.beta.threads.runs.create( thread_id=thread_id, assistant_id=config['ASSISTANT_ID'], stream=True ) full_response = "" for event in stream: if not full_response: message_placeholder.markdown(f"*Thinking{config['THINKING_DOTS'][i % len(config['THINKING_DOTS'])]}*", unsafe_allow_html=True) i += 1 time.sleep(config['THINKING_INTERVAL']) if event.data.object == "thread.message.delta": for content in event.data.delta.content: if content.type == 'text': chunk = content.text.value full_response += chunk lines = full_response.split('\n') cleaned_lines = [] for line in lines: cleaned_line = ' '.join([part.strip() for part in line.split('【') if '】' not in part]) cleaned_line = cleaned_line.strip() if cleaned_line: if cleaned_line[0].isdigit() and '. ' in cleaned_line: cleaned_lines.append(cleaned_line + '\n') else: cleaned_lines.append(cleaned_line) formatted_response = '\n'.join(cleaned_lines) message_placeholder.markdown(formatted_response) time.sleep(0.01) return full_response # Page configuration st.set_page_config(page_icon=config.get('PAGE_ICON', '🤖')) # Custom CSS from original app.py st.markdown(""" """, unsafe_allow_html=True) # Load JavaScript for handling scrolling st.markdown(""" """, unsafe_allow_html=True) # Logo and header st.markdown(f"""
{config['AGENT_NAME']}
{config.get('AGENT_SUBTITLE', 'Your AI Assistant')}
""", unsafe_allow_html=True) # Initialize session state if "messages" not in st.session_state: st.session_state.messages = [] if "conversation_starter_selected" not in st.session_state: st.session_state.conversation_starter_selected = False if "conversation_starter" not in st.session_state: st.session_state.conversation_starter = "" # Add initial welcome message if 'welcome_message_displayed' not in st.session_state: st.session_state.messages.append({"role": "assistant", "content": config.get('WELCOME_MESSAGE', 'Hi! How can I help you today?')}) st.session_state.welcome_message_displayed = True # Chat container st.markdown('
', unsafe_allow_html=True) # Display chat messages for idx, message in enumerate(st.session_state.messages): with st.chat_message(message["role"], avatar=get_avatar(message["role"])): if idx == 0 and message["role"] == "assistant": st.markdown(f'
{message["content"]}
', unsafe_allow_html=True) # Conversation starter buttons if 'CONVERSATION_STARTERS' in config: col1, col2, col3 = st.columns([1,1,1]) with col2: for starter in config['CONVERSATION_STARTERS']: if st.button(starter["text"]): st.session_state.conversation_starter = starter["text"] st.session_state.conversation_starter_selected = True else: st.markdown(message["content"], unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) # Chat input thread_id = ensure_single_thread_id() prompt = st.chat_input(config.get('CHAT_INPUT_PLACEHOLDER', 'Type your message here...')) if st.session_state.conversation_starter_selected and not prompt: prompt = st.session_state.conversation_starter st.session_state.conversation_starter_selected = False if prompt: with st.chat_message("user", avatar=get_avatar("user")): st.markdown(prompt, unsafe_allow_html=True) st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("assistant", avatar=get_avatar("assistant")): assistant_response = get_assistant_response(prompt, thread_id) st.session_state.messages.append({"role": "assistant", "content": assistant_response})