Skip to content
The Chat Archive
sonar-pro LMArena Search Arena 24K

with st.chatmessage("assistant", avatar=assistantavatar): messageplaceholder =…

  1. User

    with st.chat_message("assistant", avatar=assistant_avatar): message_placeholder = st.empty() st.session_state.stop_streaming = False

                                full_response = ""
                                start_time = time.time()
                                
                                print('-------Get Prompt-------')
                                print(llm)
                                for chunk in llm(prompt, max_tokens=8000, top_k=15, top_p=0.4, repeat_penalty=1.2, temperature=0.1, stream=True):
                                    if st.session_state.stop_streaming:
                                        st.session_state.stop_streaming = True
                                        break
                                    chunk_text = chunk['choices'][0]['text']
                                    print(chunk_text)
                                    full_response += chunk_text
                                    message_placeholder.markdown(full_response + "▌", unsafe_allow_html=True)
                                execution_time = time.time() - start_time
    
                                ntokens = len(full_response.split())
                                if ntokens > 0:
                                    timing_stats = f'<span style="color: grey; font-size: 0.8em; font-style: italic;">Literature ⏳ {execution_time:.2f}s | {(1.0*execution_time/ntokens):.3f}s/token | {round((1.0*ntokens/execution_time),1)} tokens/s</span>'
                                    full_response += f"<br>{timing_stats}"
                                message_placeholder.markdown(full_response, unsafe_allow_html=True)
    

    this is great but I want. to make st.chat_message to be white and not dark grey for some of the portion as seen in the image

    i am using streamlit for building a chatbot

  2. Assistant

    Absolutely! By default, Streamlit's st.chat_message uses theme colors (which can be dark or light depending on your app's theme). If you want to customize the background color of the chat message—specifically to make it white for the assistant's messages—you'll need to use a bit of custom CSS.

    Here's how you can do it:


    1. Inject Custom CSS

    You can use st.markdown with the unsafe_allow_html=True flag to inject CSS into your Streamlit app. Place this at the top of your script (or before your chat UI):

    import streamlit as st
    
    # Inject custom CSS for assistant chat messages
    st.markdown("""
        <style>
        /* Target assistant chat messages */
        div[data-testid="chat-message"] div[role="listitem"]:has(div[data-testid="stMarkdownContainer"]) {
            background-color: #fff !important;  /* White background */
            color: #222 !important;             /* Dark text for readability */
            border-radius: 12px !important;
            box-shadow: 0 1px 4px rgba(0,0,0,0.04);
            padding: 1em !important;
        }
        /* Optional: Only target assistant messages by avatar or other unique identifier */
        </style>
    """, unsafe_allow_html=True)
    

    Note:

    • The selector above targets all chat messages. If you want to only target the assistant's messages, you need a more specific selector. If your assistant avatar is unique, you can use an attribute selector or add a custom class to the message container (see below).

    2. Target Only Assistant Messages (Optional)

    If you want to only style the assistant's messages, you can:

    • Give the assistant messages a unique class or data attribute (if possible).
    • Or, if the avatar image is unique, use an attribute selector for the avatar.

    For example, if your assistant avatar is "assistant_avatar.png":

    div[data-testid="chat-message"] img[src*="assistant_avatar.png"] {
        /* This targets the avatar image */
        /* You can use sibling selectors to style the parent container */
    }
    

    But Streamlit's DOM structure may change, so inspect the HTML in your browser to find the best selector.


    3. Example: Putting It All Together

    import streamlit as st
    import time
    
    # Custom CSS for assistant messages
    st.markdown("""
        <style>
        /* Example: Make all chat messages white */
        div[data-testid="chat-message"] {
            background-color: #fff !important;
            color: #222 !important;
            border-radius: 12px !important;
            box-shadow: 0 1px 4px rgba(0,0,0,0.04);
            padding: 1em !important;
        }
        </style>
    """, unsafe_allow_html=True)
    
    assistant_avatar = "assistant_avatar.png"  # Your avatar path
    
    with st.chat_message("assistant", avatar=assistant_avatar):
        message_placeholder = st.empty()
        st.session_state.stop_streaming = False
        full_response = ""
        start_time = time.time()
        for chunk in llm(prompt, max_tokens=8000, top_k=15, top_p=0.4, repeat_penalty=1.2, temperature=0.1, stream=True):
            if st.session_state.stop_streaming:
                st.session_state.stop_streaming = True
                break
            chunk_text = chunk['choices'][0]['text']
            full_response += chunk_text
            message_placeholder.markdown(full_response + "▌", unsafe_allow_html=True)
        execution_time = time.time() - start_time
        ntokens = len(full_response.split())
        if ntokens &gt; 0:
            timing_stats = f'<span style="color: grey; font-size: 0.8em; font-style: italic;">Literature ⏳ {execution_time:.2f}s | {(1.0*execution_time/ntokens):.3f}s/token | {round((1.0*ntokens/execution_time),1)} tokens/s</span>'
            full_response += f"<br/>{timing_stats}"
        message_placeholder.markdown(full_response, unsafe_allow_html=True)
    

    4. Tips

    • Inspect your app in the browser (right-click → Inspect) to find the exact HTML structure and adjust the CSS selector accordingly.
    • If you want to only style some messages, consider adding a custom class or data attribute to those messages (if Streamlit allows).

    Let me know if you need help with a more specific selector based on your avatar or message type!
    If you share your app's HTML structure (from browser inspect), I can provide a more targeted CSS snippet.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.