DEV Community

El Bruno for Microsoft Azure

Posted on • Originally published at elbruno.com on

#PowerAutomate – Using OpenAI APIs, like ChatGPT, Dall-E and more. Also in Azure!

Hi!

Back in March we hosted 3 sessions, and we showed how to use OpenAI models and features in Power Apps, Power Automate and Power Virtual Agents. You can check the recordings here: https://aka.ms/OpenAIPowerPlatformSeries

These AI models are available in several places, like OpenAI APIs and Azure OpenAI Services.

Scenario

Let’s learn how to use one of these models in a Power Automate Flow using an HTTP Request Action and processing the JSON Response.

For this 1st demo, we’ll use:

In the official documentation we can learn the specific format that we need to use to send a POST HTTP Request to the service. In example


{
  "prompt": "A cute puppy playing soccer in the moon, looking at the camera, cartoon style",
  "n": 1,
  "size": "512x512"
}

Enter fullscreen mode Exit fullscreen mode

And the main parameters are

  • prompt: the prompt to generate the image.
  • n: the numbers of images to be generated.
  • size: the size of the generated image.

Super easy! Now let’s add this into a Power Automate flow. I do this in three steps. Like this:

3 actions in a flow to make the http requesto to the image generator api. The 3 steps are: HTTP REQUIEST, Parse JSON and Parse JSON.

Let’s take a look at some specifics for each action.

HTTP Request

  • Method: POST.
  • We use the API endpoint for the Image Generation.
  • In Headers we define Content-Type as application/json.
  • In the Body, I use the previous JSON demo, and replace the prompt with a flow variable.
  • Authentication is BASIC.
  • Username is a space character (is required)
  • Password is the OpenAI Key.

Parse JSON from the HTTP Request

Now it’s time to parse the body output from the previous step.

I use this sample JSON response to generate the schema.


{
  "created": 123456,
  "data": [
    {
      "url": "https://imageurl"
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode

Parse JSON to get the final result

Now it’s time to a 2nd JSON Parse. The previous response contains a collection of generated images in DATA. And even if we only requested one, we need to parse this collection.

We can use the first() function to get the first element of the response, and then parse the JSON to get the final results.

I use this sample JSON to generate the Schema.


    {
      "url": "https://imageurl"
    }

Enter fullscreen mode Exit fullscreen mode

And that’s it, after these three steps we can get an url to a generated image. And we can use it in our Power Automate Flows!

In the next post, I’ll show a similar scenario using Azure OpenAI Services.

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Top comments (0)