{"id":2364,"date":"2023-09-05T04:00:40","date_gmt":"2023-09-05T11:00:40","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=2364"},"modified":"2023-09-04T16:03:27","modified_gmt":"2023-09-04T23:03:27","slug":"using-openai-rest-endpoints-with-azure-sql-database","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/using-openai-rest-endpoints-with-azure-sql-database\/","title":{"rendered":"Using OpenAI REST Endpoints with Azure SQL Database"},"content":{"rendered":"<p>Everyone loves OpenAI these days as it can do some amazing things. Here, I asked it to create a picture of a cat in a data center:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1.jpg\"><img decoding=\"async\" class=\"alignnone wp-image-2366 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1.jpg\" alt=\"Image cat1\" width=\"464\" height=\"464\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1.jpg 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1-300x300.jpg 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1-150x150.jpg 150w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1-768x768.jpg 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1-24x24.jpg 24w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1-48x48.jpg 48w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1-96x96.jpg 96w\" sizes=\"(max-width: 464px) 100vw, 464px\" \/><\/a><\/p>\n<p style=\"text-align: center\"><em>(Go ahead and click the image to see a bigger version of this nightmare fuel)<\/em><\/p>\n<p>Incredible!!!!<\/p>\n<p>But how can we use OpenAI in real world applications and scenarios with Azure SQL Database?<\/p>\n<h2>Using OpenAI with the Azure SQL Database<\/h2>\n<p>Taking inspiration from Buck Woody&#8217;s blog &#8220;<a href=\"https:\/\/cloudblogs.microsoft.com\/sqlserver\/2023\/06\/14\/secure-your-ai-using-sql-server-machine-learning-with-microsoft-azure-openai-services\/\" target=\"_blank\" rel=\"noopener\">Secure your AI using SQL Server Machine Learning with Microsoft Azure OpenAI Services<\/a>&#8220;, what can we do in Azure SQL Database if we don&#8217;t have access to <a href=\"https:\/\/learn.microsoft.com\/sql\/machine-learning\/sql-server-machine-learning-services?view=sql-server-ver16\" target=\"_blank\" rel=\"noopener\">SQL Server Machine Learning Services<\/a>? We can use <a href=\"https:\/\/learn.microsoft.com\/sql\/relational-databases\/system-stored-procedures\/sp-invoke-external-rest-endpoint-transact-sql?view=azuresqldb-current&amp;tabs=request-headers\" target=\"_blank\" rel=\"noopener\">External REST Endpoint Invocation<\/a> of course! So, let&#8217;s take a look at how to accomplish this step by step.<\/p>\n<h2>Setups<\/h2>\n<p>First, we need access to <a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/overview\" target=\"_blank\" rel=\"noopener\">OpenAI in Azure<\/a>. You can request access to this new service with <a href=\"https:\/\/aka.ms\/oai\/access\">this form here<\/a>.<\/p>\n<p>https:\/\/aka.ms\/oai\/access<\/p>\n<p>Once you have access, creating an OpenAI service is very simple in the Azure Portal.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai1.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2373\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai1.png\" alt=\"Image openai1\" width=\"1113\" height=\"177\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai1.png 1113w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai1-300x48.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai1-1024x163.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai1-768x122.png 768w\" sizes=\"(max-width: 1113px) 100vw, 1113px\" \/><\/a><\/p>\n<p>Click the <strong>Azure OpenAI symbol<\/strong>, provide it with some basic information like resource group, name, and location (use US East if you want to play around with DALL-E) and you are done.<\/p>\n<p>Once the resource is created, click the <strong>Model Deployments optio<\/strong>n on the left side of the page, then click <strong>Manage Deployments<\/strong> to open OpenAI Studio in the browser.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai2.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2374\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai2.png\" alt=\"Image openai2\" width=\"1178\" height=\"421\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai2.png 1178w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai2-300x107.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai2-1024x366.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai2-768x274.png 768w\" sizes=\"(max-width: 1178px) 100vw, 1178px\" \/><\/a><\/p>\n<p>Now in Azure AI Studio, click on the <strong>Deployments tab<\/strong> on the left if not already selected then click <strong>Create new deploymen<\/strong>t.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai3.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2375\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai3.png\" alt=\"Image openai3\" width=\"558\" height=\"415\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai3.png 558w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai3-300x223.png 300w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/a><\/p>\n<p>In the <strong>Deploy Model<\/strong> modal window, choose <strong>gpt-35-turbo<\/strong>. The gpt-35-turbo-16k option is for when you need very lengthy responses (perfect for those term papers) but does cost a bit more per request. For this example, we will not need the 16k option.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai4.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2376\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai4.png\" alt=\"Image openai4\" width=\"622\" height=\"449\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai4.png 622w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai4-300x217.png 300w\" sizes=\"(max-width: 622px) 100vw, 622px\" \/><\/a><\/p>\n<p>Next, name the model in the<strong> Deployment nam<\/strong>e field. Then click the blue <strong>Create<\/strong> button.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai5.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2379\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai5.png\" alt=\"Image openai5\" width=\"617\" height=\"520\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai5.png 617w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai5-300x253.png 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/a><\/p>\n<p>And that&#8217;s it, you now have your own personal ChatGPT prompt to talk to endlessly about live, the universe and who the Witches of Dathomir are.<\/p>\n<h2>Ask it a question<\/h2>\n<p>While in Azure AI Studio, click the <strong>Chat<\/strong> tab on the left side of the screen.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai6.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2381\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai6.png\" alt=\"Image openai6\" width=\"245\" height=\"306\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai6.png 245w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai6-240x300.png 240w\" sizes=\"(max-width: 245px) 100vw, 245px\" \/><\/a><\/p>\n<p>On the right of the page, we are going to use the <strong>Chat Session<\/strong> area to ask a question.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai7.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2382\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai7.png\" alt=\"Image openai7\" width=\"745\" height=\"582\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai7.png 745w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai7-300x234.png 300w\" sizes=\"(max-width: 745px) 100vw, 745px\" \/><\/a><\/p>\n<p>Use the<strong> text field<\/strong> on the bottom to ask a question, then click the<strong> paper airplane icon<\/strong> to submit it.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai8.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2383\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai8.png\" alt=\"Image openai8\" width=\"705\" height=\"123\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai8.png 705w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai8-300x52.png 300w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><\/a><\/p>\n<p>Within a few seconds, we get our <strong>answer<\/strong>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai9.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2384\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai9.png\" alt=\"Image openai9\" width=\"687\" height=\"275\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai9.png 687w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai9-300x120.png 300w\" sizes=\"(max-width: 687px) 100vw, 687px\" \/><\/a><\/p>\n<p>While still on this page, on the top of the chat session tile click the <strong>View Code<\/strong> button.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai10.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2385\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai10.png\" alt=\"Image openai10\" width=\"454\" height=\"108\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai10.png 454w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai10-300x71.png 300w\" sizes=\"(max-width: 454px) 100vw, 454px\" \/><\/a><\/p>\n<p>In the<strong> Sample Code<\/strong> modal window, look at the bottom at the <strong>Endpoint<\/strong> and <strong>Key<\/strong> values. Copy these values for we will be using them later in the example.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai11.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2386\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai11.png\" alt=\"Image openai11\" width=\"554\" height=\"844\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai11.png 554w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai11-197x300.png 197w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/a><\/p>\n<h2>OpenAI REST Endpoints<\/h2>\n<p>Using the External REST Endpoint Invocation stored procedure, we can now ask our OpenAI service a question from within an Azure SQL Database using data from a table. For this example, the AdventureWorks sample data has been loaded so that we have something to work with just as Mr. Buck Woody did in his <a href=\"https:\/\/cloudblogs.microsoft.com\/sqlserver\/2023\/06\/14\/secure-your-ai-using-sql-server-machine-learning-with-microsoft-azure-openai-services\/\" target=\"_blank\" rel=\"noopener\">blog post<\/a>.<\/p>\n<p>To start using the stored procedure, open a new query sheet if in Azure Data Studio, use SSMS, or use sqlcmd; whichever you are most comfortable with. The first example is going to ask a simple question as we did in the chat session page. Let&#8217;s build up the procedure from the start as well.<\/p>\n<p>Starting to build our T-SQL code, we are going to declare and prefill some variables:<\/p>\n<p><div class=\"alert alert-danger\"><strong>Be sure to change the URL to match your service<\/strong><\/div><\/p>\n<div>\n<pre>declare @url nvarchar(4000) = N'https:\/\/skynetbeta.openai.azure.com\/openai\/deployments\/chattykathy\/chat\/completions?api-version=2023-07-01-preview';\r\ndeclare @headers nvarchar(102) = N'{\"api-key\":\"1001001sos1001001indistress\"}'\r\ndeclare @payload nvarchar(max) = N'{\"messages\":[{\"role\":\"system\",\"content\":\"Why is the sky blue?\"}]}'\r\ndeclare @ret int, @response nvarchar(max);<\/pre>\n<\/div>\n<p>First, we start by declaring the <strong>URL<\/strong> or the Endpoint we want to use. Remember a bit back where we <strong>copied the Endpoint in the Sample Code modal<\/strong>? Use that URL here. Second, are the <strong>request headers<\/strong>. For this example, we are going to pass the <strong>api-key<\/strong> which allows us access to use the endpoint. Again, use the value you copied from the <strong>Sample Code modal key field<\/strong>. The payload variable will contain the chat prompt text we want to use to ask OpenAI a question. The last parameter we are declaring is the <strong>response<\/strong> variable. This will contain the <strong>answer<\/strong> gpt gives us in a <strong>JSON<\/strong> format.<\/p>\n<p>Putting the declaration section together with the procedure call produces this T-SQL you can run against your Azure SQL Database:<\/p>\n<div>\n<pre>declare @url nvarchar(4000) = N'https:\/\/skynetbeta.openai.azure.com\/openai\/deployments\/chattykathy\/chat\/completions?api-version=2023-07-01-preview';\r\ndeclare @headers nvarchar(102) = N'{\"api-key\":\"1001001sos1001001indistress\"}'\r\ndeclare @payload nvarchar(max) = N'{\"messages\":[[{\"role\":\"system\",\"content\":\"You are an AI assistant that helps people find information.\"},\r\n                                   {\"role\":\"system\",\"content\":\"Why is the sky blue?\"}]}'\r\ndeclare @ret int, @response nvarchar(max);\r\nexec @ret = sp_invoke_external_rest_endpoint\r\n  \u00a0 @url = @url,\r\n  \u00a0 @method = 'POST',\r\n  \u00a0 @headers = @headers,\r\n  \u00a0 @payload = @payload,\r\n  \u00a0 @timeout = 230,\r\n  \u00a0 @response = @response output;\r\nselect @ret as ReturnCode, @response as Response;<\/pre>\n<div>With the response containing the answer:<\/div>\n<div>\n<pre>\"choices\": [\r\n  \u00a0 {\r\n  \u00a0 \u00a0 \u00a0 \"index\": 0,\r\n  \u00a0 \u00a0 \u00a0 \"finish_reason\": \"stop\",\r\n  \u00a0 \u00a0 \u00a0 \"message\": {\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"role\": \"assistant\",\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"content\": \"The sky appears blue because when the sun's light enters Earth's atmosphere,\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 it is scattered by the gases and particles in the air. This scattering causes the shorter\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 blue wavelengths of light to be dispersed more than the other colors in the spectrum.\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 This is known as Rayleigh scattering. As a result, when we look up at the sky during\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 the day, we see it as blue.\"\r\n  \u00a0 \u00a0 \u00a0 },\r\n  \u00a0 \u00a0 \u00a0 \"content_filter_results\": {\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"hate\": {\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"filtered\": false,\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"severity\": \"safe\"\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 },\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"self_harm\": {\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"filtered\": false,\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"severity\": \"safe\"\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 },\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"sexual\": {\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"filtered\": false,\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"severity\": \"safe\"\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 },\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"violence\": {\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"filtered\": false,\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"severity\": \"safe\"\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\r\n  \u00a0 \u00a0 \u00a0 }\r\n  \u00a0 }\r\n],<\/pre>\n<div>Congratulations, you have just called an OpenAI prompt from within an Azure SQL Database.<\/div>\n<div><\/div>\n<h2>Using Buck&#8217;s data<\/h2>\n<div>Going back to<a href=\"https:\/\/cloudblogs.microsoft.com\/sqlserver\/2023\/06\/14\/secure-your-ai-using-sql-server-machine-learning-with-microsoft-azure-openai-services\/\" target=\"_blank\" rel=\"noopener\"> Buck&#8217;s blog post<\/a>, he used the adventure works product data to ask OpenAI to create some copy for a marketing campaign. We can replicate that exact flow by building the prompt we use in the payload parameter dynamically with T-SQL. Here is the query he used (slightly changed for Azure SQL DB&#8217;s sample data):<\/div>\n<div><\/div>\n<div>\n<pre>SELECT Name \u00a0+ ': ' + Description\r\n  FROM SalesLT.vProductAndDescription\r\n WHERE ProductID = 980 and Culture = 'en';<\/pre>\n<div>with the result being:<\/div>\n<div><\/div>\n<pre>Mountain-400-W Silver, 38: This bike delivers a high-level of performance on a budget.\r\nIt is responsive and maneuverable, and offers peace-of-mind when you decide to go off-road.<\/pre>\n<div><span style=\"font-size: 1rem;text-align: var(--bs-body-text-align)\">and we can create a prompt from this with the following T-SQL:<\/span><\/div>\n<div><\/div>\n<\/div>\n<div>\n<div>\n<pre>declare @adcopy VARCHAR(500);\r\n\r\nset @adcopy =\r\n(SELECT Name \u00a0+ ': ' + Description\r\nFROM SalesLT.vProductAndDescription\r\nWHERE ProductID = 980 and Culture = 'en');<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div>And we would declare the payload with this new prompt as follows:<\/div>\n<div>\n<div>\n<pre>declare @payload nvarchar(max) = N'{\"messages\":[{\"role\":\"system\",\"content\":\"You are an AI assistant that helps people find information.\"},\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0{\"role\":\"user\",\"content\":\"You are an experienced marketing expert. Generate 2000 letters of ad copy for '+(@adcopy)+'\"}]}'<\/pre>\n<\/div>\n<p>Putting it all together, we have the following T-SQL:<\/p>\n<\/div>\n<div><\/div>\n<div>\n<div>\n<pre>declare @adcopy VARCHAR(500);\r\n\r\nset @adcopy =\r\n(SELECT Name \u00a0+ ': ' + Description\r\nFROM SalesLT.vProductAndDescription\r\nWHERE ProductID = 980 and Culture = 'en');\r\n\r\ndeclare @url nvarchar(4000) = N'https:\/\/skynetbeta.openai.azure.com\/openai\/deployments\/chattykathy\/chat\/completions?api-version=2023-07-01-preview';\r\ndeclare @headers nvarchar(102) = N'{\"api-key\":\"1001001sos1001001indistress\"}'\r\ndeclare @payload nvarchar(max) = N'{\"messages\":[{\"role\":\"system\",\"content\":\"You are an AI assistant that helps people find information.\"},\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0{\"role\":\"user\",\"content\":\"You are an experienced marketing expert. Generate 2000 letters of ad copy for '+(@adcopy)+'\"}]}'\r\ndeclare @ret int, @response nvarchar(max);\r\nexec @ret = sp_invoke_external_rest_endpoint\r\n  \u00a0 @url = @url,\r\n  \u00a0 @method = 'POST',\r\n  \u00a0 @headers = @headers,\r\n  \u00a0 @payload = @payload,\r\n  \u00a0 @timeout = 230,\r\n  \u00a0 @response = @response output;\r\nselect @ret as ReturnCode, @response as Response;<\/pre>\n<\/div>\n<\/div>\n<div>With the response being:<\/div>\n<div><\/div>\n<div>\n<pre>\"message\": {\r\n  \u00a0 \"role\": \"assistant\",\r\n  \u00a0 \"content\": \"Dear Outdoor Enthusiasts,\\n\\nAre you searching for a high-performance bike that won't break the bank? \r\n                Look no further than the Mountain-400-W Silver, 38!\\n\\nThis top-of-the-line bike was designed to deliver\r\n                a level of performance that's typically reserved for much pricier models. With responsive handling and\r\n                unbeatable maneuverability, you'll be able to tackle even the toughest trails with ease.\\n\\nBut that's\r\n                not all - the Mountain-400-W Silver, 38 also offers unparalleled peace of mind when you venture off-road.\r\n                Its sturdy construction and reliable components mean that you can trust your bike to hold up to even the\r\n                roughest terrain.\\n\\nSo why wait? Whether you're a seasoned pro or just starting out, the Mountain-400-W Silver,\r\n                38 is the perfect choice for any mountain biker looking to take their skills to the next level. Order yours\r\n                today and experience the ultimate in off-road performance!\"\r\n},\r\n\r\n<\/pre>\n<h2>Pesky Passkeys<\/h2>\n<div>In all the previous examples, we have passed an <strong>api-key<\/strong> in with the <strong>header<\/strong> parameter. This is fine but will require a developer to have this key and potentially leak it in a code repository. What we can do to solve this issue is to use Managed Identities. To start, you just add our database to the Cognitive Services OpenAI User role using <strong>Access control (IAM)<\/strong> on our OpenAI Azure Portal page:<\/div>\n<\/div>\n<div><\/div>\n<div><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-2395\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12.png\" alt=\"Image openai12\" width=\"1632\" height=\"483\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12.png 1632w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12-300x89.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12-1024x303.png 1024w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12-768x227.png 768w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/openai12-1536x455.png 1536w\" sizes=\"(max-width: 1632px) 100vw, 1632px\" \/><\/a><\/div>\n<div><\/div>\n<div>Once that is done, we can run the following T-SQL in the database we have been using (be sure to change the credential name to match your service):<\/div>\n<div><\/div>\n<div>\n<div>\n<pre>create database scoped credential [https:\/\/skynetbeta.openai.azure.com]\r\nwith identity = 'Managed Identity',\r\nsecret = '{\"resourceid\": \"https:\/\/cognitiveservices.azure.com\" }';<\/pre>\n<\/div>\n<\/div>\n<div>Now, with this created, we can remove the header parameter and replace it with the <strong>credential parameter<\/strong> in the stored procedure call:<\/div>\n<div><\/div>\n<div>\n<pre>declare @url nvarchar(4000) = N'https:\/\/skynetbeta.openai.azure.com\/openai\/deployments\/chattykathy\/chat\/completions?api-version=2023-07-01-preview';\r\ndeclare @payload nvarchar(max) = N'{\"messages\":[{\"role\":\"system\",\"content\":\"You are an AI assistant that helps people find information.\"},\r\n  \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0{\"role\":\"user\",\"content\":\"Why is the sky blue?\"}]}'\r\ndeclare @ret int, @response nvarchar(max);\r\n\r\nexec @ret = sp_invoke_external_rest_endpoint\r\n  \u00a0 @url = @url,\r\n  \u00a0 @method = 'POST',\r\n  \u00a0 @payload = @payload,\r\n  \u00a0 @timeout = 230,\r\n<strong>  \u00a0 @credential = [https:\/\/skynetbeta.openai.azure.com],<\/strong>\r\n  \u00a0 @response = @response output;\r\nselect @ret as ReturnCode, @response as Response;<\/pre>\n<\/div>\n<div>A simple fix that alleviates a lot of potential issues down the road.<\/div>\n<div><\/div>\n<h2>Want to know more?<\/h2>\n<div>Looking for more samples and examples? Here is a list of places to start and as always, reach out to us with any questions or comments or if you would like to see this feature do something it does not today!<\/div>\n<div><\/div>\n<div><a href=\"https:\/\/learn.microsoft.com\/sql\/relational-databases\/system-stored-procedures\/sp-invoke-external-rest-endpoint-transact-sql?view=azuresqldb-current&amp;tabs=request-headers\" target=\"_blank\" rel=\"noopener\">Documentation<\/a><\/div>\n<div><a href=\"https:\/\/github.com\/Azure-Samples\/azure-sql-db-invoke-external-rest-endpoints\" target=\"_blank\" rel=\"noopener\">Examples Repository<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Everyone loves OpenAI these days as it can do some amazing things. Here, I asked it to create a picture of a cat in a data center: (Go ahead and click the image to see a bigger version of this nightmare fuel) Incredible!!!! But how can we use OpenAI in real world applications and scenarios [&hellip;]<\/p>\n","protected":false},"author":95874,"featured_media":2377,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2364","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql"],"acf":[],"blog_post_summary":"<p>Everyone loves OpenAI these days as it can do some amazing things. Here, I asked it to create a picture of a cat in a data center: (Go ahead and click the image to see a bigger version of this nightmare fuel) Incredible!!!! But how can we use OpenAI in real world applications and scenarios [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/2364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/users\/95874"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/comments?post=2364"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/2364\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/2377"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=2364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=2364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=2364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}