Hey everyone, I am trying to make a conversational bot for customer issue resolution. I take input from user through chat_input. I am using chat_messages to display the messages for the user. However, there are some instances where the messages are to be shown along with options as buttons.
Expected Behavior: When the user clicks on the button, the label of the button should be taken as chat_input and conversation should continue.
Observed Behavior: This is not happening. Instead, when the button is clicked, the conversation stops altogether.
Following is a snippet of the code. Any help appreciated.
user_input = st.chat_input("Type your message here...")
print(f"User input type: {user_input}")
if user_input:
st.session_state["messages"].append({"role": "user", "content": user_input})
with st.chat_message("user", avatar=user_avatar):
st.markdown(user_input)
#with st.spinner("Please wait while we work on getting the best resolution for your issue..."):
loading_placeholder = st.empty()
display_loading_animation(loading_placeholder)
#display_loading_animation(duration=5)
#print(f"conversation_id_type:{type(st.session_state["conversation_"])}")
# Prepare request data
request_data = {
"conversation_id": str(st.session_state["conversation_id"]),
"user_response": str(user_input),
"user_id": "user_1234", # Replace with actual user ID
"timestamp": datetime.now().isoformat(),
"metadata": {} # Add any necessary metadata
}
print(f"REQUEST_DATA:{request_data}, type: {type(request_data)}")
#request_data = {key: str(value) for key, value in request_data.items()}
try:
response = requests.post(f"{API_URL}/chat", json=request_data)
response.raise_for_status() # Raise an error for bad responses
if response.status_code == 200:
response_data = response.json()
follow_up_messages = response_data.get("follow-up-messages", [])
for follow_up in follow_up_messages:
text = follow_up.get("text", "")
cta_list = follow_up.get("cta_list", [])
cta_list=["Yes","No"]
# Display the assistant's message
if isinstance(text, list):
for individual_text in text:
if individual_text and individual_text != "Moving to live agent" and individual_text!='no_follow_up':
st.session_state["messages"].append({"role": "assistant", "content": individual_text})
with st.chat_message("assistant", avatar=assistant_avatar):
st.markdown(individual_text)
else:
# If text is not a list, display it as a single message
if text and text != "Moving to live agent":
st.session_state["messages"].append({"role": "assistant", "content": text})
with st.chat_message("assistant", avatar=assistant_avatar):
st.markdown(text)
# if text and text!="Moving to live agent":
# st.session_state["messages"].append({"role": "assistant", "content": text})
# with st.chat_message("assistant", avatar=assistant_avatar):
# st.markdown(text)
# Display call-to-action options (if any)
if cta_list:
st.session_state["cta_list"] = cta_list
for option in cta_list:
st.button(option,disabled=st.session_state.button_clicked,key=option)
Basically require help with Display call-to-actions portion.
Thank you