File size: 2,377 Bytes
99ca370
 
aeb51ae
99ca370
aeb51ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# app.py
import os
import streamlit as st
from groq import Groq

# Initialize Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY"))

# ---------------- UI Setup ----------------
st.set_page_config(page_title="Advanced Groq Chatbot", layout="centered")
st.title("πŸ€– Groq Chatbot")
st.markdown("Chat using lightning-fast open-source LLMs on Groq!")

# Model selector (friendly names)
model_options = {
    "πŸ’¬ Fast Chat (LLaMA 3 8B)": "llama3-8b-8192",
    "🧠 Deep Reasoning (LLaMA 3 70B)": "llama3-70b-8192",
    "πŸ“Š Math Solver (Mixtral)": "mixtral-8x7b-32768"
}
model_choice = st.selectbox("Choose assistant type:", list(model_options.keys()))
model_id = model_options[model_choice]

# Personality selector
style_options = {
    "Friendly": "You are a friendly assistant who replies casually.",
    "Professional": "You are a formal and helpful assistant.",
    "Technical": "You are a technical assistant providing accurate information."
}
personality = st.selectbox("Choose assistant personality:", list(style_options.keys()))
system_prompt = style_options[personality]

# Initialize session history
if "messages" not in st.session_state:
    st.session_state.messages = [{"role": "system", "content": system_prompt}]

# Clear chat button
if st.button("πŸ”„ Clear Chat"):
    st.session_state.messages = [{"role": "system", "content": system_prompt}]
    st.rerun()

# Show chat history
for msg in st.session_state.messages[1:]:
    with st.chat_message(msg["role"]):
        st.markdown(msg["content"])

# Chat input
if prompt := st.chat_input("Type your message..."):
    st.chat_message("user").markdown(prompt)
    st.session_state.messages.append({"role": "user", "content": prompt})

    # Call Groq with streaming
    try:
        response = client.chat.completions.create(
            model=model_id,
            messages=st.session_state.messages,
            temperature=0.7,
            stream=True
        )

        reply_text = ""
        with st.chat_message("assistant"):
            reply_box = st.empty()
            for chunk in response:
                content = chunk.choices[0].delta.content or ""
                reply_text += content
                reply_box.markdown(reply_text)

        st.session_state.messages.append({"role": "assistant", "content": reply_text})

    except Exception as e:
        st.error(f"❌ Error: {e}")