Skip to main content

Notifications

Community site session details

Community site session details

Session Id :

Integrating ChatGPT with Power Automate Desktop

VJR Profile Picture Posted 13 Feb 2023 by VJR 7,635

How to integrate ChatGPT with Power Automate Desktop

 

1. You will need a token key for calling the OpenAI API.

 

How to get this key is explained below:

  •  Create a key on the below link using the given button. Please register to the website if you haven't already.

 

VJR_0-1674360880941.png

https://beta.openai.com/account/api-keysAccount API Keys - OpenAI API

 

 

2. Use the Invoke Web service action to call the API 

VJR_1-1674361174037.png

 

Add each of the parameters as below:

i. URL: https://api.openai.com/v1/completions

 

Be noted that the URLs keep changing at times.

Get the latest URLs from here - https://beta.openai.com/docs/api-reference/completions/create

 

ii. Method: POST

 

iii. Accept: application/json

 

iv. Content Type: application/json

 

v. Custom Headers: 

    Authorization: Bearer <YourAPITokenHere>

 

    Replace <YourAPITokenHere> with your token key from Step 1. Remove the angular brackets.

 

vi. Request body:

     

{
"model": "text-davinci-003",
"prompt": "%UserInput%",
"max_tokens": 100,
"temperature": 0,
"top_p": 1,
"n": 1,
"stream": false,
"logprobs": null,

"stop": null
}

 

Where to get these Json parameters from:https://beta.openai.com/docs/api-reference/completions/create

VJR_2-1674361636175.png

 

  • Please see in the Json parameters that we are going to pass the user input via the %UserInput% variable asked in Step No. 3.i below
  • Also, I have taken the default value "stop": null instead of stopping at a newline character "stop": "\n" 
  • Also, I have increased the max_tokens to 100 to get longer answers. 

 

The explanation for the meaning of each of the Json parameters are given in the same link here -> API Reference - OpenAI API

Example: 

VJR_3-1674362164549.png

 

 

vii. Encode Request Body (Important)

     One more important setting is required else the API call does not work.

    

     In the Advanced section of the Invoke Web service, disable the Encode request body.

 

    VJR_4-1674362400409.png

 

 

Here is a screenshot after entering all the values.

 

VJR_7-1674362695376.png

 

 

3. Entire Flow at a glance

 

VJR_8-1674362900677.png

 

i. Display Input Dialog to ask the user for an Input

 

VJR_9-1674362954836.png

 

ii. The API should not be unnecessarily called just in case the user pressed the Cancel button in the Input dialog box.

 

VJR_10-1674363013600.png

 

iii. Invoke Web service - Already complete as per Step No. 2 above

 

VJR_7-1674362695376.png

 

 

iv. The output of the Invoke Web service will be a Json object which needs to be converted to a Custom Object so that the answer from ChatGPT can be easily extracted.

 

VJR_11-1674363227752.png

 

 

v. Final answer from the ChatGPT prefixed along with the original question that the user has asked.

 

Question: %UserInput%
%JsonAsCustomObject['choices'][0].text%

 

VJR_12-1674363338099.png

 

 

 

Why %JsonAsCustomObject['choices'][0].text%

 

VJR_13-1674364136938.png

 

4. Code for the entire Flow

  Copy-paste the below into a blank Flow editor, make changes to the API parameters and run the Flow.

  

 

 

