How to 'chat' with your database using Buildship and OpenAi

Sebastian Avelar →

Low-Code/No-Code
Web Developer
Bubble
BuildShip
OpenAI
Hello, I have seen some people asking how to let an AI assistant read the content of their Bubble database and let their users ‘chat’ with their data.
I will show you how to let an AI assistant query different data types from your database and perform more actions using a chat interface. This can be done also with (supabase, airtable, firebase, etc)
You can test the chat at this Link

Overview

We will create 2 backend workflows in our Bubleapp which will be triggered by the Ai assistant. We will build the assistant using the Open Ai Assistant node from BuildShip and learn how to integrate it in our app.
Guide Overview
Guide Overview

Database Overview

We will use 2 tables for this example to keep things simple. One table is Contacts and the other one Company
Database Overview
Database Overview

Setting up Backend Workflows

The first backend workflow is for creating a Contact. It’s a POST API call that accepts some information to create a contact in our database.
This workflow includes two actions: one to create the contact and another to return data to the client.
I want to be able to create a contact and link it to a company, for that I have set up a Search for Companies and added a filter for the name of the company. These values will be filled by our Ai assistant
The Return data action is for letting know the AI that the request was successful and we pass the data of the new contact.
The second backend workflow is for searching through the Contacts and Company datatype. It’s also a POST API Call and it accepts the name of the ‘table’ we want to search through.
I use the ‘table’ parameter to create conditions for the actions of this workflow.
We have 2 'Return data from API actions, each of them does a search but in different datatypes depending on the condition. Our AI assistant will decide what ‘table’ to search for and fill in the value when making the POST API call to this workflow.

Setting up the AI Assistant

For setting up our Assistant I will use Buildship, it’s a low-code backend that will handle all the API calls necessary to interact with Open AI Assistants AP

You don’t need to use Buildship for this; it can also be achieved with Bubble by setting up all the API calls necessary. I use it because it handles also the ‘function callings’ automatically and that would take me more time to do in Bubble.

If you want to give it a quick try, you can clone the workflow that I made for this guide:
This is the overview of our workflow in Buildship. This workflow (series of actions) is triggered by a POST API call to the endpoint I called ‘/assistants’.
You must add your open AI API key for this workflow & your Assistant ID to work. You can do it by clicking the input field.
You can get your API key here: https://platform.openai.com/api-keys
You can get your Assistant ID here: https://platform.openai.com/assistants
You can add your API key as a secret and the assistant ID as an environmental variable
The user prompt is assigned to the variable ‘message’ from the body of the endpoint, while the thread ID is set to the variable ‘threadId,’ also from the body of the endpoint.
These are the instructions I added to my assistant. Notice I tell it to use the tools and how:
Instructions You can retrieve information by using the tool 'search database'. Its a POST API call, where you can pass the name of the table in the body: {"table": "string"} You can query the tables: - contacts - company You can pass the name of the table to get the information of that table You can also create a contact by using the tool 'create contact'. This is the body structure to create a new contact {"title": 'string', "name": 'string', "company": 'string' }
The OpenAI Assistants node comes with another part where you can start adding more nodes (actions). In our case, we want to add 2 API Calls that send a request to Bubble Backend workflows.
One API call is called 'search database’ and the other ‘create contact’.
For search database, I made it a POST request, filled the URL and content_type. The body is being filled by the AI, the blue spark icon indicates that the input is AI filled
When clicking on the sparks icon, a popup appears to customize the instructions of that node. In my example, I wrote the options the AI can choose from: contacts & company.
Instructions: The body to send with the API call. names of the table to querry are 'contacts' and 'company'
This is what triggers the right actions in our ‘search’ backend workflow in Bubble
For create contact , is the same logic but the instructions of the body are different. I specify the JSON format that needs to be passed.
Instructions: The body to send with the API call. {"title": 'string', "name": 'string', "company": 'string' }
The ‘return’ node is the last one we have. You can change the response to add more values if you need. In our case, we are returning back the response of the Ai assistant and the Thread ID
You can deploy your workflow by clicking the Blue Ship button, which will generate your Endpoint URL and a Chat Widget.

Integrating the Ai assistant

There are two ways to integrate this assistant: one is by setting up an API call to the Buildship endpoint from your Bubble app, and the other is by using the Chat Widget and embedding it in your Bubble app.
API Call to Buildship Endpoint You can set it up as a POST API call, use as an action, and fill in the body parameters. You can now use it across your app as you need it!
Chat Widget When clicking the Chat widget button, you can customize it, test it, and get the code for embedding the chat widget inside your app.
Copy the Code Snippet into an HTML Element in Bubble. You will get a simple button that you can customize to the appearance you need. On preview mode, clicking the button will open the chat widget
Partner With Sebastian
View Services

More Projects by Sebastian