Skip to content

Commit

Permalink
add selection for the model and the bot role
Browse files Browse the repository at this point in the history
  • Loading branch information
krisograbek committed Jul 15, 2023
1 parent 9c8faa7 commit 7b99a4c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 10 deletions.
95 changes: 85 additions & 10 deletions chatbot.py
Original file line number Diff line number Diff line change
@@ -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"""
### ℹ️ <span style="white-space: pre-line; font-family: Arial; font-size: 14px;">Current model: {st.session_state.model}.</span>
""",
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
<span style="white-space: pre-line; font-family: Arial; font-size: 14px;">{description}</span>
""",
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 = ""
Expand All @@ -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})
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
openai
streamlit
python-dotenv

0 comments on commit 7b99a4c

Please sign in to comment.