| |
| import os |
| import streamlit as st |
| from groq import Groq |
|
|
| |
| client = Groq(api_key=os.getenv("GROQ_API_KEY")) |
|
|
| |
| 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_options = { |
| "π¬ Fast Chat (LLaMA 3 8B)": "llama3-8b-8192", |
| "π§ Deep Reasoning (LLaMA 3 70B)": "llama3-70b-8192", |
| "π Math Solver (Mixtral Instruct)": "mixtral-8x7b-instruct" |
| } |
|
|
| model_choice = st.selectbox("Choose assistant type:", list(model_options.keys())) |
| model_id = model_options[model_choice] |
|
|
| |
| 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] |
|
|
| |
| if "messages" not in st.session_state: |
| st.session_state.messages = [{"role": "system", "content": system_prompt}] |
|
|
| |
| if st.button("π Clear Chat"): |
| st.session_state.messages = [{"role": "system", "content": system_prompt}] |
| st.rerun() |
|
|
| |
| for msg in st.session_state.messages[1:]: |
| with st.chat_message(msg["role"]): |
| st.markdown(msg["content"]) |
|
|
| |
| if prompt := st.chat_input("Type your message..."): |
| st.chat_message("user").markdown(prompt) |
| st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
| |
| 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}") |
|
|