Changing the font-size of the ChatInterface

Hi

I have been struggling with changing the font size for the ChatInterfce. After some experimentation, using stylesheets, I got to identify the class .bk-panel-models-markup-HTML.markdown.message. Still, it doesn’t work, maybe because this class is contained in a shadow root, but I admit I have a little understanding of JS.


Screenshot 2024-01-11 07.58.36

The code I am using is
chatplay.py (986 Bytes)

Could you provide any suggestions about how to deal with the Card resizing? Yes the Card, because as could be observed from the image, the user logo and the Card, must also be resized to match the new font-size.
Thanks in advance for any help you may provide.

2 Likes

Welcome to the Shadow-DOM. :sweat_smile:
image

chatinterface.send({"user": 'system',
                    "value": pn.pane.Markdown(
                        '<span style="font-size: x-small;">Hello...what can I do for you?</span>',
                        styles=dict(background='WhiteSmoke'),
                        )
                    }
                    )

You can pass a Markdown pane, and resize the font from within that, or alternatively, an HTML pane.
As for the icons, I don’t think there’s a direct way to accomplish that.

If you want more fine control, I’d suggest working with the ChatMessages directly.

I’m not fully certain, as I haven’t done this myself, but there may be an incredibly hacky way to traverse the shadow DOMs one by one and the modify the elements with JS.(e.g. trigger a script when the app loads that deploys the style changes you wish to make)

I do wish the chat components were more customizable, either through the classes themselves or through the DOM with custom JS.

You can also try a Feature Request in Github. I think that Chat UIs are incredibly popular nowadays, and Panel would benefit immensely from having one that has a lot of styling flexibility.
(just look at how gorgeous LMStudio is)
That said, I think the one we do have, is pretty damn awesome, all things considered, especially with the Pane and Widget integrations.

1 Like

Yes, can you submit an issue on GitHub? I think it’s important to style it too.

First of all, thank you very much for the time to answer my doubts and I apologize for not answering before. It was not a lack of interest, I was so immersed in finishing the prototype, that I didn’t realize there were already answers to my questions.

Dleybel thanks very much. I will play around with your concept for adjusting the font-size and I will publish here my conclusions. Thanks very much for your help. :smiling_face:

Regarding opening a Feature Request in Github, I will be more than glad to do it but I need some time to figure out how to do it… I was not even able to find a way to show my code so I uploaded it :disappointed_relieved:… I will figure out how to properly open the Feature Request, and it will be done. To much pressure these days to deliver results.

Thanks again.

1 Like

Hi again, I am back.

Well, I have tried different approaches but clearly the one proposed by dleybel works perfect.
Here the code snippet:

import panel as pn

R_USER = "user"
R_SYSTEM = "system"
R_ASSISTANCE = "assistance"

chatinterface_params = dict(
    default_avatars={
        R_SYSTEM: pn.chat.message.SYSTEM_LOGO,
        R_USER: "👤",
        R_ASSISTANCE: pn.chat.message.DEFAULT_AVATARS["gpt3"]
    },
    reaction_icons={},
)

async def _callback(contents: str, user: str, instance: pn.chat.ChatInterface):
    print(contents)

    return _format_answer("Done", color="yellow")

def _format_answer(txt:str, fontsize= 12, color= "white", background= "blue"):
        txt= f'<span>{txt}</span>'         
        mrk= pn.pane.Markdown(txt, styles=dict(color= color,
                                                #font_weight= 'bold',
                                                font_size= f"{fontsize}px",
                                                background=background, 
                                                border_radius= '13px',
                                                box_shadow= '5px 5px 15px #00a0a0'))
        return  mrk 

def my_send(*arg):
    print(arg)

