import os from dotenv import load_dotenv from groq import Groq import streamlit as st # Load environment variables load_dotenv() # Set Groq API Key api_key = os.getenv("GROQ_API_KEY") if not api_key: raise ValueError("GROQ_API_KEY is not set in the environment.") client = Groq(api_key=api_key) # Function to generate response using Groq def chat_with_llm(user_message, model="llama3-8b-8192"): try: # Send message to Groq LLM chat_completion = client.chat.completions.create( messages=[ {"role": "user", "content": user_message}, ], model=model, ) # Return the LLM's response return chat_completion.choices[0].message.content except Exception as e: return f"Error: {e}" # Streamlit App def main(): st.title("Real-Time Text-to-Text Chatbot") user_input = st.text_input("You:", "") if st.button("Send"): if user_input.strip(): with st.spinner("Thinking..."): bot_response = chat_with_llm(user_input) st.text_area("Bot:", bot_response, height=150) if __name__ == "__main__": main()