Skip to content
The Chat Archive
gemini-2.0-flash-grounding· Google 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

    To change the background color of st.chat_message in Streamlit from the default dark grey to white (or any other color), you can't directly modify the component's style. However, you can achieve this effect using CSS injection to override the default styling. Here's a breakdown of how to do it, along with considerations for different scenarios:

    Method: CSS Injection

    Streamlit allows you to inject custom CSS to modify the appearance of its elements. You'll target the specific CSS class associated with st.chat_message and override its background color.

    Here's the code to inject custom CSS to change the background color of st.chat_message to white:

    import streamlit as st
    
    # Define a function to inject custom CSS
    def local_css(file_name):
        with open(file_name) as f:
            st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
    
    # Option 1:  Create a CSS file (e.g., 'style.css')
    # style.css:
    '''
    div.streamlit-chat-message {
        background-color: #FFFFFF !important; /* White background */
    }
    '''
    
    # Option 2:  Embed CSS directly in the Streamlit app
    css_string = """
    <style>
    div.streamlit-chat-message {
        background-color: #FFFFFF !important; /* White background */
    }
    </style>
    """
    
    st.markdown(css_string, unsafe_allow_html=True)
    
    assistant_avatar = "🦖"  # Or your chosen avatar
    
    with st.chat_message("assistant", avatar=assistant_avatar):
        st.write("Hello, I'm a white chat message!")
    
    with st.chat_message("user"):
        st.write("Hello, I'm a default chat message!")
    

    Key improvements and explanations:

    • !important: The !important flag is crucial here. Streamlit's default styling might have higher specificity, so !important ensures your style overrides it.
    • unsafe_allow_html=True: This is necessary for st.markdown to render the HTML (including the `

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

Identical first prompt