diff --git a/chatbot.py b/chatbot.py index 6b218a5..94b2cb7 100644 --- a/chatbot.py +++ b/chatbot.py @@ -1,34 +1,108 @@ import streamlit as st import openai import os - from dotenv import load_dotenv load_dotenv() - openai.api_key = os.environ["OPENAI_API_KEY"] +# Sidebar +st.sidebar.title("Configuration") + + +def model_callback(): + st.session_state["model"] = st.session_state["model_selected"] + + +if "model" not in st.session_state: + st.session_state["model"] = "gpt-3.5-turbo" + +st.session_state.model = st.sidebar.radio( + "Select OpenAI Model", + ("gpt-3.5-turbo", "gpt-3.5-turbo-16k"), + index=0 if st.session_state["model"] == "gpt-3.5-turbo" else 1, + on_change=model_callback, + key="model_selected", +) + +st.sidebar.markdown( + f""" + ### ℹ️ Current model: {st.session_state.model}. + """, + unsafe_allow_html=True, +) + +# Bot roles and their respective initial messages +bot_roles = { + "bot_role_1": { + "role": "system", + "content": "You are a friendly bot that speaks only Polish", + "description": "This is a friendly bot speaking in Polish.", + }, + "bot_role_2": { + "role": "system", + "content": "You are a friendly bot that speaks only German", + "description": "This is a friendly bot speaking in German.", + }, + "bot_role_3": { + "role": "system", + "content": "You are a friendly bot that speaks only English Pirate", + "description": "This is a friendly bot speaking in English Pirate.", + }, +} + + +def bot_role_callback(): + st.session_state["bot_role"] = st.session_state["bot_role_selected"] + st.session_state["messages"] = [bot_roles[st.session_state["bot_role"]]] + + +if "bot_role" not in st.session_state: + st.session_state["bot_role"] = "bot_role_1" + +st.session_state.bot_role = st.sidebar.radio( + "Select Bot Role", + tuple(bot_roles.keys()), + index=list(bot_roles.keys()).index(st.session_state["bot_role"]), + on_change=bot_role_callback, + key="bot_role_selected", +) + +description = bot_roles[st.session_state["bot_role"]]["description"] + +st.sidebar.markdown( + f""" + ### ℹ️ Description + {description} + """, + unsafe_allow_html=True, +) + + +# Function to reset messages +def reset_messages(): + return [bot_roles[st.session_state["bot_role"]]] + + +# Main App st.title("My Own ChatGPT!🤖") +# Initialize session state if "messages" not in st.session_state: - st.session_state.messages = [] - + st.session_state["messages"] = reset_messages() +# Display messages for message in st.session_state["messages"]: with st.chat_message(message["role"]): st.markdown(message["content"]) -# initialize model -if "model" not in st.session_state: - st.session_state.model = "gpt-3.5-turbo" - -# user input +# User input if user_prompt := st.chat_input("Your prompt"): st.session_state.messages.append({"role": "user", "content": user_prompt}) with st.chat_message("user"): st.markdown(user_prompt) - # generate responses + # Generate responses with st.chat_message("assistant"): message_placeholder = st.empty() full_response = "" @@ -43,5 +117,6 @@ ): full_response += response.choices[0].delta.get("content", "") message_placeholder.markdown(full_response + "▌") + message_placeholder.markdown(full_response) st.session_state.messages.append({"role": "assistant", "content": full_response}) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..00872a3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +openai +streamlit +python-dotenv \ No newline at end of file