Use ChatGPT in MS PowerAutomate Flows

Learn how ChatGPT can automatically categorise your e-mails
February 1, 2024
Steffen Huß

Integrating ChatGPT with MS PowerAutomate unlocks a wide range of use cases: You automatically can

  • classify texts,
  • extract text fragments,
  • reformulate,
  • translate,

and much more in a seamless end-to-end flow. The approach described here takes a couple of steps, but all of them are easy to execute and do not require coding skills. As a small example, we build a service that automatically classifies incoming requests into categories so you get some insights into which requests are most common. Of course, this example has merely illustrative purposes and you can make the transfer to use cases you are interested in.

Anyone with a PowerAutomate Premium license can implement this method without relying on external app developers. Note that since December 2023 PowerAutomate offers an AI Builder to prompt from within PowerAutomate as well; however, it is only available in certain regions and currently limited to GPT-3.5. The method described here is a little more flexible.

1. Create an OpenAI account and add credit

Visit the OpenAI homepage and log in or register. We'll be utilising the OpenAI API; if you've never used APIs, don't fret - it's simple for this application. However, since the API isn't free, make sure to add some credit here. $5 should suffice for ample testing. For safety, consider disabling auto-recharging during your tests.

2. Create an OpenAI API key

Navigate to the “API keys” section and generate a new API key for experimentation. Copy this API key to a safe place only you have access to. Do not expose the API key to others.However, bear in mind you can always regenerate or revoke API keys if you suspect any misuse.

3. Create a workflow with a prompt

  • Return to Microsoft PowerAutomate and draft a workflow. For demonstration purposes, we'll set up a workflow to classify incoming emails — just one of a myriad of potential applications.
  • After your workflow's initial trigger, incorporate an Initialize variable Action. Label it “Create Prompt”, opt for type “String”, and in the value slot, insert your prompt.
  • Ideally, your prompt should reference dynamic content like the email's text, and it should describe the task you expect ChatGPT to perform on this content. For our email classification example, a prompt may look like this:

Description

Your task is to classify emails into categories. These emails are often, but not always requests that a lawyer is supposed to work on.

I will give you a description of categories and an email, and you will decide which category this email belongs to. Note that one email can only belong to one category.

Categories

Contract review: A request to review an incoming contract draft, mainly to identify legal risks.

Contract creation: A request to draft a certain contract type to be sent out to third-parties.

Legal advice: A request asking for legal advice on which behaviour is appropriate or beneficial in a certain situation.

Data breach: A request describing a specific potential data breach and usually asking for proper next steps.

Other request: Any email that does not fit into one of the categories above, but that is a legal request.

No request: Every email that is not a request.

Email to classify

Subject: {Reference the Subject-field from the incoming email here}

Body: {Reference the Body-field from the incoming email here}

Please classify it as one of the above categories. Please only say the category, nothing else. Please answer in one word.

4. Send a request to OpenAI

Next, we send our prompt to OpenAI. It will leave your servers, but it is worth knowing that OpenAI does not employ data sent via the API for model training! Here is the process:

  • Inject an HTTP Action to your workflow (this requires the Premium subscription). Name it “Send to ChatGPT”. Note the Action type name is “HTTP” without further addition; you may need to scroll to find it.
  • Select the POST Method and input “https://api.openai.com/v1/chat/completions” into the URI space
  • Set up a Header with “Authorization” to the left and “Bearer YOUR_API_KEY” to the right, substituting YOUR_API_KEY with the key from step 2. For security reasons you should set up an environment variable to store your API keys and reference the variable. For pure testing purposes, you may also directly paste in your API key.
  • Introduce another Header with “Accept” on the left and “application/json” on the right.
  • Populate the Body section with the code below, swapping YOUR_PROMPT with the prompt variable from step 3. For testing, we recommend using the “gpt-3.5-turbo” model, but for more complex tasks, “gpt-4” really makes a difference (explore this link to discover alternate models).In our example, we have set the length of the answers from ChatGPT to 500 tokens maximum. This way, we control the cost, but depending on your use case, it might make sense to increase it.

{
 "model": "gpt-3.5-turbo",
 "messages": [
   {
     "role": "user",
     "content": "YOUR_PROMPT"
   }
 ],
 "max_tokens": 500
}

5. Parse JSON

OpenAI's response will encapsulate more than just the answer to your prompt; it'll include supplementary details in a format called JSON. We need to distill the exact response. Here's the method:

  • Add a Parse JSON Action and name it “Parse answer JSON from ChatGPT”.
  • In the Content field, insert the Body of the previous “Send to ChatGPT” Action using dynamic content. This means that from this text, you want to extract the variable.
  • In the Schema, you need to specify how the answer format from OpenAI looks like. To keep things simple, just copy/paste the snippet below

{
   "type": "object",
   "properties": {
       "id": {
           "type": "string"
       },
       "object": {
           "type": "string"
       },
       "created": {
           "type": "integer"
       },
       "model": {
           "type": "string"
       },
       "choices": {
           "type": "array",
           "items": {
               "type": "object",
               "properties": {
                   "index": {
                       "type": "integer"
                   },
                   "message": {
                       "type": "object",
                       "properties": {
                           "role": {
                               "type": "string"
                           },
                           "content": {
                               "type": "string"
                           }
                       }
                   },
                   "finish_reason": {
                       "type": "string"
                   }
               },
               "required": [
                   "index",
                   "message",
                   "finish_reason"
               ]
           }
       },
       "usage": {
           "type": "object",
           "properties": {
               "prompt_tokens": {
                   "type": "integer"
               },
               "completion_tokens": {
                   "type": "integer"
               },
               "total_tokens": {
                   "type": "integer"
               }
           }
       }
   }
}

6. Select the correct item

In step 5 we have sorted the information in the JSON returned by OpenAI. Now we want to pick the information we need (the actual completion to our prompt).

  • Add a Compose Action and name it “GPT completion”
  • Click the $f_x$ icon that displays when you click into the input field. A bubble displays.
  • Paste the code below into the input field of the bubble. You may replace the term “body(‘Parse_answer_JSON_from_ChatGPT’) with the name of the node created in step 5. Use the tab “Dynamic content” for this and select Body for the node created in step 5.
  • first(body('Parse_answer_JSON_from_ChatGPT')?['choices'])['message']['content']


7. You are done!

You now can use the Outputs of the node created in step 6 to work with the ChatGPT completion of your question. In our example, we can store the emails along with their receiver, subject and classification in an Excel file using the “Add a row into a table” node.

Now in our example, in the Excel sheet we can analyse the incoming requests:

Discover more AI use cases for your organisation with our AI Consultancy services >

Weitere Informationen

Alle anzeigen
The four NodeMasters founders stood together in front of a brick wall

So können wir helfen

Ob erstes Digitalisierungsprojekt oder kompetente Unterstützung mit bestehenden Technologien – wir helfen gerne!