Generate images with Azure OpenAI Service (DALL-E) and Azure SQL Database
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 cat in the data center.
So how did I create such an amazing image? Let’s take a look at how you can generate images with Azure OpenAI and Azure SQL Database.
Image Generation with Azure OpenAI
The endpoint syntax for image generation is:
POST https://{your-resource-name}.openai.azure.com/openai/images/generations:submit?api-version={api-version}
And you can add some options via the request body:
Parameter | Description |
---|---|
prompt |
A text description of the desired image(s). The maximum length is 1000 characters. (Required) |
n |
The number of images to generate. Must be between 1 and 5. |
size |
The size of the generated images. Must be one of 256x256 , 512x512 , or 1024x1024 . |
for example, here is a request body one could use to generate an image via the REST endpoint:
{ "prompt":"A llama sitting on a bus in watercolor style", "size": "512x512", "n": 3 }
Calling the Endpoint with Azure SQL Database
declare @url nvarchar(4000) = N'https://skynetbeta.openai.azure.com/openai/images/generations:submit?api-version=2023-06-01-preview'; declare @headers nvarchar(102) = N'{"api-key":"10001000100010001000100"}' declare @payload nvarchar(1000) = N'{"prompt":"A llama sitting on a bus in watercolor style","size": "512x512","n": 3}' declare @ret int, @response nvarchar(max); exec @ret = sp_invoke_external_rest_endpoint @url = @url, @method = 'POST', @headers = @headers, @payload = @payload, @timeout = 230, @response = @response output; SELECT [image_id] FROM OPENJSON(@response,'$.result') WITH ([image_id] NVARCHAR(100) '$.id');
{ "id": "dhd873h-sss1-4b4b-5c5c-58ad7798ffXII", "status": "notRunning" }
The Status Field
The status field can be “notRunning” (task is queued but hasn’t started yet), “running”, “succeeded”, “canceled” (task has timed out), “failed”, or “deleted”.
GET https://{your-resource-name}.openai.azure.com/openai/operations/images/{image-id}?api-version={api-version}
declare @url nvarchar(4000) = N'https://skynetbeta.openai.azure.com/openai/operations/images/12345678910112?api-version=2023-06-01-preview'; declare @headers nvarchar(102) = N'{"api-key":"10001000100010001000100"}' declare @ret int, @response nvarchar(max); exec @ret = sp_invoke_external_rest_endpoint @url = @url, @method = 'GET', @headers = @headers, @timeout = 230, @response = @response output; SELECT [url] FROM OPENJSON(@response,'$.result.result.data') WITH ([url] NVARCHAR(MAX) '$.url');
0 comments