Skip to content

Commit

Permalink
Merge pull request #10 from yorek/main
Browse files Browse the repository at this point in the history
Various improvements
  • Loading branch information
JetterMcTedder committed Sep 26, 2024
2 parents cb8276e + 04401c6 commit 6729213
Show file tree
Hide file tree
Showing 22 changed files with 594 additions and 152 deletions.
29 changes: 12 additions & 17 deletions docs/10-using-azure-openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,26 @@ An embedding is a special format of data representation that machine learning mo
1. Copy the following SQL and paste it into the SQL query editor. You can see from the T-SQL that we are going to create an embedding for a text string.

```SQL
declare @url nvarchar(4000) = N'https://vslive2024ai.openai.azure.com/openai/deployments/vslive2024embeddings/embeddings?api-version=2024-02-01';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-ai.openai.azure.com/openai/deployments/text-embedding-3-large/embeddings?api-version=2024-02-01';
declare @headers nvarchar(300) = N'{"api-key": "OPENAI_KEY"}';
declare @message nvarchar(max);
SET @message = N'{"input": "Its me Hi Im the problem, its me At teatime Everybody agrees Ill stare directly at the sun but never in the mirror It must be exhausting always rooting for the anti-hero"}';
declare @payload nvarchar(max);
set @payload = json_object(
'input': 'Its me Hi Im the problem, its me At teatime Everybody agrees I''ll stare directly at the sun but never in the mirror It must be exhausting always rooting for the anti-hero',
'dimensions': 1536
);

declare @ret int, @response nvarchar(max);

exec @ret = sp_invoke_external_rest_endpoint
@url = @url,
@method = 'POST',
@headers = @headers,
@payload = @message,
@payload = @payload,
@timeout = 230,
@response = @response output;

select @ret as ReturnCode, @response as Response;
select count(*) from openjson(@response, '$.result.data[0].embedding') -- check that the embedding is 1536 dimensions
```

1. Replace the **OPENAI_KEY** text with the AI Language Key that was returned to you in the previous chapter when testing connectivity.
Expand Down Expand Up @@ -88,7 +92,7 @@ The image generation API creates an image from a text prompt.
1. Copy the following SQL and paste it into the SQL query editor. We are going to use a product description from the adventure works dataset which will be sent to the DALL-E 3 text to image endpoint.

```SQL
declare @url nvarchar(4000) = N'https://vslive2024ai.openai.azure.com/openai/deployments/vslive2024wallE/images/generations?api-version=2023-12-01-preview';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-ai.openai.azure.com/openai/deployments/dall-e-3/images/generations?api-version=2024-02-01';
declare @headers nvarchar(300) = N'{"api-key": "OPENAI_KEY"}';
declare @message nvarchar(max);
SET @message = N'{
Expand Down Expand Up @@ -143,7 +147,7 @@ Let's use the new GPT-4o model for this next call. We are going to ask it to des
1. Copy the following SQL and paste it into the SQL query editor.
```SQL
declare @url nvarchar(4000) = N'https://vslive2024ai.openai.azure.com/openai/deployments/vslive2024ChattyKathy/chat/completions?api-version=2024-04-01-preview';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-ai.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2023-03-15-preview';
declare @headers nvarchar(102) = N'{"api-key":"OPENAI_KEY"}';
declare @payload nvarchar(max) = N'{
"messages": [
Expand Down Expand Up @@ -234,10 +238,6 @@ Additional Best Practices from the documentation:

### Sending the prompt text with External REST Endpoint Invocation

> [!NOTE]
> The server name in the URL parameter on the next example is `aidemo` and the headers parameter value for api-key is `1234567890`.
> Please change this name and key to align with the values in your account.
>

1. Open a new query sheet

Expand All @@ -258,7 +258,7 @@ Additional Best Practices from the documentation:
from person
where person_id = 1);
declare @url nvarchar(4000) = N'https://vslive2024ai.openai.azure.com/openai/deployments/vslive2024ChattyKathy/chat/completions?api-version=2024-04-01-preview';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-ai.openai.azure.com/openai/deployments/gpt-4o-mini/chat/completions?api-version=2023-03-15-preview';
declare @headers nvarchar(102) = N'{"api-key":"OPENAI_KEY"}'
declare @payload nvarchar(max) = N'{"messages":[{"role":"system","content":"'+(@adcopy)+'"}]}'
declare @ret int, @response nvarchar(max);
Expand Down Expand Up @@ -390,11 +390,6 @@ Additional Best Practices from the documentation:

