File size: 1,348 Bytes
69a825f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py

import os
import streamlit as st
from groq import Groq

# Set your GROQ API Key here directly (since in Colab it's easier)
GROQ_API_KEY = "your-groq-api-key-here"  # <-- Replace with your key

# Create the GROQ client
client = Groq(
    api_key=GROQ_API_KEY,
)

# Streamlit app
st.title("🤖 Chatbot using GROQ API")

# Store the conversation
if "messages" not in st.session_state:
    st.session_state.messages = []

# Display old messages
for message in st.session_state.messages:
    if message["role"] == "user":
        with st.chat_message("user"):
            st.write(message["content"])
    else:
        with st.chat_message("assistant"):
            st.write(message["content"])

# Input from user
user_input = st.chat_input("Type your message...")

if user_input:
    # Save user message
    st.session_state.messages.append({"role": "user", "content": user_input})

    # Send all messages to model
    chat_completion = client.chat.completions.create(
        messages=st.session_state.messages,
        model="llama-3-3-70b-versatile",
    )

    # Get model's reply
    reply = chat_completion.choices[0].message.content

    # Save assistant reply
    st.session_state.messages.append({"role": "assistant", "content": reply})

    # Display assistant reply
    with st.chat_message("assistant"):
        st.write(reply)