Let's Build a Full-Stack AI Application in React!
Tags: react, javascript, tutorial
Published: 2023-11-04 05:49:16

In the realm of real-time AI application development, few tools offer as much power and flexibility as the Elegance SDK. Pre-configured to harmoniously work with SingleStore, this SDK provides developers with a treasure trove of tools to craft impeccable full-stack applications infused with AI capabilities. In this article, we'll journey through the world of the Elegance SDK, and explore how to build a full-stack AI application using React.

## What is the Elegance SDK?

![Elegance SDK](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dekp4y1albluvo8pjbf7.png)

The Elegance SDK is a pioneering toolkit designed to facilitate the swift construction of real-time AI applications. Its robust integrations with SingleStoreDB streamline the process, providing developers with a pre-configured environment ready for efficient application development. Notably, the Elegance SDK is a full-stack JavaScript toolkit, implying its capabilities stretch from back-end operations, using Node.js, to front-end endeavors — particularly with React.js.

**Key features**:
- **Pre-configured SingleStoreDB connection**: Say goodbye to the often tedious process of setting up database connections. Elegance SDK provides a seamless connection experience with SingleStoreDB, ensuring your data flows without a hitch.

- **AI integrations**: Harness the power of AI with minimal hassle. From chat completions to content suggestions, the SDK offers tools that tap into the OpenAI API, imbuing your applications with intelligent functionalities.

