chainlit icon indicating copy to clipboard operation
chainlit copied to clipboard

Is there a way to programmatically invoke the chat to be cleared?

Open abhat-koppert opened this issue 1 year ago • 2 comments
trafficstars

Hi!

First off, Chainlit so far has been amazing! I have managed to make a quick prototype in a few hours, and it has a lot of functionality I like built in.

However, I wanted to have a set up where if the user crosses a certain MAXIMUM_MESSAGE_LIMIT, they would be forced off the chat. I would prefer to have it such that the chat is cleared and the history is not saved - something I see already done with the "New Chat" button on the top right.

Similarly, if the conversation is idle for a certain amount of time, I would like to have it clear the chat as well. I had attempted using the @cl.on_start decorated method(not added below) as a call, but that did not clear the chat - it only started a new thread.

Is it possible to have this sort functionality? For reference, here are the @cl.on_message and @cl.action_callback methods I defined.

@cl.on_message
async def on_message(message_from_ui: cl.Message):
    thread = cl.user_session.get("thread")  # type: Thread
    await run(
        thread_id=thread.id, human_query=message_from_ui.content)
    
    
    counter = cl.user_session.get("assistant_message_count")
    counter += 1
    logger.info(f"Assistant message count: {cl.user_session.get('assistant_message_count')}")
    cl.user_session.set("assistant_message_count", counter)
    if cl.user_session.get("assistant_message_count") > MAXIMUM_MESSAGE_LIMIT:
        actions = [
            cl.Action(name="new_chat", value="new_chat", label="Start new Chat", collapsed=False),
        ]
        await cl.Message(content="Thank you for your time. You have reached the limit of messages.", actions=actions).send()
        

@cl.action_callback("new_chat")
async def on_action(action):
    await cl.Message(content=f"Executed {action.name}").send()
    # Have a function call that forces a new chat to initialize
    await action.remove()

Let me know if there's any possibility of getting this sort of functionality set up. Cheers! :)

abhat-koppert avatar Jan 19 '24 10:01 abhat-koppert