import os import streamlit as st from groq import Groq # Page configuration st.set_page_config( page_title="Groq Chatbot", page_icon="⚡", layout="centered", initial_sidebar_state="expanded" ) # Custom CSS for modern dark theme st.markdown(""" """, unsafe_allow_html=True) # Initialize Groq client @st.cache_resource def get_groq_client(): api_key = os.environ.get("GROQ_API_KEY") if not api_key: st.error("⚠️ GROQ_API_KEY not found in environment variables!") st.stop() return Groq(api_key=api_key) # Initialize session state for chat history if "messages" not in st.session_state: st.session_state.messages = [ {"role": "assistant", "content": "👋 Hello! I'm your AI assistant powered by Groq. How can I help you today?"} ] if "model" not in st.session_state: st.session_state.model = "llama-3.3-70b-versatile" # Sidebar configuration with st.sidebar: st.markdown("### ⚙️ Settings") # Model selection model_options = { "Llama 3.3 70B": "llama-3.3-70b-versatile", "Llama 3.1 70B": "llama-3.1-70b-versatile", "Llama 3.1 8B": "llama-3.1-8b-instant", "Mixtral 8x7B": "mixtral-8x7b-32768", "Gemma 2 9B": "gemma2-9b-it" } selected_model = st.selectbox( "Choose Model", options=list(model_options.keys()), index=0 ) st.session_state.model = model_options[selected_model] st.markdown("---") # Temperature slider temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1) # Max tokens slider max_tokens = st.slider("Max Tokens", 256, 4096, 1024, 128) st.markdown("---") # Clear chat button if st.button("🗑️ Clear Chat", use_container_width=True): st.session_state.messages = [ {"role": "assistant", "content": "👋 Chat cleared! How can I help you?"} ] st.rerun() st.markdown("---") st.markdown("### 📊 Stats") st.write(f"Messages: {len(st.session_state.messages)}") # API Key status if os.environ.get("GROQ_API_KEY"): st.success("🔑 API Key: Connected") else: st.error("🔑 API Key: Missing") # Main chat interface st.title("⚡ Groq Chatbot") st.markdown("*Lightning-fast AI conversations*") # Display chat messages for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) # Chat input if prompt := st.chat_input("Type your message here..."): # Add user message to history st.session_state.messages.append({"role": "user", "content": prompt}) # Display user message with st.chat_message("user"): st.markdown(prompt) # Get AI response with st.chat_message("assistant"): message_placeholder = st.empty() try: client = get_groq_client() # Show typing indicator message_placeholder.markdown('🤔 Thinking...', unsafe_allow_html=True) # Call Groq API chat_completion = client.chat.completions.create( messages=[ {"role": m["role"], "content": m["content"]} for m in st.session_state.messages ], model=st.session_state.model, temperature=temperature, max_tokens=max_tokens, stream=False ) response = chat_completion.choices[0].message.content # Display response with typewriter effect simulation message_placeholder.markdown(response) # Add to history st.session_state.messages.append({"role": "assistant", "content": response}) except Exception as e: message_placeholder.error(f"❌ Error: {str(e)}") # Footer st.markdown("---") st.markdown( "
" "Powered by Groq ⚡ | Built with Streamlit ❤️" "
", unsafe_allow_html=True )