Announcing Hasura Notebook: Prototype fast on your GenAI apps
Tags: notebook, generativeai, largelanguagemodels, ai
Published: 2023-09-05 09:24:10

---
title: Announcing Hasura Notebook: Prototype fast on your GenAI apps
published: true
date: 2023-08-31 15:24:53 UTC
tags: Notebook,GenerativeAI,LargeLanguageModels,AI
canonical_url: https://hasura.io/blog/announcing-hasura-notebook-prototype-fast-on-your-genai-apps/
---

 ![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://hasura.io/blog/content/images/2023/08/notebook-og-1.png)

**Hasura empowers you to rapidly create high-quality data APIs** for production purposes. As data sources continue to evolve, Hasura evolves as well, providing you with the capability to construct secure data APIs over versatile data stores, including vectorized data.

If you are new to Hasura, we recommend [getting started for free with Hasura Cloud](https://hasura.io/docs/latest/getting-started/overview/).  
  
As part of Hasura’s evolution, today we’re introducing Hasura Notebooks, a tool designed to facilitate the swift prototyping of cutting-edge GenAI applications.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://hasura.io/blog/content/images/2023/08/notebook-og.png)

In this blog, we’ll take you through this new tool to gain a comprehensive understanding of how it works and why you need it.🙂

## GenAI revolution

Large Language Models (LLMs) are very large deep neural networks trained on vast amounts of text data mostly scraped from the internet.

LLMs have taken the world by storm because these generalized language models are so good at understanding context, retrieving information, and generating content that can help us with numerous applications. And why not? Automation is the key to efficient applications.

At its core, LLMs learn the word probabilities to predict the next most suitable word given in a sentence. If the sentence is from a domain which the LLM hasn’t been exposed in the training period, then the LLM will make low confidence predictions, which are known as hallucinations.We can deal with this problem by providing LLM context in prompts and instructing it to use the context to complete the task. This process is called grounding.

In order to maximize the potential of LLMs across a wide range of applications, it's essential to expand the scope of secure data sources. Given the fast-paced environment, it's equally important to avoid dedicating excessive time to constructing data APIs (a crucial yet somewhat mundane task 🤷‍♀️) when you could be focused on creating exciting and lucrative applications.

Hasura to the rescue!

Let’s learn how you can  quickly build secure data APIs and prototypes with a product search demo.

## What is Hasura Notebook

Hasura Notebook is a remote Jupyter Notebook with ready-to-run examples and Jupyter Kernel Gateway baked-in to turn your code cells into endpoints.

Hasura Notebooks are great if you want to quickly prototype GenAI application with Hasura or learn GenAI from our existing templated projects.

We are going to building on Hasura Notebook in this blog. Recommended reading before we proceed - [Hasura Jupyter Python Notebook & API Server documentation](https://hasura.io/docs/latest/jupyter-notebooks/).

## Use case: Get a contextual product search powered by OpenAI in under 10 minutes 🚀

As you might have experienced yourself, searches on most e-commerce websites are keyword-oriented. This results in a lot of false positives or irrelevant results and can lead to dissatisfied users. Contextual search is an answer to this problem. Contextual search matches the user query with the product description.

## Prerequisites

**1. Hasura CLI**

If this is your first time using Hasura CLI, you will need to install it. Follow the installation instructions [in this doc](https://hasura.io/docs/latest/hasura-cli/install-hasura-cli/%20).

**2. Hasura Notebook**

Follow the steps in the documentation below to instantiate your Hasura Notebook.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://hasura.io/blog/content/images/2023/08/hasura-notebook-architecture.png)
_Architecture of Hasura + Jupyter Notebook + VectorDB_

You will know you are ready when you are able to access the [Hasura Notebook](https://hasura.io/docs/latest/jupyter-notebooks/) with a landing page that looks like this.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/hkxH2LThGp34LxaNGhI2YBluzlbIdlnS-sFvD1Xq3HbdFKy7U-DFfXiJzW7pK0wp_EOSQGCPk6CwEiNJ6p_VKi4zx4t1OQdCU-Ngs9Cmjq8WV474bU49LZxVwk0XbA3TaiS_r2G8khPJNxv9jPRgWrE)

You can find all the code under `https://<connector url>/jupyter/notebook/product_search`.

Let’s get building! 🚀

## Setup your PostgreSQL database

**Step 1:** Start a new Neon Cloud PostgreSQL DB or link your existing DB.

More details on Neon Cloud PostgreSQL DB here: [https://hasura.io/docs/latest/databases/postgres/neon/](https://hasura.io/docs/latest/databases/postgres/neon/)

**Step 2:** Create a new table called `base_products` and track it from the data tab.   
Tracking enables the table to be accessible through GraphQL query.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh6.googleusercontent.com/XNYDiJUZxRIFbC7AYmI6EbSK8WQypFYh1NYRjzIGf2sagdDsuFu5toByV0FtB-D8d1g0Tp2wJuvlNWvWOgIUSW9OFYkp__WyWpLWLuTZeCBTRF2UlJEWPYhF99_Uv7LtFWRv8lwbf-GRYruAVaTjBrI)

## Set up your vector database

**Step 1:** Create a free 14-day cluster on Weaviate.

Head to [https://console.weaviate.cloud/](https://console.weaviate.cloud/) and register for an account. After confirming via email, click + Create cluster and fill in a name before clicking Create. Once Weaviate has provisioned your sandbox cluster, proceed to the next step.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh6.googleusercontent.com/Od-Xl1LrXjq0HsHJyu8m1j0z7ksyLZ8FsRZcSuf-qjUZwWRNuBPGC4wiH2c1RMe14wFakxRGRC6EjsbjWqCjsQXGPObL-VKFqgCR4ZQyRbn08aXEC95c9P65eFHjmlB32aUe4dWiOSIt08S-2uamcDA)

**Step 2:** Create the Product schema called `Product_vectors`.

Product schema ID, Product Name, and Product Description. To execute this, run setup\_weaviate.ipynb

**Step 3:** Add the Weaviate table to Hasura.

Currently, the Weaviate connector is not natively available in Hasura. You can add it to Hasura in two simple steps using the Hasura CLI.

1. Deploy Weaviate connector using Hasura CLI.

```
# create connector using our weaviate repo
hasura connector create my_weaviate_connector:v1 --github-repo-url https://github.com/hasura/weaviate_gdc/tree/main/
# check deployment status to get the endpoint

```

```
hasura connector status my_weaviate_connector:v1
# you can also use list command
hasura connector list
# view logs at any point of time
hasura connector logs my_weaviate_connector:v1
# for more commands explore help section

```

2. Add the Weaviate connector to Hasura.

Refer to this document for details [https://hasura.io/docs/latest/databases/vector-databases/weaviate/](https://hasura.io/docs/latest/databases/vector-databases/weaviate/)

**Step 4:** Track the table.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/qEfFxoTlij4_WW16hLTrUqfjBHMzrEEgVyYuGvNt-L5A3Iu4YLc1ttBovPVFDMGNwy4BsOaMnBdnIi6rpTV_5JukgsrJ05kaXffYR0VwlUhCUPJr-HHFptEBIAFjQ1BqpIvhL1mRalbOnLsA3xZz0mg)

## Add a remote relationship between PostgreSQL and Weaviate tables

Go to Product\_vectors table and add a relationship with the PostgreSQL database as shown below.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/0q1HnJZDPKjbuL2j0PJdd1yS_P0gkL-9QdQzQhdRZKo7LNPDGOnY6udfM0bUewwks60vKwRIcDiXI9AU2pFevXg_oTdZbh6p_499D0OusLf6omqQpdHOjDmAgudiWckjFXOxCA9D3vyDktEnFeRpTSo)

## Set up an Event Trigger on a PostgreSQL table  

**Step 1:** Fetch your Hasura GraphQL API endpoint and Admin secret.  
Move over to `cloud.hasura.io` and click `Projects`. From your project, click on the `Settings` icon to access Project details.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/zcoADxSrY7nEQOlpp1rbsZ9LATxuAQvYUVcYWxqiIoiyfLGCYDTlJHA1lAoO8_XrBPvIk1goprLXpHAdLpFDycY6Ht89Gvxh8xZ0Q9VyTzCxXQDBloYGdc4vvjY6tTfgPBpMJCd6y6UunbDYRSUm2ZE)

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh6.googleusercontent.com/Or4p8Hr_3-hl0kcU7OmRnKVGCs5uBrzz-Ow9I5HnzhqzOdApHnPksZHZK-iI4MIOjmsZ2EqfO-Ab6uvvFr85vifSVx4HLT2jAwuAMsZoQJnaxHPYrU0xkP5zva64hmaHcF75D_2mizBFe5gjkiZYdXA)

**Step 2:** Update Hasura GraphQL details in the notebook under `Template for event trigger to ETL data`.

Open server.ipynb and you can see there are multiple functions with cells commented with `# POST /handle_event` These are the functions that are available on the endpoint `/handle_event`.

Update the details in the cell, and you are ready to go.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/Dr0GsL01NJsJ3Ww9Ai5jeHt0tAzzn7fYrg5BVNYPWdVMXRT0VWGGwnxQYMTWb6rbsN17AN_56z9Ib_M9GHOsQpw76XkUc5j9K028tbTBoJKGJ1reFwY9_gioF0tD2onHN_6H7gaznt3sCDw-w1Q0cGQ)

Don’t forget to restart the Jupyter gateway server.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh4.googleusercontent.com/WGPJvwoe5tB7iR-AnewW7lDj6nnBvCTMHkaYX82rn2Cnn7BJuQfTVDhnlG84nR60QU6QA6orgPhjFwepca4Vrz6DevJ9OWv1I1vCrI-s_Bsjr_52Y0s16aTRAyMl2aqehAhFMNVXo1Nef3pAb-ijYyU)

**Step 3:** Add the Event Trigger in Hasura.

Head back to the Hasura Console and click on the Event tab to create a new Event. Fill in the required details.

Your webhook URL is your notebook URL (ends with .app) + `invoke/handle_event`.

You can also fetch your webhook URL by running this CLI command again `hasura notebook status`.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh3.googleusercontent.com/AeHEoSl4EUI-cOlTJxz45GgwhL0oaSrdvcdeHcItxKScO9fi4B6CPZDmPoeZ7FU-qI1gEmifVssf5N5DVslQdnsDNx7ENjTtcmb2R0dCbjPjzd5e4AH4e6E9apN7iN0baTTj4iXo1zsfJbbO2Yw9Cio)

