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"
}
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:
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"
}
]
}
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"
}
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)