import os import streamlit as st from groq import Groq # Set your Groq API key here or load it from an environment variable api_key = os.getenv("GROQ_API_KEY") # Preferred for security # Alternatively, hardcode the API key if it's safe to do so (less secure) # api_key = "your-groq-api-key" # Streamlit app setup st.title("Groq API Chat App") st.write("Enter your input to get a response from the Groq model.") if api_key: # Initialize the Groq client with the pre-configured API key client = Groq(api_key=api_key) # User input for chat content user_input = st.text_input("Enter your question or text:") if st.button("Get Response"): # Ensure user input is not empty if user_input: try: chat_completion = client.chat.completions.create( messages=[ { "role": "user", "content": user_input } ], model="llama3-8b-8192", ) # Display the response response = chat_completion.choices[0].message.content st.write("### Response") st.write(response) except Exception as e: st.error(f"An error occurred: {e}") else: st.warning("Please enter some text before clicking 'Get Response'") else: st.error("API key not found. Please set the API key in your environment.")