DashChatComponent shows initial message twice

This seems to be a bug, or at least there is a different way to display a welcome message. I adapted the example from the package documentation and this reproduces your issue:

import time
import dash
from dash import callback, html, Input, Output, State
from dash_chat import ChatComponent



app = dash.Dash(__name__)

app.layout = html.Div([
    ChatComponent(
        id="chat-component",
        messages=[
            # {"role": "assistant", "content": "Hello"}
        ],
    )
])

@callback(
    Output("chat-component", "messages"),
    Input("chat-component", "new_message"),
    State("chat-component", "messages"),
    prevent_initial_call=True,
)
def handle_chat(new_message, messages):
    if not new_message:
        return messages

    updated_messages = messages + [new_message]

    if new_message["role"] == "user":
        time.sleep(0.2)
        bot_response = {"role": "assistant", "content": "auo response"}
        return updated_messages + [bot_response]

    return updated_messages

if __name__ == "__main__":
    app.run(debug=True)

You might search in this thread: Dash Chat Component

@gbolly

1 Like