Guide: Use OpenAI Assistants to create an entry in your database

Sebastian Avelar →

Bubble Developer
Web Developer
AI Developer
Bubble
OpenAI
Hey there!
I had an idea to turn videos into quizzes for an app idea and decided to share what I learned regarding OpenAi Assistants functions.
I will cover the following topics:
If you need further help using OpenAi Assistants in your App, book a call here:

How the Assistants API work

The first step is to create a “thread”. This gives you a thread_ID that we will use later on.
Once we have our “thread”, we create a message and add that message to the “thread” by using the thread_ID from the previous step
Then we create a “Run” and this step returns a Run_ID. The “Run” has a different status, we will focus only on the following: “in_progress”, “requires_action” and “completed”.
The next step is checking the “Run” status. The Run will have the status “in_progress” as soon as it starts. When the Assistant calls the function, it will have the status “requires_action”.
When the status is “requires_action”, we need to submit the call_ID of our function and the Output of it. Once this is done, the “Run” will continue and we need to check the status of the “Run” again.
It will go back to “in_progress” if the call_ID and the output we passed are correct. Once the status is “completed”, we will receive the JSON we defined in our function and we will send that JSON in the body of a POST request to our Bubble app which will create an entry in our database.

Create a function for our Assistant

I assume you already have an assistant, if you don’t go ahead and create it here: https://platform.openai.com/assistants
Here you can see I already have a function called “create_quizz_v2”
Create a new function and it will give us 2 examples to choose from, I chose “get_weather”.
You can use GPT to create the JSON snippet for you, here is an example of how to do it:
The parameters should be the same as the datatype you plan to create in your Bubble app
Quizz Datatype View
Quizz Datatype View
And my function ended up looking like this:
OpenAi Assistants Functions
OpenAi Assistants Functions

How to call our Assistant from our Bubble app

I set the API calls before and I won’t go into detail as there are many other videos that explain how to set them up
The first action “Open Ai - Create a thread” is an API call that creates a thread
In the second Action, I save the thread_ID in a custom state to reference after. (You can save it in your database as well)
The third action creates the message and adds it to the thread we created previously
The fourth action creates the “Run” using the thread_ID again. This action gives us the run_ID
I save the run_ID in a state
I schedule a Custom event to check the run status
The last action scheduled an event that checks the status of the “Run” and triggers other custom events depending on the status.
For checking the status, I have 2 identical custom events. This is because a custom event can’t be scheduled by its own actions, so you need to have an identical you can schedule.
These are the parameters of these custom events
And these are the actions, both have the same except for step 3.
The first action checks the status of the run and we refer to it in the next steps to decide which custom event we should trigger.
The second step schedules a custom event when the status is “completed” The third step schedules OpenAi: Check quiz status-2 if the status is “in_progress”. The fourth step schedules a custom event when the status is “requires_action”.
When the status is “requires_action”, it means our assistant used the “function” and needs the approval to continue the “Run”
We need to send to our assistant the call_ID and the content (arguments) of each. In my case, each call_ID, represents one quiz.
There are 3 actions in this Custom event.
The first is an API call that submits the call_ID and their content
The second action checks the “Run” status
Last action schedules again OpenAi: Check quiz status-1, which we have previously seen.
This is what the OpenAI - Submit Tool output to run API call looks like:

Make an API call to our Bubble app to create a new entry in our database

After we have submitted our tool output, the assistant will continue with the “Run” I have another custom event that gets triggered when the status is “completed”
This custom event makes an API call to our Bubble app sending the final output of the assistant
I used a “find & replace” to take out the word “JSON” from the result of the assistant *(By the moment this guide was made, Assistants didn't have a JSON response parameter)
The API call looks like this:
The data that is inside the green square, is just for triggering the endpoint for the first time. It has the same structure as the JSON coming from the assistant looks like
This API call triggers a backend workflow that is responsible for creating the quizzes and adding them to our database
I hope you have found this guide helpful and feel free to ask any questions :)
If you need further help, you can book a coaching call here:
Partner With Sebastian
View Services

More Projects by Sebastian