import streamlit as st
from groq import Groq
# Define the API key here
GROQ_API_KEY = "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"
# Initialize session state for chat history
if "chat_history" not in st.session_state:
st.session_state.chat_history = [
{"role": "system", "content": "you are a helpful assistant. Take the input from the users and try to provide as detailed response as possible. Provide proper examples to help the user. Try to mention references or provide citations to make it more detail-oriented."}
]
if "previous_sessions" not in st.session_state:
st.session_state.previous_sessions = []
# Function to fetch response
def fetch_response(user_input):
client = Groq(api_key=GROQ_API_KEY)
st.session_state.chat_history.append({"role": "user", "content": user_input})
chat_completion = client.chat.completions.create(
messages=st.session_state.chat_history,
model="mixtral-8x7b-32768",
stream=False
)
response = chat_completion.choices[0].message.content
st.session_state.chat_history.append({"role": "assistant", "content": response})
return response
# Streamlit app
st.set_page_config(page_title="Fastest AI Chatbot", page_icon="🤖", layout="wide")
st.markdown(
"""
""",
unsafe_allow_html=True
)
# Sidebar for previous sessions
st.sidebar.title("Previous Sessions")
st.sidebar.markdown("
", unsafe_allow_html=True)
for i, session in enumerate(st.session_state.previous_sessions):
if st.sidebar.button(f"Session {i + 1}"):
st.session_state.chat_history = session
st.sidebar.markdown("
", unsafe_allow_html=True)
st.title("Fastest AI Chatbot")
st.write("Ask a question and get a response.")
# Display chat history
st.markdown("", unsafe_allow_html=True)
for chat in st.session_state.chat_history:
if chat["role"] == "user":
st.markdown(f"**You:** {chat['content']}")
elif chat["role"] == "assistant":
st.markdown(f"**AI:** {chat['content']}")
st.markdown("
", unsafe_allow_html=True)
# Custom input field
user_input = st.text_input("Enter your question here:")
# Button to trigger response
if st.button("Get Response"):
response = fetch_response(user_input)
st.write("Response:", response)
# Save session button
if st.button("Save Session", key="save_session", help="Save the current chat session", class_="save-session"):
st.session_state.previous_sessions.append(st.session_state.chat_history)
st.session_state.chat_history = [
{"role": "system", "content": "you are a helpful assistant. Take the input from the users and try to provide as detailed response as possible. Provide proper examples to help the user. Try to mention references or provide citations to make it more detail-oriented."}
]
st.experimental_rerun()
# Footer
st.markdown(
"""
""",
unsafe_allow_html=True
)