first_chatbot / app.py
Geinji's picture
Update app.py
bfd0f24 verified
import streamlit as st
import requests
import os
# Retrieve API key from environment variables
api_key = st.secrets["groq_api_key"]
# Function to interact with Groq API
def get_groq_response(prompt):
url = "https://api.groq.com/openai/v1/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# Assuming the API requires a 'messages' field
data = {
"model": "mixtral-8x7b-32768", # Specify the model if required
"messages": [{"role": "user", "content": prompt}]
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
else:
return f"Error: {response.status_code}, {response.text}"
# Streamlit app code
st.title("Groq API Chatbot")
if "conversation" not in st.session_state:
st.session_state.conversation = []
user_input = st.text_input("You: ", "")
if st.button("Send"):
if user_input:
st.session_state.conversation.append(f"You: {user_input}")
# Get response from Groq API
response = get_groq_response(user_input)
st.session_state.conversation.append(f"Bot: {response}")
# Display the conversation history
for message in st.session_state.conversation:
st.write(message)