September 25th, 2023

Generate images with Azure OpenAI Service (DALL-E) and Azure SQL Database

Brian Spendolini
Senior Product Manager, 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.

Image cat1
(He’s back!!!! Meow??)

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 256x256512x512, 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

Putting this all together, we can create a call to the REST endpoint with the following code:
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');
The select statement will return the image-id which we will use to call the next REST endpoint.
(example return payload)
{ "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”.
The syntax of the REST endpoint that retrieves the generated image is as follows:
GET https://{your-resource-name}.openai.azure.com/openai/operations/images/{image-id}?api-version={api-version}
Use the returned image Id from the first call, plug it into where the image-id parameter is within the syntax of the image retrieval REST endpint, and perform a GET as follows:
(for this example, the image-id will be 12345678910112)
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');
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 prompt “A llama sitting on a bus in watercolor style”:
Image generated 002
What works of art can you generate with OpenAI and the Azure SQL Database? Send us examples! We would love to see them.

Author

Brian Spendolini
Senior Product Manager, Azure SQL Database

0 comments

Discussion are closed.