# app.py import streamlit as st import json from groq_chat import chat_with_groq, stream_chat_with_groq st.set_page_config(page_title="Groq Chatbot", page_icon="๐Ÿค–", layout="centered") st.title("๐Ÿค– Groq Chatbot (Kimi)") st.markdown("Chat with Moonshot AI's Kimi model powered by Groq API.") # --- Session State Initialization --- if "chat_history" not in st.session_state: st.session_state.chat_history = [] if "system_prompt" not in st.session_state: st.session_state.system_prompt = "You are a helpful AI assistant." if "temperature" not in st.session_state: st.session_state.temperature = 0.7 if "show_sidebar" not in st.session_state: st.session_state.show_sidebar = True if "theme_pref" not in st.session_state: st.session_state.theme_pref = "System" if "use_streaming" not in st.session_state: st.session_state.use_streaming = True # --- Sidebar Toggle Button --- toggle_label = "๐Ÿ‘ˆ Hide Sidebar" if st.session_state.show_sidebar else "๐Ÿ‘‰ Show Sidebar" if st.button(toggle_label): st.session_state.show_sidebar = not st.session_state.show_sidebar # --- Sidebar --- if st.session_state.show_sidebar: with st.sidebar: st.markdown("## ๐Ÿ”ง Settings") st.session_state.temperature = st.slider( "Temperature", 0.0, 1.0, st.session_state.temperature, 0.05 ) st.session_state.system_prompt = st.text_area( "System Prompt", value=st.session_state.system_prompt ) st.session_state.use_streaming = st.toggle( "โšก Use Streaming Responses", value=st.session_state.use_streaming ) st.session_state.theme_pref = st.radio( "๐ŸŒ— Theme Preference", ["Light", "Dark", "System"], index=["Light", "Dark", "System"].index(st.session_state.theme_pref) ) st.markdown("> ๐Ÿ’ก Change actual theme via โš™๏ธ in top-right.") st.markdown("---") # Chat download if st.session_state.chat_history: chat_json = json.dumps(st.session_state.chat_history, indent=2) st.download_button("๐Ÿ’พ Download Chat", chat_json, file_name="chat_history.json", mime="application/json") # Chat upload uploaded = st.file_uploader("๐Ÿ“‚ Load Previous Chat", type=["json"]) if uploaded: st.session_state.chat_history = json.load(uploaded) st.success("Chat loaded successfully.") st.rerun() # --- Clear Chat Button --- if st.button("๐Ÿงน Clear Chat"): st.session_state.chat_history = [] st.rerun() # --- Display Previous Chat --- for msg in st.session_state.chat_history: avatar = "๐Ÿ‘ค" if msg["role"] == "user" else "๐Ÿค–" with st.chat_message(msg["role"], avatar=avatar): st.markdown(msg["content"]) # --- Chat Input --- prompt = st.chat_input("Type your message here...") if prompt: st.session_state.chat_history.append({"role": "user", "content": prompt}) with st.chat_message("user", avatar="๐Ÿ‘ค"): st.markdown(prompt) with st.chat_message("assistant", avatar="๐Ÿค–"): with st.spinner("Groq is thinking..."): full_response = "" try: if st.session_state.use_streaming: stream = stream_chat_with_groq( prompt, history=st.session_state.chat_history, system_prompt=st.session_state.system_prompt, temperature=st.session_state.temperature ) response_container = st.empty() for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: token = chunk.choices[0].delta.content full_response += token response_container.markdown(full_response) st.sidebar.markdown("๐Ÿ“Š **Tokens Used:** Not available in streaming mode.") else: response = chat_with_groq( prompt, history=st.session_state.chat_history, system_prompt=st.session_state.system_prompt, temperature=st.session_state.temperature ) full_response = response.choices[0].message.content st.markdown(full_response) if "usage" in response: st.sidebar.markdown(f"๐Ÿ“Š **Tokens Used:** {response['usage']['total_tokens']}") st.session_state.chat_history.append({"role": "assistant", "content": full_response}) except Exception as e: st.error(f"๐Ÿ’ฅ API Error: {e}")