Add the authorization header required to reach the endpoint from the Hasura notebook.

Generate base64 encoded value. You will need to execute this on terminal of your choice.


```
echo -n "<username>:<password>" | base64

```

You can fetch the username and password by executing the following command


```
hasura notebook status

```

```
Key = Authorization
Value = Basic <base64 encoded value here>

```

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh6.googleusercontent.com/2JDANht9egMaeIIE6wKOszt295vKCRTH7E1OXYAUZXx-ME3aDnlpwdzQvYzmRAanuMtMsifJbnLXykOBvMCwswz1VA11r5LNJwddS4uQb2ZMY7byKZcW8KTjg_brp2GcB0RK4R5yk0tph0AEjId_Acw)

## Insert data into PostgreSQL and watch Vector DB update automatically

Now that we have integrated Weaviate with Hasura, we can use a mutation in Hasura to insert data into our Postgres table _and_ auto-vectorize and update our vector DB! You can do this by executing `insert_data.ipynb`.

Voila! We have vectors with product name and description fields.

## Secure semantic search using Hasura

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/r1sNNdB8tor43WVJXQSgfC5mD2hC6-DiCJgBDmM1fEXIlUWweHeMuMKl1BGM0DYwiS2ih9hix62IFD2z6v_fXQswr5cAU33aRcerzjTlEzeDr6zWU5_N1Ev9Pmx00H_MWmYfBAjr-C-A8SEEjgssydA)

