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.
Limit OpenAI messages: Learn the secret to optimizing your OpenAI conversations in Bubble!
Master Bubble workflows: Discover how to send only the most recent messages and avoid token limits with this workflow hack.
Boost your AI chat app: Unlock the power of context-aware conversations while keeping your OpenAI requests efficient.
Limiting Messages Sent to OpenAI API
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 low-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? And 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.
Implementing a Message Limit
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. So 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 has 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. This is the box I'm going to change. I'm going to set it to only send the last 10 messages.
Sorting Messages by Date
I'll go back to just search for messages. If you were to constrain them by add a constraint by conversation, you would have that in here. But I'm going to do something such as created date descending yes. 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.
Using Items Until for Message Limit
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. The items from is only going to be useful in our calculation of the last 10 if we're keeping a count 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 sort of running count in your app. Maybe you could keep a count on the conversation. In that case, yeah, you could make it work. But you could also just use what I'm about to show you with items until. So I'm going to say items until 10.
Reversing the Order for OpenAI
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 10 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. So now that I've got this operator of getting the most recent 10 messages, they're in the wrong order. So now I've put them back in the right order which is created date descending note. So now they're again in the right chronological order for OpenAI.
Formatting Messages as JSON
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 roll. I simply need to put I'm not sure in this demo if I have this set up. Basically this message. This message roll. 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 400s because the syntax issues with our JSON.
Finalizing the Message Format
So I'm going to get rid of all of that. And the context is this content is this message is text JSON safe. So remember we format it like this because JSON safe encloses 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 the syntax error JSON safe. So set for us. So. Yeah they 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.
Conclusion and Call to Action
And if you find this video useful and you're learning Bubble you need to check out our website planetnocode.com. 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 love to teach you about building a Bubble and many of those videos are exclusive to our members. And you can find out more at planetnocode.com.
Can't find what you're looking for?
Search our 300+ Bubble tutorial videos. Start learning no code today!
Flexible Pricing Plans to Fit Your No-Code Journey
Choose the plan that aligns with your goals and start building your startup today.
Have questions?
We have answers!
Find answers to common questions about our membership plans, programs, and more.
Both plans offer full access to our learning resources, community, and support. The Annual plan provides a significant discount (over 15%) compared to paying monthly, and it allows you to lock in your rate for a full year.
Absolutely! You can easily upgrade or downgrade your membership plan at any time by logging into your account and selecting the desired plan. Any unused portion of your current plan will be prorated and applied to your new plan.
As a Planet No Code member, you'll receive a discount on our Bubble coaching sessions. Monthly members receive a 10% discount, while Annual members receive a 17.5% discount. To redeem your discount, simply log into your account and book a coaching session through our platform.
Our 8-week intensive mentorship program is designed to provide personalized guidance and support to help you accelerate your startup journey. You'll be matched with a startup expert who will work with you one-on-one to set goals, overcome challenges, and make rapid progress.
To apply for the Mastery Program, simply click the "Request Invitation" button on our pricing page and fill out the application form. Our team will review your application and schedule a call with you to discuss your goals and determine if the program is a good fit for your needs.
We accept all major credit cards, including Visa, Mastercard, American Express, and Discover.
While we don't offer a free trial, we do provide a 14-day money-back guarantee. If you're not completely satisfied with your membership within the first 14 days, simply contact our support team, and we'll issue a full refund.
If you decide that Planet No Code isn't the right fit for you, you can cancel your membership at any time by logging into your account and navigating to the subscription management page. Click the "Cancel Membership" button, and your membership will be terminated at the end of your current billing cycle.