File size: 1,293 Bytes
f8ad2cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import streamlit as st
from groq import Groq

# ----------------------------
# Groq API key
# ----------------------------
# On Hugging Face, you'll set this as a secret: GROQ_API_KEY
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
client = Groq(api_key=GROQ_API_KEY)

# ----------------------------
# Streamlit UI
# ----------------------------
st.set_page_config(page_title="Groq AI Chatbot", page_icon="🤖")
st.title("🤖 Groq AI Chatbot (Streamlit)")

if "messages" not in st.session_state:
    st.session_state["messages"] = []

# Display chat history
for msg in st.session_state["messages"]:
    if msg["role"] == "user":
        st.markdown(f"**You:** {msg['content']}")
    else:
        st.markdown(f"**AI:** {msg['content']}")

# User input
prompt = st.text_input("Type your question here:")

if prompt:
    st.session_state["messages"].append({"role": "user", "content": prompt})
    try:
        completion = client.chat.completions.create(
            model="llama-3.1-8b-instant",
            messages=st.session_state["messages"]
        )
        reply = completion.choices[0].message.content
    except Exception as e:
        reply = f"(Error: {e})"

    st.session_state["messages"].append({"role": "assistant", "content": reply})
    st.experimental_rerun()