File size: 3,230 Bytes
d4a7025
 
 
 
 
 
a3cd1b3
d4a7025
a3cd1b3
d4a7025
a3cd1b3
d4a7025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6ae968
d4a7025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Module doc string"""

import openai
import streamlit as st
from openai import OpenAI


def return_true():
    """_summary_"""
    return True


def reset_history():
    """_summary_"""
    st.session_state.messages = []


def check_openai_api_key():
    """_summary_"""
    try:
        client = OpenAI(api_key=st.session_state.openai_api_key)
        try:
            client.models.list()
        except openai.AuthenticationError as error:
            with st.chat_message("assistant"):
                st.error(str(error))
            return False
        return True
    except Exception as error:
        with st.chat_message("assistant"):
            st.error(str(error))
        return False


def main():
    """_summary_"""
    st.set_page_config(
        page_title="Test", layout="centered", initial_sidebar_state="auto"
    )
    st.title("Simple Chat Bot")

    if "messages" not in st.session_state:
        st.session_state.messages = []

    if "openai_model" not in st.session_state:
        st.session_state["openai_model"] = "gpt-3.5-turbo"

    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"] = 50

    if st.session_state.openai_api_key is not None:
        if check_openai_api_key():
            client = OpenAI(api_key=st.session_state.openai_api_key)

            for message in st.session_state.messages:
                with st.chat_message(message["role"]):
                    st.markdown(message["content"])

            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})

                with st.chat_message("assistant"):
                    stream = client.chat.completions.create(
                        model=st.session_state["openai_model"],
                        messages=[
                            {"role": m["role"], "content": m["content"]}
                            for m in st.session_state.messages
                        ],
                        max_tokens=st.session_state["openai_maxtokens"],
                        stream=True,
                    )
                    response = st.write_stream(stream)
                st.session_state.messages.append(
                    {"role": "assistant", "content": response}
                )
        else:
            reset_history()

    with st.sidebar:
        st.session_state["openai_api_key"] = st.text_input(
            label="OpenAI API key",
            value="***",
            help="This will not be saved or stored.",
            type="password",
        )

        st.selectbox(
            "Select the GPT model",
            ("gpt-3.5-turbo", "gpt-4-turbo-preview"),
        )
        st.slider(
            "Max Tokens", min_value=20, max_value=80, step=10, key="openai_maxtokens"
        )
        st.button(label="Reset Chat", on_click=reset_history)


if __name__ == "__main__":
    main()