File size: 2,554 Bytes
ad8bdbe
5316f23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad8bdbe
 
 
5316f23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
import streamlit as st, pathlib, os
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")
    openai_api_key = os.environ.get("OPENAI_API_KEY")
    # "[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.")