Build scalable data solutions with SQL database in Microsoft Fabric

 

Exercise 1 - Setting up the Environment

Task-1: Create a New Fabric Workspace










Uploading: 246300 of 246300 bytes uploaded.

Task-2: Create a SQL Database in Microsoft Fabric





Task-3: Load the database with Sample data






Task-4: Working with the SQL database in Microsoft Fabric



Task-5: Exploring SQL analytics Endpoint












Exercise 2 - Copilot Capabilities for SQL database in Microsoft Fabric




--Create a query to get the product that is selling the most






Task-2: Copilot Quick Actions within the Query Window



SELECT c.CustomerID, c.FirstName,c.LastName,
COUNT(so.SalesOrderID) AS TotalPurchases,
SUM(so.SubTotal) AS TotalSpent,
AVG(so.SubTotal) AS AverageOrderValue,
MAX(so.OrderDate) AS LastPurchaseDate
FROM
SalesLT.Customer AS c JOIN SalesLT.SalesOrderHeader AS so ON c.CustomerID = so.CustomerID
GROUP BY c.CustomerID, c.FName, c.LName ORDER BY TotalSpent DESC;











Task-3: Using Copilot Chat Pane - Natural Language to SQL











Task-4: Chat Pane - Get results from Copilot





Task-5: Chat Pane - Write (with approval)








Exercise 3 - RAG Implementation with SQL Database in Fabric

Task-1: Create OpenAI resource in Azure Portal



















Task-2: Create Model Deployment for text-embedding-ada-002











Task-3: Setup of database credential



if not exists(SELECT * FROM sys.symmetric_keys WHERE [name] = '##MS_DatabaseMasterKey##')
begin
    CREATE master key encryption by password = N'V3RYStr0NGP@ssw0rd!';
end
go
if exists(SELECT * FROM sys.[database_scoped_credentials] where name = 
'Paste the OpenAI endpoint here')
begin
    DROP database scoped credential [Paste the OpenAI endpoint here];
end
CREATE database scoped credential [Paste the OpenAI endpoint here]
with identity = 'HTTPEndpointHeaders', secret = '{"api-key": "Paste the OpenAI Key here"}';
go


Task-4: Create embeddings for relational data


ALTER TABLE [SalesLT].[Product]
ADD  embeddings VECTOR(1536), chunk nvarchar(2000);



CREATE or ALTER PROCEDURE SalesLT.create_embeddings
(
    @input_text nvarchar(max),
    @embedding vector(1536) output
)
AS
BEGIN
DECLARE @url varchar(max) = '[Paste the OpenAI Endpoint here]openai/deployments/text-embedding-ada-002/embeddings?api-version=2024-06-01';
DECLARE @payload nvarchar(max) = json_object('input': @input_text);
DECLARE @response nvarchar(max);
DECLARE @retval int;

-- Call to Azure OpenAI to get the embedding of the search text
BEGIN try
    EXEC @retval = sp_invoke_external_rest_endpoint
        @url = @url,
        @method = 'POST',
        @credential = [Paste the OpenAI Endpoint here], 
        @payload = @payload,
        @response = @response output;
END try
BEGIN catch
    SELECT 
        'SQL' as error_source, 
        error_number() as error_code,
        error_message() as error_message
    return;
end catch
if (@retval != 0) begin
    SELECT 
        'OPENAI' as error_source, 
        json_value(@response, '$.result.error.code') as error_code,
        json_value(@response, '$.result.error.message') as error_message,
        @response as error_response
    return;
end
-- Parse the embedding returned by Azure OpenAI
DECLARE @json_embedding nvarchar(max) = json_query(@response, '$.result.data[0].embedding');

-- Convert the JSON array to a vector and set return parameter
set @embedding = CAST(@json_embedding AS VECTOR(1536));
END;




SET NOCOUNT ON
DROP TABLE IF EXISTS #MYTEMP 
DECLARE @ProductID int
DECLARE @text nvarchar(max);
DECLARE @vector vector(1536);
SELECT * INTO #MYTEMP FROM [SalesLT].Product
SELECT @ProductID = ProductID FROM #MYTEMP
SELECT TOP(1) @ProductID = ProductID FROM #MYTEMP
WHILE @@ROWCOUNT <> 0
BEGIN
    SET @text = (SELECT p.Name + ' '+ ISNULL(p.Color,'No Color') + ' '+  c.Name + ' '+  m.Name + ' '+  ISNULL(d.Description,'')
                    FROM 
                    [SalesLT].[ProductCategory] c,
                    [SalesLT].[ProductModel] m,
                    [SalesLT].[Product] p
                    LEFT OUTER JOIN
                    [SalesLT].[vProductAndDescription] d
                    on p.ProductID = d.ProductID
                    and d.Culture = 'en'
                    WHERE p.ProductCategoryID = c.ProductCategoryID
                    and p.ProductModelID = m.ProductModelID
                    and p.ProductID = @ProductID);
    exec SALESLT.create_embeddings @text, @vector output;
    UPDATE [SalesLT].[Product] SET [embeddings] = @vector, [chunk] = @text WHERE ProductID = @ProductID;
    DELETE FROM #MYTEMP WHERE ProductID = @ProductID
    SELECT TOP(1) @ProductID = ProductID FROM #MYTEMP
END



SELECT COUNT(*) FROM SalesLT.Product WHERE embeddings is null;













































































































































































































































Comments

Popular posts from this blog

AI-900-3,4

AI-900 12,13

AI-900 10,11