Build AI-Native B2B application with Postgres, NodeJS and React
In this tutorial, you will learn to build a multi-tenant AI-native todo list application, using Nile with NodeJS, React and OpenAI’s client. We’ll use Nile to provide us with virtual-tenant databases - isolating the tasks for each tenant, and we’ll use the AI models to generate automated time estimates for each task in the todo list. The estimates will be based on the task title, and estimates of similar tasks in the tenant’s history. This technique is known as RAG architecture.
Create a database
- Sign up for an invite to Nile if you don’t have one already
- You should see a welcome message. Click on “Lets get started”
- Give your workspace and database names, or you can accept the default auto-generated names. In order to complete this quickstart in a browser, make sure you select to “Use Token in Browser”.
Create a table
After you created a database, you will land in Nile’s query editor. For our todo list application, we’ll need tables to store tenants, users and todos. Tenants and users already exists in Nile, they are built-in tables. We’ll just need to create a table for todos.
You will see the new table in the panel on the left side of the screen, and you can expand it to view the columns.
The embedding column is a vector representation of the task. When the user adds new tasks, we will use these embeddings to find semantically related tasks and use this as a basis of our AI-driven time estimates. This technique - looking up related data using embeddings and using this data with text generation models is a key part of RAG (Retrieval Augumented Generation).
See the tenant_id
column? By specifying this column, You are making the table tenant aware. The rows in it will belong to specific tenants. If you leave it out, the table is considered shared, more on this later.
Get credentials
In the left-hand menu, click on “Settings” and then select “Credentials”. Generate credentials and keep them somewhere safe. These give you access to the database.
Get third party credentials
This example uses AI chat and embedding models to generate automated time estimates for each task in the todo list. In order to use this functionality, you will need access to models from a vendor with OpenAI compatible APIs. Make sure you have an API key, API base URL and the names of the models you’ll want to use.
Set the environment
Enough GUI for now. Let’s get to some code.
If you haven’t cloned this repository yet, now will be an excellent time to do so.
Rename .env.example
to .env
Update NILE_USER and NILE_PASSWORD with the credentials you picked up in the previous step. It should look something like this:
Install dependencies
Run the application
Start both NodeJS api server and the React frontend
Go to http://localhost:3000 in a browser to see the app.
You can try a few things in the app:
- Sign up as a new user
- Create a tenant
- Create a todo task and see its time estimate. If you create more tasks, the estimates for new tasks will use the embeddings of the existing tasks to generate the estimates.
Check the data in Nile
Go back to the Nile query editor and see the data you created from the app.
You should see all the todos you created, and the tenants they belong to.
What's next?
This example is a good starting point for building your own application with Nile.
You have learned basic Nile concepts and how to use them with NodeJS and React.
You can learn more about Nile’s tenant virtualization features in the following tutorials:
You can explore Nile’s JS SDK in the SDK reference.
You can learn More about AI in Nile, or try a more advanced example like:
How does it work?
The interesting part of this example is the NodeJS server. Lets take a look at /examples/quickstart/node_react/src/be/app.ts
.
Using AI models to generate estimates
This example uses AI chat and embedding models to generate automated time estimates for each task in the todo list. We handle the time estimates in
the route handler for app.post("/api/tenants/:tenantId/todos"
. This handler executes when users add new tasks.
This is what the handler code looks like:
As you can see, we look up similar tasks and then use the AI model to generate the estimate. We then store the task, with the estimate and the task embedding in the database.
The stored embedding will be used to find similar tasks in the future. The methods findSimilarTasks
, aiEstimate
and embedTask
are all defined in AiUtils.ts
.
They are wrappers around standard AI model calls and database queries, and they handle the specifics of the AI model we are using.
This will make it easy to switch models in the future.
Getting similar tasks is done by querying the database for tasks with similar embeddings.
We started by generating an embedding with SEARCH_QUERY
task type. This is because we are looking for similar tasks to the new task. We use an embedding model
from the nomic
family, which is trained to perform specific types of embedding tasks. Telling it that we are generating the embedding for a lookup vs
generating an embedding that we will store with the document (as we’ll do in a bit), should help the model produce more relevant results.
As you can see, we filter out results where the cosine distance is higher than 1. The lower the cosine distance is, the more similar the tasks are (0 indicate that they are identical). A cosine distance of 1 means that the vectors are essentially unrelated, and when cosine distance is closer to 2, it indicates that the vectors are semantically opposites.
The embedTask
function uses the embedding model to generate the embedding and is a very simple wrapper on the model:
Now that we have the similar tasks, the handler calls aiEstimate
to generate the time estimate.
This function also wraps a model, this time a conversation model rather than an embedding model. And it icludes the similar tasks in the promopt, so the model will
generate similar estimates:
This estimate is then stored in the database along with the task and its vector embedding.
Working with virtual tenant databases
The NodeJS server uses the Nile JS client to connect to Nile.
When the Nile client is initialized, it uses the credentials you provided in the .env
file to connect with the API:
The application uses Express middleware to capture the tenant identity for the current request and set Nile context:
We use Nile SDK to both execute SQL and make API calls to Nile. For example, to create as new tenant:
Was this page helpful?