Chainlit / chainlit

Build Conversational AI in minutes ⚡️
https://docs.chainlit.io
Apache License 2.0
6.89k stars 906 forks source link

Allow users to edit previous messages and "fork" #562

Open Enoch2090 opened 10 months ago

Enoch2090 commented 10 months ago

Support editing previous messages like ChatGPT. The conversation should "fork" when message is edited. If possible also creates a duplicate of the memory in LangChain?

willydouhard commented 10 months ago

This is an interesting feature and quite hard to implement in a framework agnostic way. I guess what we could do is add an edit button to root messages and a new cl decorator to let developers react to it?

Enoch2090 commented 10 months ago

This is an interesting feature and quite hard to implement in a framework agnostic way. I guess what we could do is add an edit button to root messages and a new cl decorator to let developers react to it?

My understanding is that if we can attach buttons to messages as "states" instead of using the input components only at the beginning of a chat, this will allow implementation of chains forking.
Frame agnostic is preferred but not necessary, developers can definitely do the clone of chat history manually.

willydouhard commented 10 months ago

We can already achieve this with:

  1. An update action
  2. The action trigger a ask user message to get the edited value from the user
  3. Update the message and remove the following messages (this would require to store those messages in the user_session)

Not the best user experience but would be a good proof of concept!

Enoch2090 commented 9 months ago

Hi Willy: Your proposed method is totally doable - but I do met a problem while implementing it:

Editing the memory from the framework is achieved, but for user experience we still need to remove messages after the edited ones.

Looking forward to your advices! Thanks!

willydouhard commented 9 months ago

Hey!

The the resumed chats, you have access to all the messages (including their ids) in the on_chat_resume decorated function.

From here you can probably instantiate a cl.Message with the correct id and call .remove() on it?

Enoch2090 commented 9 months ago

Hey!

The the resumed chats, you have access to all the messages (including their ids) in the on_chat_resume decorated function.

From here you can probably instantiate a cl.Message with the correct id and call .remove() on it?

  1. I think the messages will be automatically instantiated when on_chat_resume decorated function is called. I can instantiate messages according to the MessageDict here but I still need to remove those already displayed ones, or find a way to operate them (this is quite similar to losing track of a pointer in C++...).

  2. Regarding getting the edited value from the user, is it possible to use unsafe_allow_html = true in the config, then use simple JavaScript in the message body? I've played with using HTML + CSS in message body and it worked:

    msedge_December19-19-57-226b2a

    but script within HTML doesn't seem to work:

    <script>
    (function () {
        var textarea = document.getElementById('chat-input');
    
        textarea.value = 'New text in chat input';
    
        var event = new Event('input', {
            bubbles: true,
            cancelable: true,
        });
    
        textarea.dispatchEvent(event);
    
    })();
    </script>

    (pasting the JS alone in F12 console works tho) The idea of this hack is to set the chat input automatically.

nmstoker commented 7 months ago

@Enoch2090 - might it be possible to get your JS delivered via the custom JS feature? I don't know if it was present at the time you made your most recent comment, but it seems like it could help get around your problems getting it to run...

👉 https://docs.chainlit.io/customisation/custom-js