#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

Last post I wrote on how to OpenAI Image generation API. Today, I’m going to show how to Text Completions using Azure OpenAI Services.

Note: This sample applies to both, ChatGPT (preview) and GPT-4 (preview) with Azure OpenAI Service.

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 follow the steps from the Quickstart: Get started using ChatGPT (preview) and GPT-4 (preview) with Azure OpenAI Service tutorial. We will also get request and response JSON format information from OpenAI Chat Completion Scenario.

And then we need to save:

  • Azure OpenAI Model Key
  • Azure OpenAI Model Endpoint

The endpoint should be like this url

<YOUR ENDPOINT>/openai/deployments/<YOUR MODEL NALE>/chat/completions?api-version=2023-03-15-preview

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

{
  "model": "gpt-3.5-turbo",
  "messages": [
    {
      "role": "system",
      "content": ""
    },
    {
      "role": "user",
      "content": "MESSAGE PROMPT"
    }
  ]
}

Let’s understand the JSON message with the official documentation.

Chat models take a series of messages as input, and return a model-generated message as output. Although the chat format is designed to make multi-turn conversations easy, it’s just as useful for single-turn tasks without any conversations. Messages must be an array of message objects, where each object has a role (either “system”, “user”, or “assistant”) and content (the content of the message). Conversations can be as short as 1 message or fill many pages.

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

Sample image with the information in the post method.
  • Method: POST.
  • We use the endpoint from the Azure Tutorial.
  • In Headers we define
    • Content-Type as application/json.
    • api-key with the API Key from Azure.
  • In the Body, I use the previous JSON demo, and create our roles and content.
  • Optional:
    • Authentication is BASIC.
    • Username is a space character (is required)
    • Password is the Azure Model 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.

{
  "id": "chatcmpl-73sTMr0P42ayyOUYR8HPPbLhxKZxx",
  "object": "chat.completion",
  "created": 1681158732,
  "model": "gpt-35-turbo",
  "choices": [
    {
      "index": 0,
      "finish_reason": "stop",
      "message": {
        "role": "assistant",
        "content": "Dear sister,\n\nI just wanted to take a moment to thank you for inviting me to your birthday bash. It was an absolute blast, and I had so much fun pretending to be a rockstar on the karaoke machine.\n\nI mean, let's be real, we both know I can't sing to save my life. But thanks to you, I was able to live out my dream of being a superstar for a night. And if anyone tells you that I was off-key, just tell them that I was going for the owl-like screech of a dying animal.\n\nBut honestly, sis, thank you for including me in your special day. It means the world to me, and I wouldn't have wanted to celebrate your birthday any other way. I promise I'll return the favor when it's my turn to blow out the candles. Just don't expect me to sing, because we both know how that'll turn out.\n\nLove you to the moon and back,\n\n[Your name]"
      }
    }
  ],
  "usage": {
    "completion_tokens": 202,
    "prompt_tokens": 49,
    "total_tokens": 251
  }
}

Parse JSON to get the final result

Now it’s time to a 2nd JSON Parse. The previous response contains a collection of generated CHOICES. 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.

{
  "index": 0,
  "finish_reason": "stop",
  "message": {
    "role": "assistant",
    "content": "Dear sister,\n\nI just wanted to take a moment to thank you for inviting me to your birthday bash. It was an absolute blast, and I had so much fun pretending to be a rockstar on the karaoke machine.\n\nI mean, let's be real, we both know I can't sing to save my life. But thanks to you, I was able to live out my dream of being a superstar for a night. And if anyone tells you that I was off-key, just tell them that I was going for the owl-like screech of a dying animal.\n\nBut honestly, sis, thank you for including me in your special day. It means the world to me, and I wouldn't have wanted to celebrate your birthday any other way. I promise I'll return the favor when it's my turn to blow out the candles. Just don't expect me to sing, because we both know how that'll turn out.\n\nLove you to the moon and back,\n\n[Your name]"
  }
}

And that’s it, after these three steps we can get the response from a GPT model. And we can use it in our Power Automate Flows!

In the next post, I’ll show another scenarios using OpenAI and Azure OpenAI Services.

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

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


Advertisement

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: