File size: 1,775 Bytes
6ab2291
 
 
 
22a4d86
6ab2291
22a4d86
6ab2291
 
 
 
22a4d86
 
6ab2291
22a4d86
 
 
6ab2291
22a4d86
 
6ab2291
22a4d86
6ab2291
 
 
22a4d86
 
6ab2291
22a4d86
6ab2291
 
 
 
22a4d86
6ab2291
 
 
 
 
 
22a4d86
6ab2291
 
 
 
 
22a4d86
 
 
 
 
 
 
 
6ab2291
22a4d86
 
 
6ab2291
22a4d86
 
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
54
55
56
57
58
59
60
61
62
import os
import streamlit as st
from groq import Groq

# Streamlit page configuration
st.set_page_config(
    page_title="LLAMA 3.1 Chat",
    page_icon="🦙",
    layout="centered"
)

# Retrieve API key from Hugging Face Secrets
GROQ_API_KEY = os.getenv("GROQ_API_KEY")

if not GROQ_API_KEY:
    st.error("⚠️ Error: GROQ_API_KEY is missing! Please add it to Hugging Face Secrets.")
    st.stop()

# Initialize Groq client with API key
client = Groq(api_key=GROQ_API_KEY)

# Initialize the chat history in Streamlit session state if not present already
if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

# Streamlit page title
st.title("🦙 LLAMA 3.1 ChatBot")

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

# Input field for user's message
user_prompt = st.chat_input("Ask LLAMA...")

if user_prompt:
    st.chat_message("user").markdown(user_prompt)
    st.session_state.chat_history.append({"role": "user", "content": user_prompt})

    # Send user's message to the LLM and get a response
    messages = [
        {"role": "system", "content": "You are a helpful assistant"},
        *st.session_state.chat_history
    ]

    try:
        response = client.chat.completions.create(
            model="llama-3.1-8b-instant",
            messages=messages
        )

        assistant_response = response.choices[0].message.content
        st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})

        # Display the LLM's response
        with st.chat_message("assistant"):
            st.markdown(assistant_response)

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