Avatar going back to Default in chat messages after another message is sent

Summary

After a message is sent the assistant’s avatar becomes the default one instead of keeping that which had when the message was sent

Code snippet:

if "messages" not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("Send a message"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant",avatar='🌏'):
        response = llm_chain.run(prompt)
        st.markdown(response)
    st.session_state.messages.append({"role": "assistant", "content": response,"avatar":'🌏'})

Expected behavior:
I believe the chat should be keeping the avatar assigned

Debug info

  • Streamlit version: 1.25.0
  • Python version: Python 3.8.0

Requirements file

1 Like

You are not setting any avatar when displaying the old messages.

And how could I do it?

Just like you did when displaying the last response, using the avatar keyword argument.

for message in st.session_state.messages:
    avatar = ... # decide which avatar you want for this message
    with st.chat_message(message["role"], avatar=avatar):
        st.markdown(message["content"])
2 Likes

Oh thanks, I had gotten a bit lost in the code

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.