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.

https://beta.openai.com/account/api-keysAccount API Keys - OpenAI API
2. Use the Invoke Web service action to call the API

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

- 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:

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.

Here is a screenshot after entering all the values.

3. Entire Flow at a glance

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

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

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

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.

v. Final answer from the ChatGPT prefixed along with the original question that the user has asked.
Question: %UserInput%
%JsonAsCustomObject['choices'][0].text%

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

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:

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:
