How to: Open Ai Assistants read your Database and more

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.

Recording2024-09-10135859-ezgif.com-video-to-gif-converter

Sorry for the gif quality :sweat_smile:

You can test the chat in this link:

Table of contents:

  • Overview
  • Database Overview
  • Setting up Backend Workflows
  • Setting up the AI Assistant
  • Integrating the Ai assistant

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.

Database Overview

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


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.

For this test, the checkbox to run the workflow without authentication and ignore privacy rules was selected. However, for a live application, privacy rules should not be ignored, nor should it run without authentication.

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

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

Intructions:
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 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 an POST API call, used as action and fill the body parameters. You can now use it across your app as you need!

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.


I hope you found this guide useful! And if you need any further assistance, I am available for freelance work or one hour consulting for both Bubble development and Buildship :slight_smile:

You can check my portfolio here

7 Likes

Great guide!

1 Like

Thanks, George!
I have been exploring Buildship more and it’s a great add-on to Bubble!

Great guide and great write-up! Thank you for this and for sharing it with the community.

I know this guide is centered around connecting an Assistant and not around endpoint security, but i feel its still important to put a note that the endpoints need to be secured somehow. Creating a public endpoint that can run without authentication and also ignores privacy rules is a big vulnerability, especially if they create/return data.

You don’t need to talk about how to actually secure the endpoint - just leave a short disclaimer that something needs to be done but its outside the scope of the guide :blush:

3 Likes

Hey @nico.dicagno , true!
Thanks for noticing it.
I added a disclaimer now :saluting_face:

1 Like

Hi, following up on this thread. I am hoping to set this up. Two questions:

  1. The previous posts mention endpoint security for this. How is that achieved? I know how to do security and privacy rules in Bubble, but are extra steps needed when using Buildship?

  2. Is there a guide for web scraping through Buildship? When users sign up, I want them to be able to pull existing data from their own website(s) or from LinkedIn. Preferably, with real time updates when their website(s) are updated.

Thanks!
Bryan

I don’t see how a LLM could be the best solution for this? There are online specialized services returning this data.

1 Like