# app.py import streamlit as st from openai import OpenAI import os # Set page configuration st.set_page_config( page_title="Chat with Groq", page_icon="⚡️", layout="centered" ) # Initialize the Groq client # The API key is securely fetched from Hugging Face's secrets client = OpenAI( api_key=os.environ.get("GROQ_API_KEY"), base_url="https://api.groq.com/openai/v1", ) # App title and caption st.title("⚡️ Chat with Groq") st.caption("Powered by Streamlit, Hugging Face, and the lightning-fast Groq API") # Initialize chat history in session state if "messages" not in st.session_state: st.session_state.messages = [] # Display previous messages for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) # Handle user input if prompt := st.chat_input("What's on your mind?"): # Add user message to history and display it st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) # Generate and display assistant's response with st.chat_message("assistant"): message_placeholder = st.empty() try: # Create the API call response = client.chat.completions.create( model="llama-3.1-8b-instant", # Use a current, supported model messages=[ {"role": m["role"], "content": m["content"]} for m in st.session_state.messages ], ) full_response = response.choices[0].message.content message_placeholder.markdown(full_response) # Add assistant's response to history st.session_state.messages.append({"role": "assistant", "content": full_response}) except Exception as e: st.error(f"An error occurred: {e}")