File size: 1,415 Bytes
ac9e194 |
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 os
from groq import Groq
# Load API key from environment variable
api_key = os.getenv("GROQ_API_KEY")
# Initialize Groq client with the secret API key
client = Groq(api_key=api_key)
# Streamlit page config
st.set_page_config(page_title="Groq Chatbot", page_icon="🤖")
st.title("🤖 Groq Chatbot (LLaMA3-70B)")
# Initialize chat history in session state
if "messages" not in st.session_state:
st.session_state.messages = []
# Display previous chat messages
for msg in st.session_state.messages:
st.chat_message(msg["role"]).markdown(msg["content"])
# User input
prompt = st.chat_input("Type your message...")
if prompt:
# Append user message
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").markdown(prompt)
# Get response from Groq API
with st.spinner("Thinking..."):
try:
response = client.chat.completions.create(
model="llama3-70b-8192",
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
)
reply = response.choices[0].message.content
except Exception as e:
reply = f"⚠️ Error: {str(e)}"
# Append and display assistant reply
st.session_state.messages.append({"role": "assistant", "content": reply})
st.chat_message("assistant").markdown(reply)
|