Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to edit messages #3

Closed
Michael-Tanzer opened this issue Mar 3, 2023 · 15 comments
Closed

Option to edit messages #3

Michael-Tanzer opened this issue Mar 3, 2023 · 15 comments

Comments

@Michael-Tanzer
Copy link
Contributor

Hi,

I think the possibility to edit a message like you can do on ChatGPT would be an amazing addition to the project.
To do this it looks like you might need to change the storage of chat from an array to a tree-like structure. And then of course change the API call and chat display parts as well.

I might be able to look into it in a couple of weeks if no one else gets around to doing it first.

Best

@Michael-Tanzer Michael-Tanzer changed the title Edit message Option to edit messages Mar 3, 2023
Niek added a commit that referenced this issue Mar 3, 2023
@Niek
Copy link
Owner

Niek commented Mar 3, 2023

Good idea, I started to work on this a bit in commit cd2b9b9. To get it fully working, all messages after the edited one have to be discarded.

@Michael-Tanzer
Copy link
Contributor Author

I don't think discarding is a good idea. One way this could be dealt with is by adding a new field to every message (e.g. versions[]). When a message is edited, all the messages before the one that was edited could add to their versions[] the current new version (= old version + 1), while all the ones that came after would have their version unchanged. The user can now choose what "version" of the chat to look at and only the messages with right version would be shown (simple filter)

@Niek
Copy link
Owner

Niek commented Mar 3, 2023

But that would be confusing, editing a message other than the last one will affect the context of the messages. That's why ChatGPT removes all later messages if a message is edited.
The way I see it there are 2 options:

  • Discard later messages (like ChatGPT does)
  • Only allow editing the last message (actually this makes more sense to me)

What do you think?

@Michael-Tanzer
Copy link
Contributor Author

Michael-Tanzer commented Mar 3, 2023

Alternatively you could have a tree-like structure in the messages, something like this:

export const addMessage = (chatId: number, message: Message) => {
  const chats = get(chatsStorage);
  const chat = chats.find((chat) => chat.id === chatId);

  // Check if the message has a parentId, indicating that it's an edited message
  if (message.parentId !== undefined) {
    // Find the parent message
    const parentMessage = findMessageById(chat.messages, message.parentId);

    // Create a new message with the same text and user as the original message,
    // but with a new id and a parentId of the original message's parentId
    const newMessage: Message = {
      id: generateNewMessageId(),
      role: message.role,
      content: message.content,
      usage: message.usage,
      parentId: parentMessage?.id,
      children: [],
    };

    // Add the new message as a child of the original message
    if (parentMessage) {
      parentMessage.children = [...parentMessage.children, newMessage];
    } else {
      chat.messages.push(newMessage);
    }
  } else {
    // If the message doesn't have a parentId, it's a new message
    chat.messages.push({
      ...message,
      id: generateNewMessageId(),
      children: [],
    });
  }

  chatsStorage.set(chats);
};

function findMessageById(messages: Message[], id: number): Message | undefined {
  for (const m of messages) {
    if (m.id === id) {
      return m;
    }
    const childMessage = findMessageById(m.children || [], id);
    if (childMessage) {
      return childMessage;
    }
  }
  return undefined;
}

Where

  export type Message = {
    id: number;
    role: "user" | "assistant" | "system";
    content: string;
    usage?: Usage;
    parentId?: number;
    children?: Message[];
  };

In this case the editing would simply mean adding a new message with a different parent id. Versions could still be used to show the latest version when displaying a chat

@Michael-Tanzer
Copy link
Contributor Author

But that would be confusing, editing a message other than the last one will affect the context of the messages. That's why ChatGPT removes all later messages if a message is edited. The way I see it there are 2 options:

* Discard later messages (like ChatGPT does)

* Only allow editing the last message (actually this makes more sense to me)

What do you think?

I also wanted to point out that ChatGPT does not discard old messages, you can still see them using the small arrows on the left of the message you edited (see below). This is very similar to my idea.

Screenshot_10
Screenshot_11
Screenshot_12

@Michael-Tanzer
Copy link
Contributor Author

Here clicking the arrows will bring back all messages that stemmed from message 2 version 1

@Niek
Copy link
Owner

Niek commented Mar 3, 2023

Ah, I didn't even notice that before, good point. In that case a tree-like structure would indeed be best, although it's not very trivial to iterate through it.

@Michael-Tanzer
Copy link
Contributor Author

I made a non-functional proof of concept here: #4, unfortunately I have no idea how Svelte works and the pull request is not currently working. Might be a good starting point though.

@Niek
Copy link
Owner

Niek commented Mar 3, 2023

Awesome, I'll check it in the morning and add some comments.

@Michael-Tanzer
Copy link
Contributor Author

Fixed the issue in #4 where the new messages were not being shown. The new commit should be backward compatible with the previous implementation, but I haven't tried the message edit functionality

@Niek
Copy link
Owner

Niek commented Mar 5, 2023

Good one! I think it can be simplified a bit by having only a parentId property - that should be enough to build the whole tree (chats sharing the same parentId are edited versions).

@Niek
Copy link
Owner

Niek commented Mar 5, 2023

@all-contributors please add @Michael-Tanzer for ideas and code

@allcontributors
Copy link
Contributor

@Niek

I've put up a pull request to add @Michael-Tanzer! 🎉

@Michael-Tanzer
Copy link
Contributor Author

Good one! I think it can be simplified a bit by having only a parentId property - that should be enough to build the whole tree (chats sharing the same parentId are edited versions).

It's definitely possible, it makes iterating over the chat a bit harder though

@Niek
Copy link
Owner

Niek commented Jun 9, 2023

Fixed in #152

@Niek Niek closed this as completed Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants