Spaces:
Sleeping
Sleeping
File size: 1,387 Bytes
22caee5 649b106 22caee5 649b106 22caee5 59a14fe 649b106 59a14fe bfd0f24 59a14fe 649b106 59a14fe 649b106 59a14fe 22caee5 82e6fd4 22caee5 82e6fd4 22caee5 82e6fd4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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)
|