Display.InputDialog Title: $'''Please enter a question''' InputType: Display.InputType.Multiline IsTopMost: False UserInput=> UserInput ButtonPressed=> ButtonPressed
IF ButtonPressed <> $'''Cancel''' THEN
 Web.InvokeWebService.InvokeWebService Url: $'''https://api.openai.com/v1/completions''' Method: Web.Method.Post Accept: $'''application/json''' ContentType: $'''application/json''' CustomHeaders: $'''Authorization: Bearer <TokenKeyHere>''' RequestBody: $'''{
 \"model\": \"text-davinci-003\",
 \"prompt\": \"%UserInput%\",
 \"max_tokens\": 100,
 \"temperature\": 0,
 \"top_p\": 1,
 \"n\": 1,
 \"stream\": false,
 \"logprobs\": null,
 \"stop\": null
 }''' ConnectionTimeout: 60 FollowRedirection: True ClearCookies: False FailOnErrorStatus: False EncodeRequestBody: False UserAgent: $'''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.21) Gecko/20100312 Firefox/3.6''' Encoding: Web.Encoding.AutoDetect AcceptUntrustedCertificates: False ResponseHeaders=> WebServiceResponseHeaders Response=> WebServiceResponse StatusCode=> StatusCode
 Variables.ConvertJsonToCustomObject Json: WebServiceResponse CustomObject=> JsonAsCustomObject
 Display.ShowMessageDialog.ShowMessage Title: $'''Response from ChatGPT''' Message: $'''Question: %UserInput%
%JsonAsCustomObject['choices'][0].text%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False ButtonPressed=> ButtonPressed2
END

 

 

 

How to Copy-paste the above code in Power Automate Desktop:

Copy Paste Desktop Flows.gif

 

Some key points:

 

  • For a free account, a certain amount of free credit amount is given to you. For every API call made, these free credits will be reduced. You can check the usage of your account here -> https://beta.openai.com/account/usage
  • There is an expiry to the free credits given and the expiry date is visible on the above usage link.
  • If you are getting short or incomplete answers, then increase the max_tokens in the Json parameters. But be noted that the account credits will be reduced as per the tokens used in the API call.

 

 

Final Output:

 

VJR_14-1674364979763.png

 

Categories:

Desktop flows

Comments

  • VJR Profile Picture VJR 7,635
    Posted 11 Jun 2023 at 03:09:19
    Integrating ChatGPT with Power Automate Desktop

    Hi @asancho10 , were you able to have your issue fixed?

    I got this error multiple times and the only way it got fixed was by playing around with the content inside the input text.

    For example, I had to remove the double quotation mark " from the input text, 

    So, try to identify what text or character is causing this issue.

    Also make sure you have given the sufficient token size.

     

  • asancho10 Profile Picture asancho10
    Posted 01 Jun 2023 at 14:52:40
    Integrating ChatGPT with Power Automate Desktop

    I followed all the steps but I am getting this WebServiceResponse:

     

    "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)"

  • GirishV_1987 Profile Picture GirishV_1987
    Posted 04 Apr 2023 at 10:45:50
    Integrating ChatGPT with Power Automate Desktop

    Hi All,

     

    I am giving prompt as What is RPA?, however i am getting web response as below,

    package com.example.demo.controller;

    import com.example.demo.model.User;
    import com.example.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;

    import java.util.List;

    @RestController
    @RequestMapping("/user")
    public class UserController

     

    could you please help if any issue in configuration.

  • VJR Profile Picture VJR 7,635
    Posted 13 Feb 2023 at 05:30:35
    Integrating ChatGPT with Power Automate Desktop

    Hello @Lagloire 

    Since you asked about other use cases for Power Automate Desktop + ChatGPT, here are some posts I have added

     

    𝗟𝗶𝗻𝗸 𝘁𝗼 𝗨𝘀𝗲 𝗖𝗮𝘀𝗲 1: https://lnkd.in/g7EabqbN

    𝗟𝗶𝗻𝗸 𝘁𝗼 𝗨𝘀𝗲 𝗖𝗮𝘀𝗲 2: https://lnkd.in/gzHreRP6

    𝗟𝗶𝗻𝗸 𝘁𝗼 𝗨𝘀𝗲 𝗖𝗮𝘀𝗲 3: https://www.linkedin.com/posts/activity-7030763429086838784-xtVb

  • baoshaojing Profile Picture baoshaojing
    Posted 09 Feb 2023 at 02:10:45
    Integrating ChatGPT with Power Automate Desktop

    Thank you

  • VJR Profile Picture VJR 7,635
    Posted 25 Jan 2023 at 02:48:56
    Integrating ChatGPT with Power Automate Desktop

    Thats good to know it is working now. OpenAI ChatGPT has its own community where people post use cases developed out of it...Also LinkedIn has posts about how people are making use of it.

  • Lagloire Profile Picture Lagloire 86
    Posted 24 Jan 2023 at 15:46:50
    Integrating ChatGPT with Power Automate Desktop

    @VJR Thank you.

    With new email adress, i generate new secret code and it's working now. Thank you for this support. Did you make any other use case? Example, creating a chatbot? Do you know any other place where people post GPT uses case?

     

    Regards

  • VJR Profile Picture VJR 7,635
    Posted 24 Jan 2023 at 14:57:13
    Integrating ChatGPT with Power Automate Desktop

    If this is the error then you won't be seeing the choices in the data table. 

    I believe with a different email address you may get a new quota.. Don't know what changes they have made recently after declaring the paid plan.

     

    By the way how much quota you had earlier? You can check by the usages link I have shared earlier.

    Have sent you a private message. Let's connect there to avoid back and forth on this page. Let me know what you come up with on all the above questions. 

  • Lagloire Profile Picture Lagloire 86
    Posted 24 Jan 2023 at 14:40:36
    Integrating ChatGPT with Power Automate Desktop

    @VJR No i didn't enter a new line after the word "Bearer"

     

    Below on image what i have as output variables coming from the invoke web service. After making many tests to make it work, i exceeded my current quota. So, i have to take OpenAI pro plan? $42/month?

     

     

    Lagloire_0-1674571081961.png

     

  • VJR Profile Picture VJR 7,635
    Posted 24 Jan 2023 at 12:43:11
    Integrating ChatGPT with Power Automate Desktop

    @Lagloire, it is not clear from a masked screenshot, but did you enter a new line after the word "Bearer"? If yes then correct that. The token needs to be after a space right after the word Bearer.

     

    Also I am awaiting for you to show the output variables coming from the invoke web service.