### The Todo application, SWA, and External REST Endpoint Invocation

> [!NOTE]
> The server name in the URL parameter on the next example is `aidemo` and the headers parameter value for api-key is `1234567890`.
> Please change this name and key to align with the values in your account.
>

In this next section, we will be using the Todo application against our Free Azure SQL Database. Then, we will be adding to the insert_todo stored procedure to call OpenAI via External REST endpoint invocation. We will be asking OpenAI to translate the Todo task's title into German and then insert that value into the table.
1. Back in the **SQL Server Connections extension**, right click the database profile name,**Free Azure Database**, and select **New Query**. This will bring up a new query sheet.
Expand Down Expand Up @@ -423,7 +418,7 @@ In this next section, we will be using the Todo application against our Free Azu
AS
declare @translated_task VARCHAR(1000);
declare @url nvarchar(4000) = N'https://vslive2024ai.openai.azure.com/openai/deployments/vslive2024ChattyKathy/chat/completions?api-version=2024-04-01-preview';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-ai.openai.azure.com/openai/deployments/gpt-35-turbo/chat/completions?api-version=2024-04-01-preview';
declare @headers nvarchar(102) = N'{"api-key":"OPENAI_KEY"}'
declare @payload nvarchar(max) = N'{"messages":[{"role":"system","content":"Translate \"'+(@title)+'\" into German, only respond with the translation"}]}'
declare @ret int, @response nvarchar(max);
Expand Down
17 changes: 2 additions & 15 deletions docs/11-sql-bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ In this section, you will create a change data stream using Change Tracking, the
1. Next, issue the following command to start the function creation process:

```bash
func init triggerBinding --worker-runtime dotnet
func init triggerBinding --worker-runtime dotnet-isolated
```

1. When this process is finished, click the File Explorer extension to see the new files that were created.
Expand All @@ -85,7 +85,7 @@ In this section, you will create a change data stream using Change Tracking, the
then

```bash
dotnet add package Microsoft.Azure.WebJobs.Extensions.Sql
dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Sql
```

### Create the SQL trigger function
Expand Down Expand Up @@ -181,19 +181,6 @@ In this section, you will create a change data stream using Change Tracking, the

1. One quick item to check is at the top of the file.

```C#
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Sql
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
```

You may see that the **using Microsoft.Azure.WebJobs.Extensions.Sql** line is missing a semicolon (;). Please add a semicolon at the end of that line (**using Microsoft.Azure.WebJobs.Extensions.Sql;**) so it looks like the following:

```C#
using System;
using System.Collections.Generic;
Expand Down
8 changes: 4 additions & 4 deletions docs/13-expand.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The available operators are:
Let's start with the first REST call made, the PII detection and redaction endpoint:

```SQL
declare @url nvarchar(4000) = N'https://vslive2024language.cognitiveservices.azure.com/language/:analyze-text?api-version=2023-04-01';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-ls.cognitiveservices.azure.com/language/:analyze-text?api-version=2023-04-01';
declare @headers nvarchar(300) = N'{"Ocp-Apim-Subscription-Key":"LANGUAGE_KEY"}';
declare @payload nvarchar(max) = N'{
"kind": "PiiEntityRecognition",
Expand Down Expand Up @@ -238,7 +238,7 @@ WHERE A.[key] = 'entities'

Can we make a stored procedure that takes in say the authentication key and content we want to send to a set of endpoints? Maybe a parameter that sets the endpoint we want to use?

If we look at the AI Content Safety URLs, they start with "https://vslive2024language.cognitiveservices.azure.com/contentsafety/text:". The URL also ends with the API version "?api-version=2024-02-15-preview". What is different is what is after text:. The 3 options we can use that have a similar payload are analyze, detectJailbreak, and detectProtectedMaterial.
If we look at the AI Content Safety URLs, they start with "https://dm-dev-workshop-ls.cognitiveservices.azure.com/contentsafety/text:". The URL also ends with the API version "?api-version=2024-02-15-preview". What is different is what is after text:. The 3 options we can use that have a similar payload are analyze, detectJailbreak, and detectProtectedMaterial.

