File size: 3,490 Bytes
7f2cb09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Module doc string"""

import streamlit as st

from .constants import ConstantVariables
from .logs import logger
from .openai_utils import OpenAIFunctions


class StreamlitFunctions:
    """Module doc string"""

    @staticmethod
    def streamlit_page_config():
        """_summary_"""
        st.set_page_config(
            page_title="simple-chat-bot",
            page_icon="👾",
            layout="centered",
            initial_sidebar_state="auto",
        )
        st.title("👾👾 Simple Chat Bot 👾👾")

    @staticmethod
    def streamlit_side_bar():
        """_summary_"""
        with st.sidebar:
            st.text_input(
                label="OpenAI API key",
                value=ConstantVariables.api_key,
                help="This will not be saved or stored.",
                type="password",
                key="api_key",
            )

            st.selectbox(
                "Select the GPT model",
                ConstantVariables.model_list_tuple,
                key="openai_model",
            )
            st.slider(
                "Max Tokens",
                min_value=ConstantVariables.min_token,
                max_value=ConstantVariables.max_tokens,
                step=ConstantVariables.step,
                key="openai_maxtokens",
            )
            st.button(
                "Start Chat",
                on_click=StreamlitFunctions.start_app,
                use_container_width=True,
            )
            st.button(
                "Reset History",
                on_click=StreamlitFunctions.reset_history,
                use_container_width=True,
            )

    @staticmethod
    def streamlit_initialize_variables():
        """_summary_"""
        logger.debug("Initializing Streamlit Variables")
        if "messages" not in st.session_state:
            st.session_state.messages = []

        if "openai_model" not in st.session_state:
            st.session_state["openai_model"] = ConstantVariables.default_model

        if "openai_api_key" not in st.session_state:
            st.session_state["openai_api_key"] = None

        if "openai_maxtokens" not in st.session_state:
            st.session_state["openai_maxtokens"] = ConstantVariables.default_token

        if "start_app" not in st.session_state:
            st.session_state["start_app"] = False

    @staticmethod
    def reset_history():
        """_summary_"""
        logger.debug("Resetting Chat State")
        st.session_state.openai_api_key = st.session_state.api_key
        st.session_state.messages = []

    @staticmethod
    def start_app():
        """_summary_"""
        logger.debug("Starting Application")
        st.session_state.start_app = True
        st.session_state.openai_api_key = st.session_state.api_key

    @staticmethod
    def streamlit_print_messages():
        """_summary_"""
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

    @staticmethod
    def streamlit_invoke_model():
        """_summary_"""
        if prompt := st.chat_input("Type your Query"):
            with st.chat_message("user"):
                st.markdown(prompt)
            st.session_state.messages.append({"role": "user", "content": prompt})
            response = OpenAIFunctions.invoke_model()
            logger.debug(response)
            st.session_state.messages.append({"role": "assistant", "content": response[0]})