Spaces:
Sleeping
Sleeping
| import os | |
| import streamlit as st | |
| from groq import Groq | |
| from dotenv import load_dotenv | |
| # Load environment variables from .env file | |
| load_dotenv() | |
| # Ensure your environment variable for the API key is set | |
| api_key = os.getenv("GROQ_API_KEY") | |
| if not api_key: | |
| st.error("API key is not set. Please set your GROQ_API_KEY environment variable.") | |
| st.stop() | |
| # Initialize Groq client with API key | |
| client = Groq(api_key=api_key) | |
| # Streamlit interface | |
| st.title("General Knowledge Chatbot") | |
| # Chat history container | |
| chat_history = st.container() | |
| user_input = st.text_input("Ask me anything:") | |
| if user_input: | |
| # Add user message to chat history | |
| with chat_history: | |
| st.write(f"**You:** {user_input}") | |
| # Chat completion request to Groq API | |
| chat_completion = client.chat.completions.create( | |
| messages=[ | |
| {"role": "user", "content": user_input}, | |
| ], | |
| model="llama-3.3-70b-versatile", # Use the model that suits your needs | |
| ) | |
| # Get the response from the API | |
| response = chat_completion.choices[0].message.content | |
| # Display response | |
| with chat_history: | |
| st.write(f"**Chatbot:** {response}") | |