|
import streamlit as st |
|
import os |
|
from groq import Groq |
|
|
|
|
|
st.set_page_config(page_title="Groq Chatbot", page_icon="π€") |
|
|
|
|
|
st.title("π€ Chat with Groq LLM") |
|
|
|
|
|
if "GROQ_API_KEY" not in os.environ: |
|
groq_api_key = st.text_input("Enter your GROQ API Key", type="password") |
|
else: |
|
groq_api_key = os.environ.get("GROQ_API_KEY") |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
for msg in st.session_state.messages: |
|
st.chat_message(msg["role"]).markdown(msg["content"]) |
|
|
|
|
|
user_input = st.chat_input("Type your message here...") |
|
|
|
if user_input and groq_api_key: |
|
|
|
st.chat_message("user").markdown(user_input) |
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
client = Groq(api_key=groq_api_key) |
|
try: |
|
response = client.chat.completions.create( |
|
messages=st.session_state.messages, |
|
model="llama3-70b-8192" |
|
) |
|
reply = response.choices[0].message.content |
|
st.chat_message("assistant").markdown(reply) |
|
st.session_state.messages.append({"role": "assistant", "content": reply}) |
|
except Exception as e: |
|
st.error(f"Error: {e}") |
|
|