allyourbot / hostedgpt

An open version of ChatGPT you can host anywhere or run locally.
MIT License
192 stars 75 forks source link

Add support for versioned messages within the Message model #273

Closed krschacht closed 3 weeks ago

krschacht commented 1 month ago

This PR is my attempt to isolate all the backend logic changes to begin supporting versioned messages. All of the messages in a conversation now have:

With those model changes, a bunch of query logic was updated so that it's pretty easy to create messages and not have to think too hard about what the index and version are.

An old conversation looks like this:

0.1
1.1
2.1
3.1

The first number is the message index, the second number after the decimal is the version of that message. In this start case it's the first version of every message. Now, message index 2 is going to be edited creating a new version and the assistant will reply. Now the messages for this conversation look like this:

0.1
1.1
2.1 2.2
3.1 3.2

The messages at index of 2 both have branched = true and if you check the versions attribute for those you'll get [1,2]. Even though there are two versions of message index 3 it was not branched there so versions will be an empty array. In other words, message 2 is where the UI is going to let you do next/previous and not at message 3.

By default, when you do conversation.messages.for_conversation_version(1) you will get the original message sequence back. But when you do ...(2) or the shorthand conversation.messages.latest_version_for_conversation you get the following message sequence back:

0.1
1.1
2.2
3.2

And it supports more complicated branching such as this. In this example, the user went back to message 1 and branched again from there so there are now 3 different versions of this conversation and the message at index 0 is the only message that is shared between all three versions.

0.1        
1.1       1.3
2.1  2.2  2.3
3.1. 3.2