chatinterface = pn.chat.ChatInterface(
    user=R_USER,
    callback=_callback,
    callback_exception='verbose',
    callback_user=R_ASSISTANCE,
    placeholder_threshold=2,
    placeholder_text="Waiting for reply...",
    message_params=chatinterface_params,
    show_clear=False,
    show_stop=False,
    show_undo=False,
    show_rerun=False,
    styles={"background": "#0a0a0a"},
    # https://github.com/holoviz/panel/blob/main/panel/chat/interface.py 
    button_properties={"send": {"icon": "send", "callback": my_send}} 
)
chatinterface.send({"user": 'system', "value": _format_answer("Hello, ... What can I do for you?")})

pn.Column(chatinterface).show()

As usual, a new problem arises and is related to the send button. The output, is the default output from Send and I could not find a way to hack the event to change the content to Markdown format the output message. I have tried to use button_properties={“send”: {“icon”: “send”, “callback”: my_send}} to capture the content before show, but the callback variables, ChatInterface and Events are too dense. I got to the point where working with code debugging interface, I identified and tried to set background, but it is deprecated and suggest to use Styles. I have explored the option to use Styles during the instantiation of the ChatInterface class, hopping to introduce an equivalent parametrization as the one from Markdown widget, but after some digging and experimentation, I am pretty sure the Style is not working, a pity.

I think that the way to fix this is to have my own callback to the on_click event for the Send Button, in that way I can managed the action and format the content as I whish. But if I have to get that extreme, maybe the best way to procced is not using the ChatInterface and build my own interface using ChatFeed blackboard interface, combine with my own TextInputArea and Send button. May be this is the right way to go!?

Regarding the ChatMessage, I haven’t been able to figure out, how to parametrize the image (50px-50px), or the text that represent the Role or the Time of the message (both 13px), etc. In my case, focusing on the ChatFeed and Chatmessage may fix some limitations, but to deal with the parametrization of the front (JS), something else needs to be done.

Do you still think that make sense to open a feature request?
Suggestions, comments?

Thanks in advance for all the help and time.
Regards

1 Like

Hi again,

I figure out how to do it. “arg” is a list of objects, the first one is the ChatInterface. Working with this instance, “ChatInterface.objects”, keep a list of the different ChatMessage, and the last of the list is the send message. Then is possible to synch the Send with the callback_post, then is possible to rewrite the ChatMessage object created by the Send button click:

def my_send(*arg):

    arg[0].objects[-1].object= _format_answer(arg[0].objects[-1].object, color= "white", background= "red")


chatinterface = pn.chat.ChatInterface(
    user=R_USER,
    callback=_callback,
    callback_exception='verbose',
    callback_user=R_ASSISTANCE,
    placeholder_threshold=2,
    placeholder_text="Waiting for reply...",
    message_params=chatinterface_params,
    show_clear=False,
    show_stop=False,
    show_undo=False,
    show_rerun=False,
    styles={"background": "#0a0a0a"},
    # https://github.com/holoviz/panel/blob/main/panel/chat/interface.py 
    button_properties={"send": {"icon": "send", "post_callback": my_send}} 


It works and the performance is ok.
Make sense? Comments, suggestions?
Thanks, in advance

2 Likes

Thank you for sharing!

I’ll work on making this a lot easier in the near future.

Thanks a lot.
My next stop it is not displaying the avatar in all the chats… I will explore how to hide or show the avatar icon. In my understanding, there are 3 actors, the app (system), the user, and the bot. If I use colors maybe some messages don’t require the avatar.

Finally, I don’t know how to help you, but I will stay tuned.
Best regards,

Maybe ChatInterface(message_params=dict(show_avatar=False))

Thanks.
Yes, you are right.
I have checked the message.py and found the different options available.
Excellent work.
Nice week end.

These docs should contain all the styling opts
https://panel.holoviz.org/reference/chat/ChatFeed.html
https://panel.holoviz.org/reference/chat/ChatInterface.html
https://panel.holoviz.org/reference/chat/ChatMessage.html

ChatInterface inherits from ChatFeed which contains ChatMessages.

In the Panel 1.4.0, styling should be made a lot easier: