01-24-2023 04:32 AM
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:
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
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:
Final Output:
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
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.
@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?
For Cloud flows there may be separate articles. This is for Desktop Flows.
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?
What are the outputs of the web response variables?
@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.
@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.