File size: 4,986 Bytes
d8d6ce7
 
 
 
5bd9d05
d8d6ce7
 
 
 
 
2331ae8
d8d6ce7
 
 
 
 
 
 
 
9292d37
 
5bd9d05
 
 
2331ae8
d8d6ce7
 
 
 
5bd9d05
d8d6ce7
 
 
 
 
 
 
5bd9d05
d8d6ce7
 
 
 
5bd9d05
 
 
f7f9be5
5bd9d05
 
 
 
 
9292d37
d8d6ce7
 
2331ae8
d8d6ce7
 
2331ae8
d8d6ce7
2331ae8
d8d6ce7
 
 
2331ae8
d8d6ce7
 
2331ae8
d8d6ce7
 
 
 
2331ae8
d8d6ce7
 
 
 
 
5bd9d05
d8d6ce7
 
 
 
 
 
 
 
9292d37
 
 
5bd9d05
9292d37
5bd9d05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cadb135
 
 
5bd9d05
 
 
 
 
 
 
2331ae8
5bd9d05
 
2331ae8
 
9292d37
cadb135
 
 
9292d37
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# app.py

import streamlit as st
import json
from groq_chat import chat_with_groq, stream_chat_with_groq

st.set_page_config(page_title="Groq Chatbot", page_icon="πŸ€–", layout="centered")
st.title("πŸ€– Groq Chatbot (Kimi)")
st.markdown("Chat with Moonshot AI's Kimi model powered by Groq API.")

# --- Session State ---
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []
if "system_prompt" not in st.session_state:
    st.session_state.system_prompt = "You are a helpful AI assistant."
if "temperature" not in st.session_state:
    st.session_state.temperature = 0.7
if "show_sidebar" not in st.session_state:
    st.session_state.show_sidebar = True
if "theme_pref" not in st.session_state:
    st.session_state.theme_pref = "System"
if "use_streaming" not in st.session_state:
    st.session_state.use_streaming = True

# --- Sidebar Toggle ---
toggle_label = "πŸ‘ˆ Hide Sidebar" if st.session_state.show_sidebar else "πŸ‘‰ Show Sidebar"
if st.button(toggle_label):
    st.session_state.show_sidebar = not st.session_state.show_sidebar

# --- Sidebar ---
if st.session_state.show_sidebar:
    with st.sidebar:
        st.markdown("## πŸ”§ Settings")

        st.session_state.temperature = st.slider(
            "Temperature", 0.0, 1.0, st.session_state.temperature, 0.05
        )

        st.session_state.system_prompt = st.text_area(
            "System Prompt", value=st.session_state.system_prompt
        )

        st.session_state.use_streaming = st.toggle(
            "⚑ Use Streaming Responses", value=st.session_state.use_streaming
        )

        st.session_state.theme_pref = st.radio(
            "πŸŒ— Theme Preference", ["Light", "Dark", "System"],
            index=["Light", "Dark", "System"].index(st.session_state.theme_pref)
        )
        st.markdown("> πŸ’‘ Change actual theme via βš™οΈ in top-right.")

        st.markdown("---")

        # Download chat
        if st.session_state.chat_history:
            chat_json = json.dumps(st.session_state.chat_history, indent=2)
            st.download_button("πŸ’Ύ Download Chat", chat_json, "chat_history.json", "application/json")

        # Upload chat
        uploaded = st.file_uploader("πŸ“‚ Load Previous Chat", type=["json"])
        if uploaded:
            st.session_state.chat_history = json.load(uploaded)
            st.success("Chat loaded.")
            st.rerun()

# --- Clear chat ---
if st.button("🧹 Clear Chat"):
    st.session_state.chat_history = []
    st.rerun()

# --- Show chat history ---
for msg in st.session_state.chat_history:
    avatar = "πŸ‘€" if msg["role"] == "user" else "πŸ€–"
    with st.chat_message(msg["role"], avatar=avatar):
        st.markdown(msg["content"])

# --- Chat Input ---
prompt = st.chat_input("Type your message here...")

if prompt:
    st.session_state.chat_history.append({"role": "user", "content": prompt})

    with st.chat_message("user", avatar="πŸ‘€"):
        st.markdown(prompt)

    with st.chat_message("assistant", avatar="πŸ€–"):
        with st.spinner("Groq is thinking..."):
            full_response = ""

            try:
                if st.session_state.use_streaming:
                    stream = stream_chat_with_groq(
                        prompt,
                        history=st.session_state.chat_history,
                        system_prompt=st.session_state.system_prompt,
                        temperature=st.session_state.temperature
                    )

                    response_container = st.empty()

                    for chunk in stream:
                        if chunk.choices and chunk.choices[0].delta.content:
                            token = chunk.choices[0].delta.content
                            full_response += token
                            response_container.markdown(full_response)

                    st.sidebar.markdown("πŸ“Š **Tokens Used:** Not available in streaming mode.")

                    with st.expander("πŸ” Raw API Response"):
                        st.info("Raw streaming responses are not shown due to incremental chunks.")

                else:
                    response = chat_with_groq(
                        prompt,
                        history=st.session_state.chat_history,
                        system_prompt=st.session_state.system_prompt,
                        temperature=st.session_state.temperature
                    )
                    full_response = response.choices[0].message.content
                    st.markdown(full_response)

                    if response.usage:
                        st.sidebar.markdown(f"πŸ“Š **Tokens Used:** {response.usage.total_tokens}")

                    with st.expander("πŸ” Raw API Response"):
                        st.json(response.model_dump())

                st.session_state.chat_history.append({"role": "assistant", "content": full_response})

            except Exception as e:
                st.error(f"πŸ’₯ API Error: {e}")