How to sort a list in descending order in a repeating group

Hello guys ,
I am trying to reduce my workload costs . I created a chat bot application , right now I am testing out things whether they work or not . I have to fields of messages for database one is from AI bot and another is from the user .
Before I use to create a new table everytime a user or the AI send a message and store it . But now I was wondering whether I can try like this :?

I will create one table for every user individually and in that table I will create two fields (Ai messages , User messages ). So when a message one I will add the message to the table as following example :

I create a table when a user send “Hi” and if the user send another message saying “How are you today” , Instead of creating a new table I will combine the text like this [Hi(1),How are you today(3)] (1, 3 numbers are ID for every message , they are unique)
So to display the chat I will split these messages using spliby function separating from (,) . Now I will have list of two messages like Hi(1) and How are you today(3) .
I want to know how can I sort this list according to the numbers in descending order .

Note:I cant display it after splitting because in between those messages there are AI bot messages aswell . AI messages will be like “Hello , How can I assist you (2)” and “I am good , How are you. Thanks for asking (4)” .

Example messages to get a clear idea

You need the following structure:

Option set: Role

  • User
  • Assistant


  • User (User)
  • title (text)


  • Conversation (Conversation)
  • Role (Role option set)
  • content
  • User

To display messages, do a search for messages for X conversation, sorted by created date in ascending order.

To submit the messages as message history in an API call to OpenAI or similar, Do a search for Message:format as text and inside each one it’ll be something like {"role": This Message's Role's id:formatted as JSON-safe, "content": This Message's content:formatted as JSON-safe}.

1 Like

Do a search function costing lot of workloads , that’s why I choose to get this preexisting list so that searching workload wont be necessary .

Like we will just get the message texts from the user and split them then sort them and then display them accordingly

Don’t do that, for a whole host of reasons. Just don’t. The simplest solution is generally the correct one.

I am just doing a trial and error , So if this works and if it reduce the workloads , this method might be useful for the users in other use cases as well . But if it should not be done no matter what , I wont do it .
Would you mind specifying the few reasons

  • Your structure format can be broken easily if a user formats their message in a certain way
  • When you display a list of conversations in a sidebar, all of the conversation’s message history will be downloaded at the same time (as it’s stored on the conversation) which will make it WU intensive and less performant
  • Your current structure stores no information, even as basic as when the message is sent, which you’ll want for analytics and basic tracking.
  • You’ll find exceedingly difficult to delete or edit past messages
  • You’ll have no way for users to attach specific actions (e.g flagging/rating a message with thumbs up/down for feedback) to a specific message

This method is not useful at all, unfortunately. You’re making life hard for yourself!

1 Like

Thanks for the knowledge , I really din’t go this deep

1 Like

And by the way can you tell me any alternative for creating a data everytime a new message is sent .

When a user send a message and after getting the reply there are three tables that are created .

Which is consuming more workload for single message sent

Any alternatives ?

Why 3?

When user clicks send:

  1. Create a new Message
  • content = Input’s value
  • Role = user
  • Conversation = (The conversation - possibly stored in a group as a variable like var - conversation)
  1. Make API call to Open AI

  2. Create a new message

  • content = Result of step 2’s choices:first item’s message content
  • Role = assistant
  • Conversation = The conversation

I am storing the system role also because when a user send a message , I will send all the previous messages to OpenAI to get answers based on past messages . The answers should be based on the systems prompt so I will send the systems prompt as a first message then other messages .

[Note: I am using a OpenAI plugin]|