With that said, it seems we have 3 parameters for the stored procedure: Auth key, operation, and message.

Expand All @@ -254,7 +254,7 @@ Can you create a stored procedure that takes in 3 parameters and calls the 3 AI
```SQL
CREATE PROCEDURE aiContentSafety @operation nvarchar(100), @safetykey nvarchar(100), @message nvarchar(max)
AS
declare @url nvarchar(4000) = N'https://vslive2024language.cognitiveservices.azure.com/contentsafety/text:' + @operation + '?api-version=2024-02-15-preview';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-safety.cognitiveservices.azure.com/contentsafety/text:' + @operation + '?api-version=2024-02-15-preview';
declare @headers nvarchar(300) = N'{"Ocp-Apim-Subscription-Key":"'+ @safetykey +'"}';
declare @payload nvarchar(max) = N'{
"text": "'+ @message +'"
Expand Down Expand Up @@ -350,7 +350,7 @@ Can you add logic and JSON functions to the stored procedure to extract informat
```SQL
CREATE PROCEDURE aiContentSafety @operation nvarchar(100), @safetykey nvarchar(100), @message nvarchar(max)
AS
declare @url nvarchar(4000) = N'https://vslive2024language.cognitiveservices.azure.com/contentsafety/text:' + @operation + '?api-version=2024-02-15-preview';
declare @url nvarchar(4000) = N'https://dm-dev-workshop-safety.cognitiveservices.azure.com/contentsafety/text:' + @operation + '?api-version=2024-02-15-preview';
declare @headers nvarchar(300) = N'{"Ocp-Apim-Subscription-Key":"'+ @safetykey +'"}';
declare @payload nvarchar(max) = N'{
"text": "'+ @message +'"
Expand Down
3 changes: 2 additions & 1 deletion docs/2-Database-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The SQL Database Projects extension is an Azure Data Studio and Visual Studio Co
cd /workspaces/azure-sql-db-developers-workshop
```

1. Using the codespace termnal, source the .bashrc file to uptake any changes the install may have made to the path
1. Using the codespace termnal, source the `.bashrc` file to uptake any changes the install may have made to the path

```bash
. ~/.bashrc
Expand Down Expand Up @@ -446,6 +446,7 @@ The SQL Database Projects extension is an Azure Data Studio and Visual Studio Co
```SQL
select * from person
select p.person_name, a.address
from person p, address a
where p.person_id = a.person_id;
Expand Down
21 changes: 14 additions & 7 deletions docs/3-Data-API-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ The `.rest` files all use a VS Code extension that allows you to submit REST/Gra
### REST Examples
#### Get all persons in the table
**Request:**
```bash
http://localhost:5000/rest/person
GET http://localhost:5000/rest/person
```
**Response:**
Expand All @@ -190,10 +191,11 @@ http://localhost:5000/rest/person
---
#### Get person by Primary Key
**Request:**
```bash
http://localhost:5000/rest/person/person_id/1
GET http://localhost:5000/rest/person/person_id/1
```
**Response:**
Expand All @@ -214,10 +216,11 @@ http://localhost:5000/rest/person/person_id/1
---
#### Filter the columns using select
**Request:**
```bash
http://localhost:5000/rest/person?$select=person_email
GET http://localhost:5000/rest/person?$select=person_email
```
**Response:**
Expand Down Expand Up @@ -245,7 +248,7 @@ http://localhost:5000/rest/person?$select=person_email
**Request:**
```bash
http://localhost:5000/rest/person?$filter=person_name eq 'bill'
GET http://localhost:5000/rest/person?$filter=person_name eq 'bill'
```
**Response:**
Expand All @@ -266,7 +269,7 @@ http://localhost:5000/rest/person?$filter=person_name eq 'bill'
**Request:**
```bash
http://localhost:5000/rest/person?$filter=person_name ne 'bill'
GET http://localhost:5000/rest/person?$filter=person_name ne 'bill'
```
**Response:**
Expand Down Expand Up @@ -297,7 +300,7 @@ http://localhost:5000/rest/person?$filter=person_name ne 'bill'
**Request:**
```bash
http://localhost:5000/rest/person?$orderby=person_id desc
GET http://localhost:5000/rest/person?$orderby=person_id desc
```
**Response:**
Expand Down Expand Up @@ -330,7 +333,6 @@ http://localhost:5000/rest/person?$orderby=person_id desc
---
#### Using POST to create a record
(POST will precede the URL)
**Request:**
Expand Down Expand Up @@ -360,6 +362,8 @@ content-type: application/json
}
```
You can double check with the GET commands you learned in the previous section that the new record has been added to the database
---
Expand Down Expand Up @@ -452,6 +456,9 @@ To test the GraphQL endpoints you can either use the `graphqlWorksheet.rest` fil
> [!NOTE]
> Your URL will be different than the one in the above image excepting for the appending of `/graphql/` at the end.
> [!WARNING]
> If you are running from Codespaces and you get an error like `This <your-app-name>.app.github.dev page can’t be found`, check the URL to see if `:5000` has been automatically added before `/graphql/`. If yes, remove it: Codespaces port redirection will automatically take care of directing request to the correct port.
1. You will now see the GraphQL playground, Banana Cake Pop:
![A picture of the welcome page of Banana Cake Pop GraphQL interactive playground](../docs/media/ch3/dab-bcp.png)
Expand Down
5 changes: 0 additions & 5 deletions docs/5-deploy-to-azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ The next section of the workshop will be using an Azure SQL Database. To move ou

