Skip to main content
Power Automate
    • Connectors
    • Templates
    • Take a guided tour
    • Digital process automation
    • Robotic process automation
    • Business process automation
    • Process advisor
    • AI Builder
  • Pricing
  • Partners
    • Blog
    • Documentation
    • Roadmap
    • Self-paced learning
    • Webinar
    • Business process and workflow automation topics
    • Overview
    • Issues
    • Give feedback
    • Overview
    • Forums
    • Galleries
    • Submit ideas
    • User groups
    • Register
    • ·
    • Sign in
    • ·
    • Help
    Go To
    • Microsoft Power Automate Community
    • Welcome to the Community!
    • News & Announcements
    • Get Help with Power Automate
    • General Power Automate Discussion
    • Using Connectors
    • Building Flows
    • Using Flows
    • Power Automate Desktop
    • Process Advisor
    • AI Builder
    • Power Automate Mobile App
    • Translation Quality Feedback
    • Connector Development
    • Power Platform Integration - Better Together!
    • Power Platform Integrations
    • Power Platform and Dynamics 365 Integrations
    • Galleries
    • Community Connections & How-To Videos
    • Webinars and Video Gallery
    • Power Automate Cookbook
    • Events
    • 2021 MSBizAppsSummit Gallery
    • 2020 MSBizAppsSummit Gallery
    • 2019 MSBizAppsSummit Gallery
    • Community Engagement
    • Community AMA
    • Community Blog
    • Power Automate Community Blog
    • Community Support
    • Community Accounts & Registration
    • Using the Community
    • Community Feedback
    cancel
    Turn on suggestions
    Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.
    Showing results for 
    Search instead for 
    Did you mean: 
    • Microsoft Power Automate Community
    • Galleries
    • Power Automate Cookbook
    • Integrating ChatGPT with Power Automate Desktop

    Integrating ChatGPT with Power Automate Desktop

    01-21-2023 21:41 PM - last edited 02-12-2023 21:22 PM

    Multi Super User VJR
    Multi Super User
    7038 Views
    LinkedIn LinkedIn Facebook Facebook Twitter Twitter
    VJR
    Multi Super User VJR
    Multi Super User
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    Integrating ChatGPT with Power Automate Desktop

    ‎01-21-2023 09:41 PM

    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

     

    PAD - ChatGPT Flow.zip
    Labels:
    • Labels:
    • Desktop flows
    Message 1 of 16
    7,038 Views
    4 Kudos
    Reply
    • All forum topics
    • Previous Topic
    • Next Topic
    • « Previous
      • 1
      • 2
    • Next »
    Lagloire
    Lagloire Helper III
    Helper III
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-22-2023 11:21 AM

    Hi  @VJR 

    I'm trying to do exactly the same thing as you suggested here and i'm getting this error : Variable 'JsonAsCustomObject' doesn't have a property 'choices'.

     

    Could you tell me why am i getting this error and how to solve it?

     

    Thank you

    Message 2 of 16
    5,821 Views
    0 Kudos
    Reply
    VJR
    Multi Super User VJR
    Multi Super User
    In response to Lagloire
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-23-2023 06:54 PM

    Hi @Lagloire 

     

    That error is occurring because the API is not returning the results and the Datatable 'choices' portion is not getting populated.

    Could you recheck all the parameters thoroughly especially the token key.

     

     

    Message 3 of 16
    5,544 Views
    0 Kudos
    Reply
    Lagloire
    Lagloire Helper III
    Helper III
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 02:15 AM

    @VJR Hi

    I rechecked the parameters many times and everything is correct, but not working. It worked perfectly for you?

    I tried to use in cloud flow GPT3, and not working at all. Any help?

    Message 4 of 16
    5,494 Views
    0 Kudos
    Reply
    VJR
    Multi Super User VJR
    Multi Super User
    In response to Lagloire
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 02:19 AM

    For Cloud flows there may be separate articles. This is for Desktop Flows.

    Message 5 of 16
    5,489 Views
    0 Kudos
    Reply
    Lagloire
    Lagloire Helper III
    Helper III
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 02:22 AM

    Yes, i know it's desktop flow. As it didn't work, i've tried cloud flow to see if i can get it worked. It's there possible to have a screen share and there you can see what is not working?

    Message 6 of 16
    5,483 Views
    0 Kudos
    Reply
    VJR
    Multi Super User VJR
    Multi Super User
    In response to Lagloire
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 02:27 AM

    What are the outputs of the web response variables?

    Message 7 of 16
    5,478 Views
    0 Kudos
    Reply
    Lagloire
    Lagloire Helper III
    Helper III
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 04:32 AM

    @VJR Below are the screen share. Let me know what am'i doing wrong.

     

    Lagloire_0-1674562899406.png

     

    Lagloire_2-1674563039785.png

     

    Lagloire_3-1674563096940.png

     

    Lagloire_4-1674563214394.png

     

     

    Lagloire_5-1674563272544.png

     

    Lagloire_6-1674563440446.png

     

    Message 8 of 16
    5,448 Views
    0 Kudos
    Reply
    VJR
    Multi Super User VJR
    Multi Super User
    In response to Lagloire
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 04:43 AM

    @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. 

    Message 9 of 16
    5,442 Views
    0 Kudos
    Reply
    VJR
    Multi Super User VJR
    Multi Super User
    In response to Lagloire
    • Mark as New
    • Bookmark
    • Subscribe
    • Mute
    • Subscribe to RSS Feed
    • Permalink
    • Print
    • Report Inappropriate Content

    ‎01-24-2023 04:43 AM

    @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. 

    Message 10 of 16
    5,439 Views
    0 Kudos
    Reply
    • « Previous
      • 1
      • 2
    • Next »

    Power Platform

    • Overview
    • Power BI
    • Power Apps
    • Power Pages
    • Power Automate
    • Power Virtual Agents

    • Sign up free
    • Sign in

    Browse

    • Templates
    • Connectors
    • Partners

    Downloads

    • Mobile
    • Gateway

    Learn

    • Documentation
    • Learn
    • Support
    • Community
    • Give feedback
    • Blog
    • Pricing

    • © 2023 Microsoft
    • Contact us
    • Trademarks
    • Privacy & cookies
    • Manage cookies
    • Terms of use
    • Terms & conditions
    California Consumer Privacy Act (CCPA) Opt-Out Icon Your California Privacy Choices