- **Support for multiple databases**: Beyond SingleStoreDB, the SDK is versatile, offering support for MySQL and [MongoDB using SingleStore Kai™](https://www.singlestore.com/spaces/mongo-atlas-single-store-kai/).

- **File embeddings generation**: An intriguing feature, this allows developers to convert various file formats into actionable insights, paving the way for enhanced data analysis.

- **Full-stack tooling**: With ready-to-use Node.js controllers for the back end and React.js hooks for the front end, the SDK ensures a holistic development experience.

## Tutorial: Let’s build a full stack AI app
**Let’s see the magic of Elegance SDK **
### Pre-requisites
- Create your [SingleStoreDB Cloud](https://www.singlestore.com/cloud-trial/?utm_campaign=singlestore-for-ai&utm_medium=referral&utm_source=pavan&utm_term=elegancesdk) account for free

- Install [GitHub CLI](https://github.com/cli/cli#installation). This is needed to clone our sample repo for the tutorial

- Download Node.js and npm from the official [Node.js website](https://nodejs.org/en) [npm will be installed automatically]

Sign up for a free SingleStoreDB Cloud trial — and get  $600 in credits.

![SingleStoreDB Cloud](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11viv14nbcr8lv74a9ow.png)

The next step is to create a workspace. I have created my workspace named ‘elegancesdk’.

You can see from the following screenshot that, by default, you will have some sample databases created. 
![database connect](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z7fbqz808dhynirnus5x.png)

Let’s create a new database named ‘books_chat_mysql’ under ‘elegancesdk’ workspace as suggested in this step by step [GitHub repo](https://github.com/singlestore-labs/elegance-sdk-app-books-chat/tree/main). 

![create database](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jg19u2w64zq81qwxutj.png)

You can see our new database under ‘elegancesdk’. 

![db created](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ui3useacmbbgga30qbi.png)

Now, let’s clone this repository and follow the steps shared in the README.md file. 

Open your favorite terminal and clone the repo using the following command:

```
git clone https://github.com/singlestore-labs/elegance-sdk-app-books-chat.git
```

The code and all the components get downloaded in your local machine. 

![code in local](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr44y5nts6wcpu00ry7b.png)

The first thing after cloning the code to your local machine is to set the environment variables specified in the `.env.sample` file. 

Create a new `.env` file and add these environment variables.

![env sample](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yj2t3ujixpqj34n689c6.png)

Set the NEXT_PUBLIC_CONNECTION_TYPE as mysql or kai. I am setting it up as mysql.

Get the DB_PASSWORD from ‘Access’ tab

You should get an idea of how to go to the ‘Access’ tab from the below screenshot. It is present on your SingleStore dashboard. 

![access tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3g6yx40qvadusbe2296.png)

Click on the ‘Access’ tab and you should see the username and password.

![password of db](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fcbf5dg7ujt7ibfp5mhe.png)

MYSQL_USER is set to ‘admin’

Get MYSQL_HOST from connecting directly to your workspace, as shown here.

![connect to workspace](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z88djisuzknihmdpxr3b.png)

Go to the ‘Your App’ tab and select ‘Node.js’. You should see the ‘Host’ parameter; copy it and use it in your environment variable.

![app tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpko3a7b7f6wpi7etu6t.png)

Get the MYSQL_URI from the ‘SQL IDE’ tab.
![sql url](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqzwwp7h0h5oeehaexx1.png)

Next, set the last environment variable ‘OPEN_API_KEY’ and we are done. Save the file.

Next, get your ‘OPEN_API_KEY’. If not, take a look at the following instructions.

If you are already logged in into your [OpenAI](https://platform.openai.com/overview) account, you can find your Secret API key in your [User settings](https://beta.openai.com/account/api-keys). 

![API Keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3547kmt0i501ttfupebd.png)

Make sure to install the dependencies using the following command.

```
npm i 
```

Then, use the below command to run the application.

```
sh ./scripts/start.sh
```

![next build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kucjo6c641x9gv8653hi.png)

![running build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trp7uit0qn9rh9cwkfmw.png)
You can see the application running on port 3000 as specified in the preceding screenshot.

![sdk image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2i3rbsp139ybs51zo3h3.png)

You can then interact with your application. For example, by modifying the ratings, and entering a message as shown here.

![reviews](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxu8wjhe5vizh3nwj5yt.png)

![journey book](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prp7rt1fj7kfjbycssgr.png)

![book chat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sncffs1efu7iq8oqi8cy.png)

You can go back to your database list and within it(i,e ‘books_chat_mysql’) you can see the tables and embeddings. 

![db table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp6s7ts0qtv18l6wjdhx.png)

Click on the first item from the list ‘a_journey_to_the_centre_of_the_e’ and you should see the embedding. 

![embeddings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jos69cfgya5x8q4tu178.png)

If you want to go a little deeper and understand how the embeddings look, click on ‘Sample Data’ to see the embeddings. 

![embeddings vector](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0i2e6483mrtdzc286kkk.png)

### Customize and build your own app
The beauty, or should we say the elegance, of the Elegance SDK is how easy it is to build your own application on top of it. In the following section, I will walk you through building your own app on top of the Elegance SDK.

Follow the steps shared in [this repository](https://github.com/singlestore-labs/elegance-sdk-template-next).

Make sure your database is configured before moving forward, using the tutorial to guide you.

Start with the following command 

```
npx create-singlestoredb-app --template elegance-next
```

![react app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t30sbit7s0mcg6y7rbqb.png)

The base Elegance SDK template is loaded into the folder you are in.

![elegance sdk image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxzabrdtxag9e5llp0az.png)

Set the environment variables, using the tutorial shared.

![env variables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kn33aayek6ou7iv58xq5.png)

Install all the required dependencies using the following command.

```
npm i
```

Once the dependencies are installed, let’s build the application using the following command.

```
npm run build
```

Now, let’s run the application using the following command.

```
npm run dev
```

![app running in 3001](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eu7s64vsrx9gibalo0xy.png)

Once you do that, you should see your application running.

![react app using singlestore](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1l47ivof94yyslo4oru5.png)

You can change the look and application per your requirements by modifying the code to meet your application’s needs. 

You can learn more about the [SingleStore Elegance SDK here](https://www.npmjs.com/package/@singlestore/elegance-sdk). Also, take a look at this detailed [webinar on SingleStore Elegance SDK from our engineers](https://www.singlestore.com/resources/webinar-how-to-build-a-full-stack-ai-app-in-react-with-elegance-sdk/).

### Conclusion:
The future of application development is real time and AI driven. With tools like the Elegance SDK, developers are equipped with the resources they need to usher in this future. By combining the raw power of SingleStoreDB with the intelligent capabilities of AI, the SDK provides a platform for creating revolutionary applications. So, dive in, explore its myriad features and craft your next groundbreaking full-stack AI app with React.