Spaces:
Sleeping
Sleeping
File size: 4,645 Bytes
d4a7025 410e594 d4a7025 410e594 d4a7025 410e594 17c52e7 410e594 a3cd1b3 d4a7025 a3cd1b3 d4a7025 a3cd1b3 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 ee2d18c d4a7025 7789156 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 17c52e7 d4a7025 17c52e7 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
"""Module doc string"""
import os
import openai
import streamlit as st
from discord_webhook import DiscordWebhook
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
def discord_hook(message):
"""_summary_"""
if os.environ.get("ENV", "NOT_LOCAL") != "LOCAL":
url = os.environ.get("DISCORD_HOOK", "NO_HOOK")
if url != "NO_HOOK":
webhook = DiscordWebhook(
url=url, username="simple-chat-bot", content=message
)
webhook.execute()
discord_hook("Simple chat bot initiated")
def return_true():
"""_summary_"""
return True
def reset_history():
"""_summary_"""
st.session_state.openai_api_key = st.session_state.api_key
st.session_state.messages = []
def start_app():
"""_summary_"""
st.session_state.start_app = True
st.session_state.openai_api_key = st.session_state.api_key
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="simple-chat-bot",
page_icon="👾",
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 "start_app" not in st.session_state:
st.session_state["start_app"] = False
if st.session_state.start_app:
print(st.session_state.openai_api_key)
if (
st.session_state.openai_api_key is not None
and st.session_state.openai_api_key != ""
):
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()
else:
with st.chat_message("assistant"):
st.markdown("**'OpenAI API key'** is missing.")
with st.sidebar:
st.text_input(
label="OpenAI API key",
value="",
help="This will not be saved or stored.",
type="password",
key="api_key",
)
st.selectbox(
"Select the GPT model", ("gpt-3.5-turbo", "gpt-4-turbo"), key="openai_model"
)
st.slider(
"Max Tokens", min_value=20, max_value=80, step=10, key="openai_maxtokens"
)
st.button("Start Chat", on_click=start_app, use_container_width=True)
st.button("Reset History", on_click=reset_history, use_container_width=True)
if __name__ == "__main__":
main()
|