File size: 1,253 Bytes
eb50bae
 
 
 
 
cdb865e
eb50bae
 
 
 
 
9f76fe9
eb50bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import os

# 🔐 Get secret from Hugging Face (NOT hardcoded)
API_KEY = os.getenv("GROQ_API_KEY_ChatBot")

API_URL = "https://api.groq.com/openai/v1/chat/completions"

st.set_page_config(page_title="Groq AI Chatbot", layout="centered")

st.title("AI Chatbot")

if "messages" not in st.session_state:
    st.session_state.messages = []

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

def get_response(messages):
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": "llama-3.1-8b-instant",
        "messages": messages,
        "temperature": 0.7
    }

    res = requests.post(API_URL, headers=headers, json=payload)

    if res.status_code == 200:
        return res.json()["choices"][0]["message"]["content"]
    else:
        return res.text

user_input = st.chat_input("Type your message...")

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

    reply = get_response(st.session_state.messages)

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

    st.rerun()