Build a recommender Full stack App using OpenAI and Azure SQL: Step-by-Step Guide

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.

Introduction

Vector databases are gaining quite a lot of interest lately. Using text embeddings which is a bunch of numbers representing that text and vector operations makes it extremely easy to find similar "things". Things can be articles, photos, products…everything. As one can easily imagine, this ability is great for implementing suggestions in applications easily. The applications are many from providing suggestions on similar articles or other products that may be of interest to quickly finding and grouping similar items.

 

Scenario

Imagine you want to build a cool recommendation system that uses LLMs to analyze the user's input and recommends the most similar items from your database. How can you do such a thing and give your users valuable recommendations? You may use Azure OpenAI to create a tool that can help people find the most relevant things for their interests. Before you do that you'll need to convert the items into embeddings using Azure OpenAI's natural language processing capabilities. Then, use vector search to compare the embeddings with a query topic and rank the items by similarity.

You want to quickly and easily discover the items that match your goals and preferences, without having to read all that data manually using simple, scalable, and fast technologies.

 

What will you learn?

In this blog, you'll learn to:

  • Fork a GitHub repository.
  • Create an Azure OpenAI cloud resource.
  • Create a deployment for an embedding LLM.
  • Create an Azure SQL server and database cloud resource.
  • Create database user credentials using T-SQL in the Azure SQL database Query editor.
  • Clone the repository locally.
  • Execute a SQL script using sqlcmd locally to set up your database.
  • Create an Azure function cloud resource.
  • Configure Azure function Application settings.
  • Configure a GitHub workflow to run successfully.
  • Create an Azure Static Web App cloud resource.
  • Configure Database connection for the Azure Static Web App.
  • Insert data into the database using the Azure database Query editor.

 

What is the main objective?

Build a recommender full stack application using OpenAI and Azure SQL.

session-recommender-architecture.png

 

Prerequisites

 

Summary of the steps:

Step 1: Fork the sample Repository on GitHub

Step 2: Open the Azure portal and create a new Resource group

Step 3: Create an Azure OpenAI resource and Deploy an embedding Model

Step 4: Create an Azure SQL server and database

Step 5: Create an Azure Function App

Step 6: Create an Azure Static Web App

 

Step 1: Fork the sample Repository on GitHub

GitHub is a cloud platform for hosting and sharing Git Projects. It enables collaboration between developers online.

 

In this step, you create a copy from the source code on your GitHub account to be able to edit it and use it later.

 

Complete the following steps to fork a copy from the sample repository on GitHub:

1. Visit the sample github.com/Azure-Samples/azure-sql-db-session-recommender in your browser and sign in.

 

2. Select Fork from the top of the sample page.

fork-sample.png

 

3. Select an owner for the fork then, select Create fork.
select-create-fork.png

Congratulations! You successfully created a fork. In the next step, you'll create a resource group to logically group your resources.

fork-screenshot.png

 

Step 2: Open the Azure portal and create a new Resource group

A resource group is a container that holds related resources for an Azure solution. The resource group can include all the resources for the solution or only those resources that you want to manage as a group.

 

In this step, you'll:

  • Open the Azure Portal.
  • Create a new Resource group in the Azure Portal.

 

Open the Azure Portal

 

1. Visit the Azure Portal https://portal.azure.com in your browser and sign in.

JohnAziz_0-1702054644528.png

 

Now you are inside the Azure portal!

JohnAziz_1-1702054645081.png

 

Create a new Resource group in the Azure Portal

In this step, you create a resource group that enables you to group your cloud resources.

 

Complete the following steps to provision a Resource group in the Azure portal:

1. Type resource group in the search bar at the top of the portal page and select Resource groups from the available options.

JohnAziz_2-1702054644737.png

 

2. Select Create from the toolbar to create a new resource group.

JohnAziz_3-1702054644789.png

 

3. Add the following information to create a resource:

What Value
Subscription Use the same subscription you used to apply for Azure OpenAI access.
Name Enter a unique name.
Region Select a region close to you for best response times. For example, Select East US. Note that Azure OpenAI is not available across all regions, you can check the regional availability here.

 

JohnAziz_4-1702054645078.png
4. Select Review + create to start the validation process.
5. Confirm your configuration settings and select Create.

 

6. Select the name of the resource group to open it. Here, you can create new resources and manage existing ones.

select-rg-name.png

Congratulations! You successfully created a resource group that enables you to create a logical grouping in the cloud for your resources. In the next step, you'll create an Azure OpenAI resource to be able to interact with different large language models.

 

Step 3: Create an Azure OpenAI resource and Deploy an embedding Model

Azure OpenAI Service provides REST API access to OpenAI's powerful language models including the GPT-4, GPT-35-Turbo, and Embeddings model series.

 

In this step, you'll:

  • Create an Azure OpenAI resource in the Azure portal.
  • Deploy an embedding Model in Azure OpenAI studio.

 

Create an Azure OpenAI resource in the Azure portal

In this step, you create an Azure OpenAI Service resource that enables you to interact with different large language models (LLMs).

 

Complete the following steps to provision an Azure OpenAI resource in the Azure portal:

1. Inside your resource group, select Create.

select-create-rg.png

 

2. Type openai in the search bar of the Marketplace page and select Create Azure OpenAI from the options that appear.

JohnAziz_1-1702493811238.png

 

3. Add the following information to create a resource:

What Value
Subscription Use the same subscription you used to apply for Azure OpenAI access.
Resource group Use the resource group you created in the previous step.
Region Select a region close to you for best response times. For example, Select East US.
Name The name must be globally unique.
Pricing tier
Currently, S0 is the only available tier.

 

create-openai-resource.png

 

5. Now that the basic information is added, select Next to confirm your details and proceed to the next page.

6. Select Next to confirm your network details.

7. Select Next to confirm your tag details.

 

8. Confirm your configuration settings and select Create to start provisioning the resource. Wait for the deployment to finish.

 

9. After the deployment finishes, select Go to resource to inspect your created resource. Here, you can manage your resource and find important information like the endpoint URL and API keys.

select-go-to-resource-openai.png

 

Congratulations! You successfully provisioned an Azure OpenAI resource that enables you to interact with OpenAI models. In the next step, you'll deploy an embedding model that creates the vectors from the data.

 

Deploy an embedding Model in Azure OpenAI studio

In this step, you create an Azure OpenAI embedding model deployment. Creating a deployment on your previously provisioned resource allows you to generate text embeddings (i.e. numerical representation for text).

 

Complete the following steps to deploy an embedding model in the Azure OpenAI studio:

1. Select Go to Azure OpenAI Studio from the toolbar to open the studio.

select-go-to-openai-studio.png

 

2. Select Create new deployment to go to the deployments tab.

JohnAziz_5-1702493811006.png

 

3. Select + Create new deployment from the toolbar. A Deploy model window opens.

JohnAziz_6-1702493811009.png

 

4. Add the following information to create a resource:

What Value
Select a model Select text-embedding-ada-002.
Model version Select 2.
Deployment name Add a name that's unique for this cloud instance. For example, embedding-model because this model type is optimized for creating embeddings.

 

new-deployment.png

 

5. Select Create.

 

Congratulations! You successfully created a model deployment that enables you to create embeddings. In the next step, you'll provision your Azure SQL server and database that enables you to store and retrieve your data.

 

Step 4: Create an Azure SQL server and database

Azure SQL is a family of managed, secure, and intelligent products that use the SQL Server database engine in the Azure cloud.

 

In this step, you'll:

  • Create an Azure SQL server and database in the Azure portal.
  • Create database user credentials using the Azure SQL Database Query editor.
  • Clone the sample code locally.
  • Execute a SQL script using sqlcmd locally to set up the database.

 

Create an Azure SQL server and database in the Azure portal

In this step, you create an Azure SQL server with both Microsoft Entra and SQL authentication then, you create a database with public access enabled which allows you to access it.

 

Complete the following steps to create an Azure SQL server and database in the Azure portal:

1. Inside your resource group, select Create.

select-create-rg.png

 

2. Type sql database in the search bar of the Marketplace page and select Create SQL Database from the options that appear.

search-sql-database.png

 

3. Perform the following tasks:

What Value
Subscription Use the same subscription you used to apply for Azure OpenAI access.
Resource group Use the same resource group you created before.
Database name Enter a unique name for your database. For example, sqldb.
Server Select Create new to create a new SQL server. Further details are provided below.
Want to use SQL elastic pool? Select No. This option configures a scalable cost-effective pool that manages multiple databases with varying and unpredictable usage demands.
Workload environment Select Development. This option configures a cheaper compute and storage for you during the "development" phase. Once you want to move to production you can scale it up.
Compute + storage Select General Purpose - Serverless. This option uses a cheap serverless compute that has a one-hour auto-pause delay. The auto-pause delay option defines the period of time the database must be inactive before it is automatically paused. 
Backup storage redundancy Select Locally-redundant backup storage. This option incurs less cost and is appropriate for pre-production environments that do not require the redundance of zone- or geo-replicated storage.

 

48dd4352-54ff-4c9c-a7d4-5741957a85ff.png

 

4. In the Create SQL Database Server, perform the following tasks to create a server:

What Value
Server name Enter a globally unique name.
Location Select a region close to you for best response times. For example, Select East US.
Authentication method Select Use both SQL and Microsoft Entra authentication. This option enables you to log into your database using your email and normal SQL authentication with a username and a password.
Set Microsoft Entra admin Select Set admin, search for your email, select your email and confirm by selecting the Select button.
Server admin login Enter a username for your database. For example, enter sqlAdmin.
Password Enter a strong password and save it in a secure place.
Confirm password Enter the same password to confirm it.

 

create-sql-db-server.png

 

5. Select OK to confirm your selections and text entries.

 

6. Select Next : Networking > to move to the networking configuration parameters.

 

7. In the Networking section, perform the following tasks to configure the server's network:

What Comment
Connectivity method Select Public endpoint. A private endpoint will only work inside a virtual network.
Allow Azure services and resources to access this server
Select Yes. This enables other cloud resources to access this resource.
Add current IP address Select Yes. This adds your IP address to the firewall allowed list.
Connection policy Select Default - Uses Redirect policy for all client connections originating inside of Azure (except Private Endpoint connections) and Proxy for all client connections originating outside Azure.
Minimum TLS version Select TLS 1.2.

 

select-review-create-sql-db.png

 

8. Select Review + create.

 

9. Confirm your configuration settings and select Create to start provisioning the resource. Wait for the deployment to finish.

 

10. After the deployment finishes, select Go to resource to inspect your created resource. Here, you can manage your resource and find important information like the connection strings for your database.

select-go-to-resource-sql-db.png

 

Congratulations! You successfully created an Azure SQL server and database. In the next step, you will create a database user to use it in allowing access from other cloud resources.

 

Create database user credentials using the Azure SQL Database Query editor

In this step, you create a database user in the Query editor to use it in allowing access from other cloud resources.

 

Complete the following steps to create a new database username and password in the Azure SQL database Query editor:

1. Inside your database, select Query editor from the left-side navigation menu.

select-query-editor.png

 

2. Select your preferred authentication method to log into the database and execute SQL commands.authenticate-query-editor.png

 

3. In the query editor, execute the following command to make sure the user doesn't exit.

drop user if exists "appUser"
go
 
4. Copy and paste the command in the query editor and select Run.
drop-user-if-exists.png

 

5. In the query editor, execute the following command to create a new user and password. Make sure to change the values and save them in a secure place.

create user appUser with password = 'veryStrongPassword123'
go

 

6. Copy and paste the command in the query editor and select Run.
create-user-and-password.png
 

7. In the query editor, execute the following command to give administration permission to the user.

alter role db_owner add member appUser
go
 
8. Copy and paste the command in the query editor and select Run.
give-admin-permission-user.png
 
Congratulations! You successfully created a database user. In the next step, you'll clone the code locally to execute an SQL script.
 

Clone the sample code locally

In this step, you clone a copy locally from the GitHub sample you forked to be able to execute commands locally.

 

Complete the following steps to create a local copy from the repository:

1. Visit the repository you forked on your account.

fork-screenshot.png

 

2. Select Code, then Local and copy the URL.

clone-repo-github.png

 

3. Open git bash locally, and execute the following command to clone the repository. Make sure to use your URL not the one in the command below.

git clone https://github.com/john0isaac/azure-sql-db-session-recommender.git
 
4. Open the cloned folder in your preferred text editor.
open-repo-vscode.png
 

Execute a SQL script using sqlcmd locally to set up the database

In this step, you modify a SQL script and execute it on your database using sqlcmd to create the tables and procedures that enable your backend to work.
 
1. Open the database/setup-database.sql file and add the appUser username and password you created in the previous step.
vs-code-before-adding.png

 

2. Inside the resource group you created, select Azure OpenAI resource to get the endpoint URL and key.
 
3. Select keys and Endpoint from the left side navigation menu.
select-openai-keys-endpoints.png

 

4. Copy your endpoint URL and one of the two keys to your clipboard.
 
5. Make the following changes to the SQL script to setup your database:
What Value
OpenAIUrl Add your Azure OpenAI endpoint URL and remove the forward slash from the end of the URL.
OpenAIKey Add one of the two keys you copied from the Azure OpenAI Keys and Endpoint page.
APPUSERNAME Add the username you created in a previous step from the Query Editor.
APPUSERPASSWORD Add the password you created in a previous step with the username in the Query Editor.
OpenAIDeploymentName Add the name of your embedding model deployment. For example, enter embedding-model.

 

vs-code-after-adding.png
 
6. Save the file.
 
7. In your preferred terminal, execute this command to connect with the database server and execute the setup-database.sql script.
sqlcmd --server session-recommender-demo.database.windows.net --database-name sqldb --user-name appUser --password veryStrongPassword123 --input-file ./database/setup-database.sql
 
terminal-screenshot.png

 

Congratulation! You successfully executed an SQL script to set up your database with the required tables and procedures. In the next step, you'll create a function that gets the embeddings for your data from OpenAI.

 

Step 5: Create an Azure Function App

Azure Functions is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers.

 

In this step, you'll:

  • Create an Azure Function App in the Azure portal.
  • Configure Azure Function Application settings in the Azure portal.
  • Configure the Workflow to deploy your function in GitHub.

 

Create an Azure Function App in the Azure portal

In this step, you create an Azure Function which enables you to interact with serverless functions that trigger based on a specific time or event like a database insert operation.

 

Complete the following steps to create an Azure Function App in the Azure portal:

1. Inside your resource group, select Create.

JohnAziz_0-1702566384180.png

 

 

2. Type function app in the search bar of the Marketplace page and select Create Function App from the options that appear.

search-function-app.png

 

3. Perform the following tasks:

What Value
Subscription Use the same subscription you used to apply for Azure OpenAI access.
Resource group Use the same resource group you created before.
Function App name Enter a unique name for your database. For example, session-recommender-function.
Server Select Create new to create a new SQL server. Further details are provided below.
Do you want to deploy code or container image? Select Code. This option specifies whether your deployment consists of code or a container.
Runtime stack Select .NET.
Version Select 6.
Region Select East US. This is the region where the rest of the resources you created reside.
Operating System Select Linux.
Hosting options and plans Select Consumption.

 

create-function-app.png

 

4. Select Next : Storage > to move to the storage settings.

 

5. Create a new storage account or proceed with a preexisting one.

 

6. Select Next : Networking to move to the networking settings.

 

7. Make sure that Enable public access is On.

 

8. Select Next : Monitoring > to move to the monitoring settings.

 

9. Enable Application insights or disable it.

 

10. Select Next : Deployment > to move to the deployment settings.

 

11. Perform the following tasks to enable continuous deployment from GitHub:

What Value
Continuous deployment Select Enable.
GitHub account Select your GitHub account.
Organization Select your organization. If you are using your personal account then select it.
Repository Select azure-sql-db-session-recommender.
Branch Select main.

 

deployment-config-function.png

 

12. Select Review + create.

 

13. Confirm your configuration settings and select Create to start provisioning the resource. Wait for the deployment to finish.

 

14. After the deployment finishes, select Go to resource to inspect your created resource. Here, you can manage your resource and find important information like the function URL and application settings.

go-to-resource-function.png

 

Congratulation! You successfully created an Azure Function App. In the next step, you will configure the Function Application settings and add your environment variables.

 

Configure Azure Function Application settings in the Azure portal

In this step, you configure the Application settings to make the function able to communicate with other cloud resources.

 

Complete the following steps to configure the Application settings of an Azure Function App in the Azure portal:

1. In the Azure Function App, select Configuration from the left side menu. 

function-configuration.png

 

2. Select + New application setting to add new environment variables to the function configuration.

select-new-application-setting.png

 

3. Add the following names and values one by one and select Ok. Make sure to add your own values.

Name Value
AzureSQL.ConnectionString Server=session-recommender-demo.database.windows.net; Database=sqldb; User=appUser; Password=veryStrongPassword123
AzureOpenAI.Endpoint https://session-recommender-openai-demo.openai.azure.com/
AzureOpenAI.Key ADD-YOUR-OPENAI-KEY
AzureOpenAI.DeploymentName embedding-model

 

4. Once you add the four variables, select Save to confirm your changes.

save-application-setting.png

 

Congratulations! You successfully configured your application settings. In the next step, you will configure the GitHub deployment workflow to make it work successfully.

 

Configure the Workflow to deploy your function in GitHub

In this step, you modify the GitHub deployment workflow to point to the folder that contains your Function configuration.

 

Complete the following steps to configure the workflow to deploy your function in GitHub:

1. Visit your forked repository on GitHub and notice the failing workflow.

github-workflow-failed.png

 

2. Open the function workflow file.github/workflows/main_session-recommender-demo.yml and select the pen icon to edit the file.

edit-function-workflow.png

 

3. Modify the AZURE_FUNCTIONAPP_PACKAGE_PATH to point to the function folder "./func".

 

4. Select Commit changes, and review your commit message and description. Select Commit changes.

 

5. Select actions to review the workflow run status.

select-actions.png

 

Congratulations! You successfully configured your function. In the next step, you will create a static web app to host your React frontend application.

 

Step 6: Create an Azure Static Web App

Azure Static Web Apps is a service that automatically builds and deploys full stack web apps to Azure from a code repository.

 

In this step, you'll:

  • Create an Azure static web app in the Azure portal.
  • Configure the Azure static web app database connection in the Azure portal.
  • Insert a test sample in the database using the Azure SQL database Query editor.

 

Create an Azure static web app in the Azure portal

In this step, you create an Azure Static Web App and connect it with your GitHub account to deploy a React application.

 

Complete the following steps to configure the workflow to deploy your function in the Azure portal:

1. Inside your resource group, select Create.

JohnAziz_0-1702566384180.png

 

2. Type function app in the search bar of the Marketplace page and select Create Function App from the options that appear.

create-static-web-app.png

 

3. Perform the following steps to configure your static web app.

What Comment
Subscription Use the same subscription you used to apply for Azure OpenAI access.
Resource group Use the same resource group you created before.
Name Enter a unique name for your application.
Plan type Select Free. You can scale it up later.
Region Select a region close to you for best response times. For example, select East US 2.

 

static-web-app-part-one.png

 

4. Scroll down to the GitHub settings.

 

5. Perform the following steps to set up the GitHub continuous deployment workflow.

What Comment
Source Select Enable.
GitHub account Select your GitHub account.
Organization Select your organization. If you are using your personal account then select it.
Repository Select azure-sql-db-session-recommender.
Branch Select main.
Build Presets Select React.
App location Enter "/client".
Api location Leave empty.
Output location Enter "dist".

 

static-web-app-part-two.png

 

6. Select Review + create.

 

7. Confirm your configuration settings and select Create to start provisioning the resource. Wait for the deployment to finish.

 

8. After the deployment finishes, select Go to resource to inspect your created resource. Here, you can manage your resource and find important information like the website URL and database connection settings.

select-go-to-resource-static-app.png

 

Congratulations! You successfully created a static web app and deployed your website. In the next step, you will connect the static web app with the database to be able to access your data.

 

Configure the Azure static web app database connection in the Azure portal.

In this step, you enable communication between your static web app and database using the databases link.

 

1. In the static web app, select Database connection from the left side navigation menu.

select-database-connection.png

 

2. Select Link existing database to start the database linking process.

select-link-exisitng-db.png

 

3. Perform the following tasks to configure the connection between the website and database:

What Comment
Database type Select Azure SQL Database.
Subscription Use the same subscription you used to apply for Azure OpenAI access.
Resource group Add the name of the resource group that contains your SQL server.
Resource name Add the name of your SQL server.
Database name Add the name of your database. For example, enter sqldb.
Username Add the username you created before. Enter appUser.
Password Add the password you used while creating the new user appUser.

 

database-connection-config-static-web.png

 

4. Select the acknowledgement then, select Link to confirm your selections. Note that the connection configuration file exists on the repository you cloned staticwebapp.database.config.json.

 

Congratulations! You successfully established a link between your website and database. In the next step, you will insert some data to trigger the function and get the embedding vector.

 

Insert a test sample in the database using the Azure SQL database Query editor

In this step, you insert some data into the web.sessions table which will trigger the Azure function to get the embeddings from OpenAI.

 

Complete the following steps to create a new record inside your database using the Azure SQL database Query editor:

1. Inside your database, select Query editor from the left-side navigation menu.

JohnAziz_0-1702575572780.png

 

2. Select your preferred authentication method to log into the database and execute SQL commands.

JohnAziz_1-1702575571420.png

 

3. In the query editor, execute the following command to insert a new record in the database.

insert into web.sessions
(title, abstract)
values
('Building a session recommender using OpenAI and Azure SQL', 'In this fun and demo-driven session you’ll learn how to integrate Azure SQL with OpenAI to generate text embeddings, store them in the database, index them and calculate cosine distance to build a session recommender. And once that is done, you’ll publish it as a REST and GraphQL API to be consumed by a modern JavaScript frontend. Sounds pretty cool, uh? Well, it is!')
 
4. Copy and paste the command in the query editor and select Run.
insert-data-db.png
 
5. Navigate to your static web app and select View app in browser.
view-website-static-app.png

 

6. Note that there is only one session available for you to query the one you inserted before.

website-demo-before.png

 

7. Enter any related word to the data you have inserted and notice the results.

website-demo-after.png

 

Congratulations!! You successfully built the full application.

 

Clean Up

Once you finish experimenting on Microsoft Azure you might want to delete the resources to not consume any more money from your subscription.

You can delete the resource group and it will delete everything inside it or delete the resources one by one that's totally up to you.

 

Conclusion

Congratulations! You've learned how to create an Azure OpenAI resource, how to deploy an embedding model from the Azure OpenAI studio, how to create an Azure SQL database and server, how to use the query editor to create a database user, how to execute SQL scripts locally using sqlcmd to setup the database, how to create an Azure Function and configure continuous deployment with GitHub, and how to create an Azure Static Web App and establish a database connection. By using these technologies you can explore the power of OpenAI and Azure SQL in generating text embeddings and finding the similarity between "things".

 

Next steps

 

Documentation

 

Training Content

 

Found this useful? Share it with others and follow me to get updates on:

Feel free to share your comments and/or inquiries in the comment section below..

See you in future demos!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.