ChatBox clear / reset button

Has anyone had luck adding a second button to ChatBox to reset / clear the conversation? Great there is a chat_box.clear() function, now how do I call it?! :grinning: Any help would be greatly appreicated!

1 Like

Haven’t tested, but something like:

def clear(event):
    cb.clear()

cb = pn.widgets.ChatBox()
button = pn.widgets.Button()
button.on_click(clear)
pn.Row(cb, button)

Thanks so much! I’m still struggling integrating your solution into what I have working. Basically, it looks like pn.Row and pn.bind are not playing nicely together (or, more accurately, I don’t know how to make them work together nicely). I can comment out the pn.bind & chat_box.servable() rows and the Clear button shows up and works (but chat doesn’t) OR I can comment out the pn.Row row and the pn.bind chat works just fine.

I’m sure I’m missing something (probably simple) and your help is greatly appreciated! Really enjoying this library BTW! Keep up the awesome work!

Here is an example:

import panel as pn
from panel.widgets import ChatBox, Button
from typing import List, Dict

pn.extension()

def clear(event):
    # Clear the Chat Window
    chat_box.clear()
    # Append the greeting
    chat_box.append({"AI": "Greetings, how can I help you today?"}) 
    
def chat(user_messages: List[Dict[str, str]]) -> None:
    # Call some service
    # Append the results
    chat_box.append({"AI": "That's nice"}) 
    

chat_box = pn.widgets.ChatBox(
    value = [
        {"AI": "Greetings, how can I help you today?"},
    ],
    primary_name="You",
)

button = pn.widgets.Button(name='Clear')
button.on_click(clear)
pn.Row(chat_box, button)

pn.bind(chat, user_messages=chat_box, watch=True)    
chat_box.servable()```
2 Likes

Thanks for the kind words!

Something like:

import panel as pn
from panel.widgets import ChatBox, Button
from typing import List, Dict

pn.extension()

def clear(event):
    chat_box.clear()
    chat_box.append({"AI": "Greetings, how can I help you today?"}) 
    
def chat(user_messages: List[Dict[str, str]]) -> None:
    if not user_messages or "AI" in user_messages[-1]:
        return
    # Call some service
    # Append the results
    chat_box.append({"AI": "That's nice"}) 
    

chat_box = pn.widgets.ChatBox(
    value = [
        {"AI": "Greetings, how can I help you today?"},
    ],
    primary_name="You",
)

button = pn.widgets.Button(name='Clear')
button.on_click(clear)

pn.bind(chat, user_messages=chat_box, watch=True)
pn.Row(chat_box, button).servable()

Or you rather than calling clear + append, simply replace the objects.


def clear(event):
    chat_box.value = [{"AI": "Greetings, how can I help you today?"}]

In the near future, there will be ChatBox v2 (ChatInterface) that has a reset button built in

That is great news! I was able to get your modifications to work, the formatting isn’t perfect, but I’m sure that will be easier once the functionality is built into the package. I also added a ā€œtemperatureā€ slider as I am using this to interact with an LLM and want to be able to adjust with each request. Super useful and I’m looking forward to V2!

2 Likes