I’ve developed an app with a multi-turn chat interface. Initially, I use a “Restart” button to send an empty request to the server, prompting the bot to initiate a conversation. Users can continue the conversation through an input box, and the ongoing dialogue is displayed in the chat column. The above function works fine.
The “Clear” button functions correctly too, clearing the history immediately as intended.
However, I’m encountering an issue with the “Restart” button:
My goal is for this button to clear the existing conversation history and then resend the initial request to start a new session.
Although as shown in my log, st.session.messages updates correctly, reflecting the cleared state, the conversation history remains visible on the rendered webpage.
Could anyone help resolve this issue where the conversation history doesn’t disappear upon restarting?
import sys
import json
import requests
import streamlit as st
def init_chat_history():
with chat_column:
if "messages" in st.session_state:
for message in st.session_state.messages:
avatar = '🐼' if message["role"] == "user" else '🤖'
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
else:
st.session_state.messages = []
return st.session_state.messages
st.set_page_config(page_title='Demo', layout="wide")
st.title('Demo')
chat_column, side_column = st.columns((0.6, 0.4))
def stream_chat(messages):
data = {
'uid': 'web-demo',
'session_id': "1",
'messages': messages
}
response = requests.post(
"http://127.0.0.1:7081/stream_chat",
json=data,
stream=True)
answer = ''
for byte_line in response.iter_lines():
byte_line = byte_line.rstrip(b'\n')
if byte_line.startswith(b'data:'):
data = json.loads(byte_line.decode().lstrip('data:'))
answer += data['text']
yield answer
def clear():
if hasattr(st.session_state, 'messages'):
del st.session_state.messages
def start():
if hasattr(st.session_state, 'messages'):
del st.session_state.messages
messages = init_chat_history()
with chat_column:
with st.chat_message("assistant", avatar='🤖'):
placeholder = st.empty()
for response in stream_chat(messages):
placeholder.markdown(response)
messages.append({"role": "assistant", "content": response})
return response
def main():
messages = init_chat_history()
st.button("Restart", on_click=start)
st.button("Clear", on_click=clear)
if (prompt := st.chat_input("Enter to sent. Shift + Enter to change line.")):
with chat_column:
with st.chat_message("user", avatar='🐼'):
st.markdown(prompt)
messages.append({"role": "user", "content": prompt})
with st.chat_message("assistant", avatar='🤖'):
placeholder = st.empty()
for response in stream_chat(messages):
placeholder.markdown(response)
messages.append({"role": "assistant", "content": response})
if __name__ == "__main__":
main()
image1: 0.5s after clicking “Restart”, the result is streaming here, leaving the previous turn conversation history uncleaned.
image2: 1s after clicking “Restart”, history is cleaned, but the the bot’s greeting duplicates (actually it is from this new round, not the previous round)
images 3: as the conversation continues, the extra bot’s greeting are replaced by normal conversation.
So image1 and image2 are not what I expected. Image 3 is normal but it only happens after the user gives a query.
The thing is, when I click “Clear”, it seems the logic is same with “Restart”, but it cleans all display instantly as I intended.