| | import streamlit as st |
| | import os |
| | from groq import Groq |
| |
|
| | |
| | api_key = os.getenv("GROQ_API_KEY") |
| |
|
| | |
| | client = Groq(api_key=api_key) |
| |
|
| | |
| | st.set_page_config(page_title="Groq Chatbot", page_icon="🤖") |
| | st.title("🤖 Groq Chatbot (LLaMA3-70B)") |
| |
|
| | |
| | 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"]) |
| |
|
| | |
| | prompt = st.chat_input("Type your message...") |
| |
|
| | if prompt: |
| | |
| | st.session_state.messages.append({"role": "user", "content": prompt}) |
| | st.chat_message("user").markdown(prompt) |
| |
|
| | |
| | with st.spinner("Thinking..."): |
| | try: |
| | response = client.chat.completions.create( |
| | model="llama3-70b-8192", |
| | messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages], |
| | ) |
| | reply = response.choices[0].message.content |
| | except Exception as e: |
| | reply = f"⚠️ Error: {str(e)}" |
| |
|
| | |
| | st.session_state.messages.append({"role": "assistant", "content": reply}) |
| | st.chat_message("assistant").markdown(reply) |
| |
|