You can obtain an API key from OpenAI API
API pricing: Pricing
To see the API usage OpenAI API
API Reference: OpenAI API
What’s changed with engine names and best practices? What's changed with engine names and best practices? | OpenAI Help Center
Example used in this tutorial: OpenAI API
- We want to adapt the same Rest API request in UiPath Studio.
URL: https://api.openai.com/v1/engines/davinci/completions
Method: POST
Headers:
Authorization: Bearer + API_KEY
Content-Type: application/json
Body:
- In UiPath Studio (I used the latest stable version at this moment 2022.10.4 with Windows project compatibility and C# language)
Add to your project these dependencies:
UiPath.System.Activities[22.10.4]
UiPath.WebAPI.Activities[1.13.3]
- In your workflow add an Assign activity to hold your API_KEY
- Add an Input Dialog activity and save the value entered to Question variable
- Add another Assign activity that will build your Body JSON input data and store into JsonInputData variable
@"{
" + "\n" +
@"""model"": """",
" + "\n" +
@"""prompt"": ""Q: " + Question + @"?\nA:"",
" + "\n" +
@"""temperature"": 0,
" + "\n" +
@"""max_tokens"": 1000,
" + "\n" +
@"""top_p"": 1,
" + "\n" +
@"""frequency_penalty"": 0.0,
" + "\n" +
@"""presence_penalty"": 0.0,
" + "\n" +
@"""stop"" :""\n""
" + "\n" +
@"}"
- Add an HTTP Request activity and configure it as below. You can skip the Preview and just press OK and configure later on the Properties panel.
Enable SSL certificate verification: true
Timeout (milliseconds): 60000
Accept Format: JSON
Request Method: POST
Request URL: "https://api.openai.com/v1/engines/davinci/completions"
Body: JsonInputData
Body Format: application/json
Headers:
Authorization: string.Format("Bearer {0}", API_KEY)
Content-Type: “application/json”
Response Content: ResponseContent (create a variable with Ctrl + K)
- Add a Deserialize JSON activity and save its output to JsonObjectOutput
- Add a new Assign activity that will store the Rest API response ExtractedValueOutput
JsonObjectOutput.SelectToken("$.choices.[0].text").ToString()
- Add in the end a Message Box activity to display the results.
"Question: " + Question.Replace("?","") + "?" + "\n\n" + "Answer: " + ExtractedValueOutput
-
Publish the project to Orchestrator
-
Add the process to a Modern Folder
-
Run the process from UiPath Assistant
The sample workflow can be found here: ChatGPT ask.zip (90.7 KB)