File size: 4,027 Bytes
4309cba a067442 4309cba a067442 4309cba bf681f9 a067442 4309cba 7219ca8 4309cba 8cf160b 7219ca8 4309cba 7219ca8 4309cba 7219ca8 4309cba 7219ca8 4309cba 7219ca8 4309cba fb4ad29 a067442 b617d5d a067442 c761672 a067442 4309cba a067442 4309cba a067442 c9fa163 |
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 |
import streamlit as st
from openai import OpenAI
# Setting up the Streamlit page configuration
st.set_page_config(page_title="StreamlitChatMessageHistory", page_icon="💬")
st.title("Chatbot")
# Initialize session state variables
if "setup_complete" not in st.session_state:
st.session_state.setup_complete = False
if "user_message_count" not in st.session_state:
st.session_state.user_message_count = 0
if "feedback_shown" not in st.session_state:
st.session_state.feedback_shown = False
if "chat_complete" not in st.session_state:
st.session_state.chat_complete = False
if "messages" not in st.session_state:
st.session_state.messages = []
# Helper functions to update session state
def complete_setup():
st.session_state.setup_complete = True
# Setup stage for collecting user details
if not st.session_state.setup_complete:
# st.subheader('Personal Information')
st.subheader('Personal Information afaefaef')
# Get personal information input
st.session_state["name"] = st.text_input(label="Name", value="", placeholder="Enter your name", max_chars=40)
# Company and Position Section
st.subheader('Company and Position')
st.session_state["position"] = st.selectbox(
"Choose a position",
("Data Scientist", "Data Engineer", "ML Engineer", "BI Analyst", "Financial Analyst"),
index=("Data Scientist", "Data Engineer", "ML Engineer", "BI Analyst", "Financial Analyst").index("Data Scientist")
)
st.session_state["company"] = st.selectbox(
"Select a Company",
("Amazon", "Meta", "Udemy", "365 Company", "Nestle", "LinkedIn", "Spotify"),
index=("Amazon", "Meta", "Udemy", "365 Company", "Nestle", "LinkedIn", "Spotify").index("Amazon")
)
# Button to complete setup
if st.button("Start Interview", on_click=complete_setup):
st.write("Setup complete. Starting interview...")
# Interview phase
if st.session_state.setup_complete and not st.session_state.chat_complete:
# Initialize OpenAI client
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
# Setting OpenAI model if not already initialized
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-4o"
# Initializing the system prompt for the chatbot
if not st.session_state.messages:
st.session_state.messages = [{
"role": "system",
"content": (f"You are an HR that interviews {st.session_state['name']}. You should interview him for the "
f"{st.session_state['position']} position in the company {st.session_state['company']}")
}]
# Display chat messages
for message in st.session_state.messages:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Handle user input and OpenAI response
# Put a max_chars limit
if st.session_state.user_message_count < 5:
if prompt := st.chat_input("Your response", max_chars=1000):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
if st.session_state.user_message_count < 4:
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],
stream=True,
)
response = st.write_stream(stream)
st.session_state.messages.append({"role": "assistant", "content": response})
# Increment the user message count
st.session_state.user_message_count += 1
# Check if the user message count reaches 5
if st.session_state.user_message_count >= 5:
st.session_state.chat_complete = True |