{"id":2435,"date":"2023-09-25T01:00:19","date_gmt":"2023-09-25T08:00:19","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/azure-sql\/?p=2435"},"modified":"2023-09-22T13:08:27","modified_gmt":"2023-09-22T20:08:27","slug":"generate-images-with-openai-and-azure-sql","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/azure-sql\/generate-images-with-openai-and-azure-sql\/","title":{"rendered":"Generate images with Azure OpenAI Service (DALL-E) and Azure SQL Database"},"content":{"rendered":"<p>Following on the series of <a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/using-openai-rest-endpoints-with-azure-sql-database\/\" target=\"_blank\" rel=\"noopener\">OpenAI posts<\/a> I have done recently, here is a quick how-to on using DALL-E with External REST endpoint invocation in the Azure SQL Database. Using the <a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/overview\" target=\"_blank\" rel=\"noopener\">OpenAI service in Azure<\/a>, we can use the <a href=\"https:\/\/learn.microsoft.com\/azure\/ai-services\/openai\/reference#image-generation\" target=\"_blank\" rel=\"noopener\">image generation REST endpoints<\/a> to create new and wonderful images such as, if you remember, the cat in the data center.<\/p>\n<p><figure id=\"attachment_2366\" aria-labelledby=\"figcaption_attachment_2366\" class=\"wp-caption aligncenter\" ><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1.jpg\"><img decoding=\"async\" class=\"wp-image-2366\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/cat1.jpg\" alt=\"Image cat1\" width=\"362\" height=\"362\" 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: 362px) 100vw, 362px\" \/><\/a><figcaption id=\"figcaption_attachment_2366\" class=\"wp-caption-text\">(He&#8217;s back!!!! Meow??)<\/figcaption><\/figure><\/p>\n<p>So how did I create such an amazing image? Let&#8217;s take a look at how you can generate images with Azure OpenAI and Azure SQL Database.<\/p>\n<h2>Image Generation with Azure OpenAI<\/h2>\n<p>The endpoint syntax for image generation is:<\/p>\n<pre>POST https:\/\/{your-resource-name}.openai.azure.com\/openai\/images\/generations:submit?api-version={api-version}<\/pre>\n<p>And you can add some options via the request body:<\/p>\n<div class=\"has-inner-focus\">\n<table class=\"table table-sm\" style=\"width: 99.9383%\" aria-label=\"Table 15\">\n<thead>\n<tr>\n<th style=\"width: 198px\">Parameter<\/th>\n<th style=\"width: 1423px\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"width: 198px\"><code>prompt<\/code><\/td>\n<td style=\"width: 1423px\">A text description of the desired image(s). The maximum length is 1000 characters. (Required)<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 198px\"><code>n<\/code><\/td>\n<td style=\"width: 1423px\">The number of images to generate. Must be between 1 and 5.<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 198px\"><code>size<\/code><\/td>\n<td style=\"width: 1423px\">The size of the generated images. Must be one of\u00a0<code>256x256<\/code>,\u00a0<code>512x512<\/code>, or\u00a0<code>1024x1024<\/code>.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>for example, here is a request body one could use to generate an image via the REST endpoint:<\/p>\n<div>\n<pre>{\r\n \"prompt\":\"A llama sitting on a bus in watercolor style\",\r\n \"size\": \"512x512\",\r\n \"n\": 3\r\n}<\/pre>\n<\/div>\n<\/div>\n<h2>Calling the Endpoint with Azure SQL Database<\/h2>\n<div>Putting this all together, we can create a call to the REST endpoint with the following code:<\/div>\n<div><\/div>\n<div>\n<pre>declare @url nvarchar(4000) = N'https:\/\/skynetbeta.openai.azure.com\/openai\/images\/generations:submit?api-version=2023-06-01-preview';\r\ndeclare @headers nvarchar(102) = N'{\"api-key\":\"10001000100010001000100\"}'\r\ndeclare @payload nvarchar(1000) = N'{\"prompt\":\"A llama sitting on a bus in watercolor style\",\"size\": \"512x512\",\"n\": 3}'\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 [image_id]\r\nFROM OPENJSON(@response,'$.result')\r\nWITH ([image_id] NVARCHAR(100) '$.id');<\/pre>\n<\/div>\n<div>The select statement will return the <strong>image-id<\/strong> which we will use to call the next REST endpoint.<\/div>\n<div>(example return payload)<\/div>\n<div><\/div>\n<pre>{ <span class=\"hljs-attr\">\"id\"<\/span>: <span class=\"hljs-string\">\"dhd873h-sss1-4b4b-5c5c-58ad7798ffXII\"<\/span>, <span class=\"hljs-attr\">\"status\"<\/span>: <span class=\"hljs-string\">\"notRunning\"<\/span> }<\/pre>\n<div><div class=\"alert alert-success\"><p class=\"alert-divider\"><i class=\"fabric-icon fabric-icon--Lightbulb\"><\/i><strong>The Status Field <\/strong><\/p>The status\u00a0field can be\u00a0&#8220;notRunning&#8221;\u00a0(task is queued but hasn&#8217;t started yet),\u00a0&#8220;running&#8221;,\u00a0&#8220;succeeded&#8221;,\u00a0&#8220;canceled&#8221;\u00a0(task has timed out),\u00a0&#8220;failed&#8221;, or\u00a0&#8220;deleted&#8221;.<\/div><\/div>\n<div><\/div>\n<div>The syntax of the REST endpoint that retrieves the generated image is as follows:<\/div>\n<div><\/div>\n<pre>GET https:\/\/{your-resource-name}.openai.azure.com\/openai\/operations\/images\/<strong>{image-id}<\/strong>?api-version={api-version}<\/pre>\n<div>Use the returned<strong> image Id<\/strong> from the first call, plug it into where the <strong>image-id<\/strong> parameter is within the syntax of the image retrieval REST endpint, and perform a <strong>GET<\/strong> as follows:<\/div>\n<div>(for this example, the image-id will be 12345678910112)<\/div>\n<div><\/div>\n<div>\n<pre>declare @url nvarchar(4000) = N'https:\/\/skynetbeta.openai.azure.com\/openai\/operations\/images\/12345678910112?api-version=2023-06-01-preview';\r\ndeclare @headers nvarchar(102) = N'{\"api-key\":\"10001000100010001000100\"}'\r\ndeclare @ret int, @response nvarchar(max);\r\nexec @ret = sp_invoke_external_rest_endpoint\r\n  \u00a0 @url = @url,\r\n  \u00a0 @method = 'GET',\r\n  \u00a0 @headers = @headers,\r\n  \u00a0 @timeout = 230,\r\n  \u00a0 @response = @response output;\r\nSELECT [url]\r\nFROM OPENJSON(@response,'$.result.result.data')\r\nWITH ([url] NVARCHAR(MAX) '$.url');<\/pre>\n<\/div>\n<div>And the select statement will return the URLs to where you can find the images. Just copy and paste them into your favorite browser! Here is an example of what was generated for the <strong>prompt<\/strong> &#8220;A llama sitting on a bus in watercolor style&#8221;:<\/div>\n<div><a href=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002.png\"><img decoding=\"async\" class=\"size-full wp-image-2440 aligncenter\" src=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002.png\" alt=\"Image generated 002\" width=\"512\" height=\"512\" srcset=\"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002.png 512w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002-300x300.png 300w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002-150x150.png 150w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002-24x24.png 24w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002-48x48.png 48w, https:\/\/devblogs.microsoft.com\/azure-sql\/wp-content\/uploads\/sites\/56\/2023\/09\/generated_002-96x96.png 96w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><\/div>\n<div><\/div>\n<div>What works of art can you generate with OpenAI and the Azure SQL Database? Send us examples! We would love to see them.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Following on the series of OpenAI posts I have done recently, here is a quick how-to on using DALL-E with External REST endpoint invocation in the Azure SQL Database. Using the OpenAI service in Azure, we can use the image generation REST endpoints to create new and wonderful images such as, if you remember, the [&hellip;]<\/p>\n","protected":false},"author":95874,"featured_media":2449,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,582,576,577],"tags":[510,465,469,583,30,561,410,34],"class_list":["post-2435","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure-sql","category-openai","category-rest","category-rest-endpoint-invocation","tag-azure-sql-database","tag-azuresql","tag-azuresqldb","tag-dall-e","tag-developers","tag-openai","tag-rest","tag-t-sql"],"acf":[],"blog_post_summary":"<p>Following on the series of OpenAI posts I have done recently, here is a quick how-to on using DALL-E with External REST endpoint invocation in the Azure SQL Database. Using the OpenAI service in Azure, we can use the image generation REST endpoints to create new and wonderful images such as, if you remember, the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/2435","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=2435"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/posts\/2435\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media\/2449"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/media?parent=2435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/categories?post=2435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/azure-sql\/wp-json\/wp\/v2\/tags?post=2435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}