07-06-2024 16:14 PM
Use-Case:
While Power Automate can add two numbers, it does not have an in-built functionality to add an array of numbers. For example, if you have a SharePoint column containing various numbers, then adding the numbers within Power Automate is not an out of box functionality.
Solution:
We will create a custom AI prompt that we will use in Power Automate. We will provide information to process different scenarios and get output in a JSON object format.
1 - Create a new AI Builder Prompt and for this demo, we will name it as Sum of Array
2 - Create a new Input variable under Prompt Settings, we will name it as InputArray. Note that there is no option to specify the data type of the variable and it is assumed to be string.
3 - While within the Prompt Settings, chose output as JSON.
4 - You can leave the Model at default for this use-case.
5 - Under the Prompt box, add the following text:
Add the numbers provided in the array @{InputArray}. Convert all numbers to the same format before summation. If the array contains both integers and floats, convert all integers to floats before summation. If all numbers are already integers or float, do not convert them. If the sum is successful: Respond with a JSON object containing the sum, an error flag set to false, and an error message of "None", for example: {"sum": 23, "error":false, "error_message":"None"}. If the sum is unsuccessful, for reasons such as input array contain invalid values such as words or input is not an array, then inform accordingly in a json object, for example: {"sum": null, "error": true, "error_message":"Invalid values found in the input array. One of the invalid value is Eight"}. If the input is not an array then make sure error_message is provided accordingly for example: {"sum": null, "error": true, "error_message":"Invalid values. Input is not an array"}. In case of other failure modes, provide error_message accordingly.
Replace @{InputArray} with your input parameter.
6 - Enter sample data under Input parameter and then click on Test Prompt. You may want to try different variations to ensure the prompt is working as expected.
Use Custom Prompt in Power Automate
1 - We are assuming that you have an array of numbers that you want to sum up. This could be through a column in your datasource or other methods.
2 - Add "Create text with GPT using a prompt" action. Choose 'Sum of Array' as custom prompt from drop down. Under the 'InputArray' parameter, enter the array of numbers that you want to sum up. You can leave "AdditionalContext" as blank.
3 - You may get a warning because as part of responsible AI, you should have a human approval step to review the output of AI generated content. You can skip that for this demo.
4 - Run the flow and examine the results.
Demo
I tried the above with three inputs
[2, 3.2, 4]
[one, two, 2, 4.5]
this is deliberately a text and not an array
In all three cases, I received the expected outputs as shown in screenshot below: