Azure-Samples / azure-sql-db-session-recommender-v2

Build a Retrieval Augmented Generation solution using OpenAI, Azure Functions, Azure Static Web Apps, Azure SQL DB, Data API builder and Text Embeddings
https://ai.microsofthq.vslive.com/
MIT License
33 stars 19 forks source link
azure-functions azure-sql azure-sql-database data-api-builder open-ai retrieval-augmented-generation vectors

page_type: sample languages:

This sample demonstrates how to build a session recommender using Jamstack and Event-Driven architecture, using Azure SQL DB to store and search vectors embeddings generated using OpenAI. The solution is built using Azure Static Web Apps, Azure Functions, Azure SQL Database, and Azure OpenAI.

A fully working, production ready, version of this sample, that has been used at VS Live conferences, is available here: https://ai.microsofthq.vslive.com/

Retrieval Augmented Generator flow

This repository is a evoution of the Session Recommender sample. In addition to vector search, also Retrieval Augmented Generation (RAG) is used to generate the response to the user query. If you are completely new to this topic, you may want to start there, and then come back here.

Architecture Diagram

A session recommender built using

For more details on the solution check also the following articles:

Native or Classic ?

Azure SQL database can be used to easily and quickly perform vector similarity search. There are two options for this: a native option and a classic option.

The native option uses the new Vector Functions, recently introduced in Azure SQL database. Vector Functions are a set of functions that can be used to perform vector operations directly in the database.

[!NOTE]
Vector Functions are in Public Preview. Learn the details about vectors in Azure SQL here: https://aka.ms/azure-sql-vector-public-preview

The classic option uses the classic T-SQL to perform vector operations, with the support for columnstore indexes for getting good performances.

[!IMPORTANT]
This branch (the main branch) uses the native vector support in Azure SQL. If you want to use the classic T-SQL, switch to the classic branch.

Deploy the sample using the Azure Developer CLI (azd) template

The Azure Developer CLI (azd) is a developer-centric command-line interface (CLI) tool for creating Azure applications.

Prerequisites

Install AZD CLI

You need to install it before running and deploying with the Azure Developer CLI.

Windows

powershell -ex AllSigned -c "Invoke-RestMethod 'https://aka.ms/install-azd.ps1' | Invoke-Expression"

Linux/MacOS

curl -fsSL https://aka.ms/install-azd.sh | bash

After logging in with the following command, you will be able to use azd cli to quickly provision and deploy the application.

Authenticate with Azure

Make sure AZD CLI can access Azure resources. You can use the following command to log in to Azure:

azd auth login

Initialize the template

Then, execute the azd init command to initialize the environment (You do not need to run this command if you already have the code or have opened this in a Codespace or DevContainer).

azd init -t Azure-Samples/azure-sql-db-session-recommender-v2

Enter an environment name.

Deploy the sample

Run azd up to provision all the resources to Azure and deploy the code to those resources.

azd up 

Select your desired subscription and location. Then choose a resource group or create a new resource group. Wait a moment for the resource deployment to complete, click the Website endpoint and you will see the web app page.

Note: Make sure to pick a region where all services are available like, for example, West Europe or East US 2

GitHub Actions

Using the Azure Developer CLI, you can setup your pipelines, monitor your application, test and debug locally.

azd pipeline config

Deploy the database

Since the database is using features that are in Private Preview, it must be deployed manually. After all resources have been deployed, get the database connection string and OpenAI endpoint and key and create a .env file from the .env.sample file. Once that is done, go into the database folder and run the following command:

dotnet run

The .NET application will create the database schema and the required objects.

Test the solution

Add a new row to the Sessions table using the following SQL statement (you can use tools like Azure Data Studio or SQL Server Management Studio to connect to the database. No need to install them if you don't want. In that case you can use the SQL Editor in the Azure Portal):

insert into web.speakers
    (id, full_name, require_embeddings_update)
values
    (5000, 'John Doe', 1)
go

insert into web.sessions 
    (id, title, abstract, external_id, start_time, end_time, require_embeddings_update)
values
    (
        1000,
        '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!',
        'S1',
        '2024-06-01 10:00:00',
        '2024-06-01 11:00:00',
        1
    )
go

insert into web.sessions_speakers
    (session_id, speaker_id)
values
    (1000, 5000)
go

immediately the deployed Azure Function will get executed in response to the INSERT statement. The Azure Function will call the OpenAI service to generate the text embedding for the session title and abstract, and then store the embedding in the database, specifically in the web.sessions table.

select * from web.sessions

You can now open the URL associated with the created Static Web App to see the session recommender in action. You can get the URL from the Static Web App overview page in the Azure portal.

Website running

Run the solution locally

The whole solution can be executed locally, using Static Web App CLI and Azure Function CLI.

Install the required node packages needed by the fronted:

cd client
npm install

once finished, create a ./func/local.settings.json and .env starting from provided samples files, and fill out the settings using the correct values for your environment.

Go back to the sample root folder and then run:

swa build

to build the fronted and then start everything with:

swa start 

and once the local Static Web App environment is running, you can connect to

http://localhost:4280/

and test the solution locally.

Fluent UI

The solution uses Fluent UI for the UI components. The Fluent UI is a collection of UX frameworks from Microsoft that provides a consistent design language for web, mobile, and desktop applications. More details about Fluent UI can be found at the following links:

Credits

Thanks a lot to Aaron Powell for having helped in building the RAG sample, doing a complete UI revamp using the Fluent UI and for the implementaiton of the ask endpoint.