How to: Use Open AI Assistants funtions to create an entry in your database

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.

You can check the demo here: (work in progress)

I will cover the following topics:

  • How the Assistants API work
  • Create a function for our Assistant
  • How to call our Assistant from our Bubble app
  • Make an API call to our Bubble app

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 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

And my function ended up looking like this:

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

  1. The first action “Open Ai - Create a thread” is an API call that creates a thread
  2. 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)
  3. The third action creates the message and adds it to the thread we created previously
  4. The fourth action creates the “Run” using the thread_ID again. This action gives us the run_ID
  5. I save the run_ID in a state
  6. 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
Third 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

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

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 :slight_smile:

If you need further help, you can book a call with me here:


You can check my other “How to guide” here also on the forum:

5 Likes

AMAZING
Are you offering any kind of developer help?

1 Like

Glad you liked the post!

Sure, I do consulting and you can check my portfolio in the link below as well as hire my service :slight_smile:

Thanks for sharing. Can you post a link to the editor so that we can follow along on some of the steps that you didn’t go into too much detail about?

Also, looks like the live link is dead now due to your account type.

1 Like

@anthony.woodard91
Yeah, the live link is dead

I was paying the personal plan for that app to have access to the Data API but decided to take it out.

I will be happy to help if you have any specific questions, I can’t share the editor as I have few API keys of different services there.

I also do consulting if you would rather have a longer session to discuss your usecase! : )

Also, I’m now using Buildship to create OpenAi assistants, it’s really useful and easy to integrate with Bubble through the API connector!

I will make another guide this month for it!

1 Like

Hi! Great posts, thanks for sharing this knowledge. Question, what’s the advantage/s of using Buildship? Why not doing everything in bubble?

Hey @eugenio.leiguarda

Thanks! Yes, right now I use Buidlship for this

It’s helps to avoid do all the setup yourself.
The ai assistant node of Buidlship, handles all the function calling process, let’s you add a bunch of Buidlship ‘actions’ to the assistant.

If someone would like to test something fast, it would be faster in Buidlship as all these API call are being handled by you.

I will be making a guide next week to show how to integrate an ai assistant with your bubble app to perform a series of searches or create entries in the database from a chat interface

1 Like