File size: 1,489 Bytes
d9f1c93
7f45ad6
 
 
97b65f0
4b4c745
 
97b65f0
041458e
97b65f0
4b4c745
7f45ad6
97b65f0
7f45ad6
 
97b65f0
7f45ad6
97b65f0
 
7f45ad6
 
 
 
 
 
 
 
 
 
97b65f0
 
 
7f45ad6
 
 
 
 
 
 
 
97b65f0
7f45ad6
 
 
97b65f0
7f45ad6
 
 
 
 
 
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
import os
import streamlit as st
from groq import Groq

# βœ… Set your Groq API key here (replace with your actual key)
#GROQ_API_KEY = MY_KEY  # πŸ” Replace this with your real key
GROQ_API_KEY = os.environ.get("MY_KEY")
# Initialize Groq client
client = Groq(api_key=GROQ_API_KEY)


# Set page configuration
st.set_page_config(page_title="Groq Chatbot", page_icon="πŸ€–")

# Title
st.title("πŸ€– Groq Chatbot using LLaMA 3")

# Text input from user
user_input = st.text_input("You:", placeholder="Type your question here...")

# Initialize chat history
if "messages" not in st.session_state:
    st.session_state.messages = []

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

# Handle user input
if user_input:
    # Add user message to history
    st.session_state.messages.append({"role": "user", "content": user_input})
    with st.chat_message("user"):
        st.markdown(user_input)

    # Call Groq API
    try:
        response = client.chat.completions.create(
            messages=st.session_state.messages,
            model="llama3-70b-8192",  # model ID from Groq
        )
        reply = response.choices[0].message.content

        # Add assistant reply to history
        st.session_state.messages.append({"role": "assistant", "content": reply})
        with st.chat_message("assistant"):
            st.markdown(reply)

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