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:

7 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

As I mentioned, here is the guide.

Ill be building a template for Bubble and Buildship that will make this faster to set-up and easy to keep adding actions easily!

1 Like

It might not quite be on topic but is it possible to create a server function to wait for the result from open Ai

Yes, i think it should be possible. Where would the server function be hosted and would trigger it?

For anybody interested in adding AI capabilities to their app, I’m working on a plugin that will allow you to have an Open AI Assistant access to your Bubble database!

The assistant will be able to:

  • Call your database before answering a user message to get context
  • Answer back in JSON format or natural language
  • Create a thing inside your database
  • Update a thing in your database
  • Create in bulk a list of things

Usecase:

  1. Users can ask questions about the items inside your database
    Example: You have an app with a list of products, and your users will be able to ask the AI assistant for information about them through natural language. You will also be able to filter a repeating group

  2. Users can create/update things in your database
    Example: A user can create a new product by just describing it the assistant will format the data and create it.

Most clients I have worked with need these 2 use cases, if anybody else has another use-case you might need it, let me know here!!

If you are interested in trying out this plugin, leave a message here or DM me!

I’m building the first version and I would like some feedback :slight_smile:

1 Like

Functions/Tools are already available to meet those use-cases.

1 Like

Yeah for sure, but I will abstract all of the setup and will make a plugin that handle this

This way people won’t have to setup functions by themselves, do polling, submit outputs IDs, etc

It will make it easier for bubble devs who are not experienced with Open Ai assistant API :slight_smile:

I wonit do self promotion for only the sake of doing so here, but if you are able to make it simpler than in OpenAI - Assistants with Streaming Plugin | Bubble, let’s talk :slight_smile:

1 Like

Yes, I think I can make it more simple!

I think your plugin is great but aimed more at devs who are more comfortable using API’s and have used Bubble for some time. Also I imagine your plugin allows more detailed control just from looking at it

My goal with this plugin is to have an action that lets the user send a message to the Assistant, and the assistant will be ready to query the bubble database without needing to set any functions

As soon as I have the first version, I’ll contact you back and you can give it a try :slight_smile:

Hello!
I’m offering a service for allowing an OpenAI assistant to read your Bubble Database using Buildship.
This service includes a Buildship Workflow with Documentation and a One-on-One Setup Support Call in case you have problems or want to customize it further!

It’s a great opportunity to learn how to use Buildship Open AI assistants node and how to connect it with Bubble. Buildship takes care of the polling, submitting tool outputs id and also lets you add new function calls very easily.

You can learn more about this service here :slight_smile:

1 Like