For more on building secure APIs with Hasura, refer to this overview: [https://hasura.io/docs/latest/auth/overview/](https://hasura.io/docs/latest/auth/overview/)

Let us now go one step further, with complex query powered by LLM.

## LLM-powered product search

**Step 1:** Update Hasura GraphQL and OpenAI details in the notebook under `Template for event trigger to ETL data`.  
  
Update the details, and you are ready to go.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh6.googleusercontent.com/0l14DqkMegvmx9_ylBSsXowc26qVmrS6XBz5EDMwArU_cvcvrIjh1xQjoasdFYQLRiN-T-BJ-XvZCbGzxIw7Nwa6kdXz-uYy_SP8COIDg2SwvKmRpk9yUDETm1Q7Uqf8cmPejEOFN0zoxVqZ4X9UN6g)

After updating any of these values, it’s important to restart your notebook. You can do this from the gateway generated earlier using the CLI.

**Step 2:** Create LLM prompt

This step is available as a template for you. Feel free to tweak the prompt and play with it.

**Step 3:** Create Hasura Action

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh5.googleusercontent.com/Sw5Nj3QQ7lihaQwHdfqjsUQn5WfuAmC20fuWAlwK_tvMRscwASg6B6hxgBZNM0YxV-S2MywJVpONypAIjW37wZiSn86FKyB9bzxoj1oGRq4BxN9Bh2x-GsZW-X7c_J3geVNTEfIKudvexkwveyfoUpw)

Like before, you will need to add Authorization headers.

**Step 4:** What you just created is a secure API on your LLM query. Integrate the API with your app or just play around on the Hasura Console. You now have the power of the Hasura by your side.

![Announcing Hasura Notebook: Prototype fast on your GenAI apps](https://lh6.googleusercontent.com/nps1HPaBz5uEhc-J2O53oVuIaBK3bnk36oETfeqDeiQ_1MF5B1XlOjJsyCB6bKbVC3GuAxu74rOgbIsG7YSq-jaTy9nEpRxPvzzHhNDxbAaupTprav6INWmDxhPWyM2z3Co_XdB_YHdDSTp3SLFzpdg)

## Conclusion

By seamlessly deploying a Jupyter Notebook through the Hasura CLI, you've unlocked a world of possibilities. The ability to automatically vectorize relational data has not only accelerated your data manipulation processes but has also paved the way for more efficient and effective analyses.   
  
Moreover, the integration of robust security measures into your LLM queries ensures that your data remains protected at all times. This remarkable journey, powered by Hasura, empowers you to harness the true potential of your data, all while simplifying the intricate processes involved!

With Hasura by your side, what will **you** build?