![A picture of using Free Azure Database as the connection profile name](./media/ch5/deploy9.png)

* After pressing Enter and the connection profile is verified, a warning box **may** appear on the lower right of the screen. This warning is indicating that due to new security features within the database, you need to enable the self-signed certificate.
Click the Enable Trust Server Certificate green button to continue.

![A picture of clicking the Enable Trust Server Certificate green button to continue](./media/ch5/deploy10.png)

* There is now a connection to the Azure SQL Database running in the cloud in the code space you can use for deployment and further development.

![A picture of code spaces indicating a successful connection](./media/ch5/deploy11.png)
Expand Down
32 changes: 22 additions & 10 deletions docs/6-JSON-in-the-db.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,17 @@ The available operators are:

```SQL
SELECT o.OrderNumber,
JSON_OBJECT('Date':o.OrderTime, 'Price':o.Price, 'Quantity':o.Quantity,
'AccountDetails':JSON_OBJECT('AccountNumber':o.AccountNumber,
'PhoneNumbers':JSON_ARRAY(a.Phone1, a.Phone2, a.Phone3))) AS OrderDetails
FROM OrdersR AS o
JOIN Accounts AS a
JSON_OBJECT(
'Date':o.OrderTime,
'Price':o.Price,
'Quantity':o.Quantity,
'AccountDetails':JSON_OBJECT(
'AccountNumber':o.AccountNumber,
'PhoneNumbers':JSON_ARRAY(a.Phone1, a.Phone2, a.Phone3)
)
) AS OrderDetails
FROM OrdersR AS o
JOIN Accounts AS a
ON a.AccountNumber = o.AccountNumber;
```

Expand All @@ -199,11 +205,17 @@ The available operators are:

```SQL
SELECT JSON_OBJECTAGG(
OrderNumber:JSON_OBJECT(
'Date':o.OrderTime, 'Price':o.Price, 'Quantity':o.Quantity,
'AccountDetails':JSON_OBJECT('AccountNumber':o.AccountNumber,
'PhoneNumbers':JSON_ARRAY(a.Phone1, a.Phone2, a.Phone3))))
AS Orders
OrderNumber:JSON_OBJECT(
'Date':o.OrderTime,
'Price':o.Price,
'Quantity':o.Quantity,
'AccountDetails':JSON_OBJECT(
'AccountNumber':o.AccountNumber,
'PhoneNumbers':JSON_ARRAY(a.Phone1, a.Phone2, a.Phone3)
)
)
)
AS Orders
FROM OrdersR AS o
JOIN Accounts AS a
ON a.AccountNumber = o.AccountNumber;
Expand Down
Loading

0 comments on commit 6729213

Please sign in to comment.