Power Automate Documentation by GPT
Tags: powerautomate, aibuilder, powerplatform, gpt3
Published: 2023-07-24 06:54:09

If there is one thing we can all agree on is how much we hate doing documentation. No matter how useful it is, it a pain to complete (Its one of the reasons I made my auto document chrome extensions [AutoReview](https://chrome.google.com/webstore/detail/autoreview-for-power-auto/laaendfpgmhjilhjkbebekgdgfjaajif).

Recently I have been exploring the `Create text with GPT` Power Automate connector, and it gave me an idea, could we use a GPT model to automatically create documentation for our flows.

If you want to know more about the connector check out my blog [here](https://dev.to/wyattdave/new-power-automate-gpt-connector-58g7), in a nutshell my plan is to get the flow definition, pass it to the GPT connector to get a summary of what it does. Additionally, I want to see a simple graphical representation of what it does.

For this explanation I'm going to use a standard template, the Create a task in Planner from Microsoft Forms and post message in Teams.
![Create a task in Planner from Microsoft Forms and post message in Teams](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fa55t5kw60ef2w2lbt4.png)

---

Im going to split the process into 4 sections

1. Get the definition and prepare the data
2. GPT Prompts
3. Creating Report
4. Additional Features

## 1. Get the definition and prepare the data
First we need to get the flow definition, we use the `Get Flow` action from the Flow Managment connector, one of its outputs is the definition. The definition is the key blueprint to all flows, and has all the key information about the flow, and the primary data we need.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tmpa5mwfsideqxcirb6.png)

But the connector also provides some additional useful information,

- Actions Array - all the actions (5 in the demo)
- Connection References - all the connections (Forms, Teams & Planner in our example) 

Because flows can get long and because we have a token (character limit) on our GPT action I only want the API actions (so no conditions, variables or scope, just the actions like Get items or Send Email).
We do this with a simple filter and select only the type's that are 'OpenApiConnection'.

![filter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s2cjpvguk106pxjdcol8.png)

We then simply use the `Create HTML Table` actions to create a table of the filtered actions and a table of the connection references.

![html tables](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8lfxqxm816hrvsx4i4e.png)

## 2. GPT Prompts
We need to use few GPT prompts (so be aware when this comes out of preview the report will cost a few AI Builder credits). The first prompt is probably the most powerful, we use it to summarise what the flow does.

![summarise flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s0tdvyjm3imczxe67sw2.png)

In our demo we get:

> The power automate definition is a workflow definition that triggers an action when a new response is submitted. It then gets the response details, creates a task, and posts a message in a chat or channel. If the task creation fails, the workflow is terminated.

Pretty cool 😎

We could stop there, but I want to add something extra cool, a sequence diagram. To do that we have to use 2 GPT prompts (due to the token limit). First we have to extract all the actions that are 'OpenApiConnection'


![get api actions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lstrquinxv3cbu8h906.png)
_You may notice this sounds like what we did to the actions array, but that didn't include action name and it is in the wrong order_

```
[
  {
    "name": "Get_response_details",
    "type": "OpenApiConnection"
  },
  {
    "name": "Create_a_task",
    "type": "OpenApiConnection"
  },
  {
    "name": "Post_message_in_a_chat_or_channel",
    "type": "OpenApiConnection"
  }
]
```

Next we ask GPT to create our mermaid sequence diagram

![create sequence diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x83xwmpigqxpky9nnf6d.png)
_You could do different mermaid diagrams, or even different models. Mermaid is a powerful free JavaScript library that creates process diagrams_

> sequenceDiagram
  Get_response_details->>Create_a_task: Request
  Create_a_task->>Post_message_in_a_chat_or_channel: Request
  Post_message_in_a_chat_or_channel-->>Get_response_details: Response
  Create_a_task-->>Get_response_details: Response
  Post_message_in_a_chat_or_channel-->>Create_a_task: Response

## 3. Creating Report
To create the report Im going to follow a standard process:

- Create html template
- Get File Content
- Replace place holders with new data
- Save new file

You can create any template you want, I went with Bootstrap, just remember if you are using mermaid to add the JavaScript import

![template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/um2aeoqmus20ygsy05p5.png)

I use placeholders like {name} and {table}, which I then use a replace expression to replace with the GPT or HTML table outputs.

![update template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xz97580d20dxegu6b932.png)

```
replace(
    replace(
        replace(
            replace(
                replace(
                    replace(
                        replace(body('Get_file_content_template'),
                            '{name}'
                        ,
                            outputs('Get_Flow')?['body/properties/displayName']
                        )
                    ,
                        '{author}'
                    ,
                        outputs('Get_Flow')?['body/properties/creator/userId']
                    )
                ,
                    '{description}'
                ,
                    outputs('Create_text_with_GPT_description')?['body/responsev2/predictionOutput/text']
                )
            ,
                '{table}'
            ,
                body('Create_HTML_table_actions')
            )
        ,
            '{diagram}'
        ,
            concat('sequenceDiagram',split(outputs('Create_text_with_GPT_diagram')?['body/responsev2/predictionOutput/text'],'sequenceDiagram')[1])
        )
    ,
        '{date}'
    ,
        formatDateTime(utcNow(),'dd/MM/yyyy')
    )
,
    '{tableCon}'
,
    body('Create_HTML_table_connections')
)
```
And the output file will look like this:

![report](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inoncouo9qp9ehrxr1f6.png)
_Teamplates can be download [here](https://github.com/wyattdave/Power-Platform/tree/main/Power%20Automate%20Artifacts/GPT%20Flow%20Documentation)_

**Full Flow**
![full flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ntquhjwfbubozvlqk2ef.png)

## 4. Additional Features
As you can see you are only limited by your imagination, but there are a couple of things I considered adding:

**Classify**
The GPT has a cool classification prompt, so you could create different categories and ask it to categorise the flow.

![classify](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9ucaimul7yttzspc66n.png)

**Inventory Default Environment**
The flow is a manual flow, but it could be called by another flow listing all the flows in the default environment, the description and other data could also be stored in a list for easier reviewing.

**Compress Definition**
As we have a token limit some long flows may hit that limit, so I wrote an office script to strip out unneeded information from the definition first.

![office script](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26fsdq6hv6zqli6ucptz.png)

---

**Further Reading**

- [https://learn.microsoft.com/en-us/connectors/flowmanagement/#get-flow](https://learn.microsoft.com/en-us/connectors/flowmanagement/#get-flow)
- [Mermaidchart.com](https://www.mermaidchart.com/)
- [Mermaid Online Editor](https://mermaid.live/edit#pako:eNqNkLEKAjEMhl_lyKwvcIOLgqvoJoUSrj96aNOzyQ1y-O62qIMo6JQm-b8_aSbqUgC1pLiMkA6rng-Zo5OmWcN8hg5JFD7AuD_rfLFYZrDBszfWU9tsK6hWgbdOUW6Smo9Q5QN8L6XRHdl8yjWK4PxG_1TPi-W3narLo_K5xF_IX5M_vv3gaUYROXIfyhGn6ubIjohw1JZn4Hxy5ORWdDxa2l2lo9byiBmNQyiOz4O_igPLPqVnersDZxuYcQ)
- [Power Automate Office Scripts](https://dev.to/wyattdave/how-to-master-power-automate-scripts-4o8j)