File size: 1,712 Bytes
d8ae058
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf4e306
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
import streamlit as st
import requests
# Set up the Groq API endpoint and your API key
GROQ_API_URL = "https://api.groq.com/v1/chat/completions"
GROQ_API_KEY = "your_groq_api_key_here"  # Replace with your actual Groq API key

# Streamlit app title
st.title("Educational Chatbot")

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

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# Accept user input
if prompt := st.chat_input("What is your question?"):
    # Add user message to chat history
    st.session_state.messages.append({"role": "user", "content": prompt})
    # Display user message in chat message container
    with st.chat_message("user"):
        st.markdown(prompt)

    # Send user message to Groq API
    headers = {
        "Authorization": f"Bearer {GROQ_API_KEY}",
        "Content-Type": "application/json"
    }
    data = {
        "model": "groq-3.5-turbo",  # Replace with the model you want to use
        "messages": st.session_state.messages
    }
    response = requests.post(GROQ_API_URL, headers=headers, json=data)
    if response.status_code == 200:
        chatbot_response = response.json()["choices"][0]["message"]["content"]
        # Add chatbot response to chat history
        st.session_state.messages.append({"role": "assistant", "content": chatbot_response})
        # Display chatbot response in chat message container
        with st.chat_message("assistant"):
            st.markdown(chatbot_response)
    else:
        st.error("Failed to get response from Groq API")