Limited Time Offer: Watch All Our Bubble Tutorial Videos for Only $99 per Year!

Bubble & OpenAI - Send only recent messages

In this Bubble tutorial we demonstrate how you can lower your OpenAI token spend by just sending the most recent messages to the OpenAI API. This method will ensure your message still has some awareness of previous messages without sending the oldest messages.

Thank you to everyone who has subscribed, liked, and commented on the video. It really helps our YouTube channel and us to continue to create fantastic content around Bubble and no-code development. One of the comments we received was asking a question about OpenAI and Bubble, and that is how can I limit the number of messages that I send through to the OpenAI API?

That's because even if you increase the token limit to say, the 16K models, you will eventually get to a point where the conversation with your user exceeds the number of tokens that you can send back over to OpenAI. Because remember, the OpenAI requires you to send all of the history of all of the messages of that conversation in order for it to be aware of what's been said in previous messages.

How to limit OpenAI messages sent

I'm going to show you how very quickly you could limit it to say the last 10 messages so that there is still some contextual historical awareness in the conversation, but you're not sending 60 messages, you're only sending 10.

I'm going to adapt this workflow here. This is one that I've demoed in plenty of other OpenAI videos. If you're not sure how to get to this point, go and check those ones out. But I basically create a message. That's the one that the user's created. I then send all of the messages that have been between the user and the AI. I then reset the inputs and I create a new message. That's the AI's response. And this is the box I'm going to change. I'm going to set it to only send the last 10 messages.

So I'll go back to just search for messages. And if you were to constrain them by, add the constraints by conversation, you would have that in here. But I'm going to do something such as created date descending yes. And so what this will do is this will put the most recent message first. This is the wrong way around for OpenAI. OpenAI is expecting, like any other chat application, for the oldest to be up top, and then for it to get newer as the conversation goes down to the bottom. So the last message should be the most recent. I'm flipping it around. And that's because of a limitation that Bubble gives us here, which it gives us items until and items from. And the items from is only going to be useful in our calculation of the last 10 if we're keeping account of how many messages there are.

So if there have been 40 messages, we need to know there's been 40 messages to take 10 from that, leaving us with 30. And then we could say items from 30. Now you could keep some running count in your app. Maybe you could keep a count on the conversation. In that case, yeah, you could make it work.

Using 'Items Until' to Sort Messages

You could also just use what I'm about to show you with items until. I'm going to say items until 10. And so what that will do is it's going to start with the most recent message and it will then show the previous 10. So message one is the most recent, message two is the one before that, all the way through to message ten, which will be the oldest message, but of that most recent block of 10. It's still the wrong way around for OpenAI.

So then what I can do is I can say, Sort. Now that I've got this operator of getting the most recent 10 messages, they're in the wrong order. So now I put them back in the right order, which is created date descending no. So now they're again in the right chronological order for OpenAI.

And then I can add in my format as text. And this is how I get my JSON expression. And to do that, I'm just going to pop into the OpenAI documentation and copy this part here. The delimiter, that's the computer science term for what you put in between. So that's going to be a comma and a space. And then role, I simply need to put, I'm not sure in this demo if I have this set up. Basically, this message, oh, this message role. Fine. There we go. So that's again if it's system, assistant or user. And then the context, remember, I'm making this all JSON safe. We don't want to be getting those error 400 because of syntax issues with our JSON. So I'm going to get rid of all of that. And the context is this content is this message's text, JSON safe. So remember, we format it like this because JSON safe enclosed the content in speech marks, and also it will escape. It will make safe any characters in the content that the user has typed in that could cause a syntax error, json-safe solves it for us. So there you have it.

That's how to send the most recent 10 messages. You reverse the order, get the first 10, reverse the order back, and then format as text.

And if you find this video useful and you're learning Bubble, you need to check out our website, We've got hundreds of Bubble tutorial videos. At the moment, we're releasing a new video basically every day of the week. Our library is growing and growing and growing, and we've got so much that we'd love to teach you about building a Bubble. And many of those videos are exclusive to our members.

Latest videos