chat / Home.py
aiXpert's picture
v1
5316f23
raw
history blame
2.49 kB
import streamlit as st, pathlib
from utilities import get_products, aichat
folder = pathlib.Path(__file__).parent.resolve()
st.set_page_config(page_title="Chatbots", page_icon="🚀",)
st.write("# 👋 Welcome to Receipt Bot!👋")
st.markdown(
"""
#### 🚀Intelligent Bookkeeping and Document Management Hub🍨
"""
)
# st.sidebar.title("Store Spark")
st.sidebar.image(f"{folder}/resources/sslogo.png", use_column_width=True)
with st.sidebar:
# store_link = st.text_input("Enter Your Store URL:", value="http://hypech.com/StoreSpark", disabled=True, key="store_link")
openai_api_key = st.text_input("OpenAI API Key", key="chatbot_api_key", type="password")
"[Get an OpenAI API key](https://platform.openai.com/account/api-keys)"
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo-0125"
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
st.session_state.messages.append({"role": "system", 'content': f"""
You are General Chat Bot, an AI assistant for General Purpose.
"""})
st.session_state.messages.append({"role": "assistant", "content": "How May I Help You Today💬?"})
# Display chat messages from history on app rerun
for message in st.session_state.messages[1:]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("💬"):
if not openai_api_key:
st.info("Please add your OpenAI API key to continue.")
else:
try:
# 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)
# Display assistant response in chat message container
with st.chat_message("assistant"):
stream = aichat(messages = [
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],openai_api_key=openai_api_key)
response = st.write_stream(stream)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
except:
st.info("Invalid OpenAI API key. Please enter a valid key to proceed.")