How to integrate ChatGPT in UiPath using the HTTP Request activity?

You can obtain an API key from OpenAI Platform
API pricing: Pricing
To see the API usage OpenAI Platform
API Reference: OpenAI Platform
GPT models: OpenAI Platform
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 Platform


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

  1. 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]

image

  1. In your workflow add an Assign activity to hold your API_KEY

image

  1. Add an Input Dialog activity and save the value entered to Question variable

image

  1. 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" +
      @"}"
  1. 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)

  1. Add a Deserialize JSON activity and save its output to JsonObjectOutput

  1. Add a new Assign activity that will store the Rest API response ExtractedValueOutput
JsonObjectOutput.SelectToken("$.choices.[0].text").ToString()

image

  1. Add in the end a Message Box activity to display the results.
"Question: " + Question.Replace("?","") + "?" +  "\n\n" + "Answer: " +   ExtractedValueOutput

  1. Publish the project to Orchestrator

  2. Add the process to a Modern Folder

  3. Run the process from UiPath Assistant

The sample workflow can be found here: ChatGPT ask.zip (90.7 KB)

13 Likes

Hey!

Thank you alot for this detailed Tutorial,

but i receive a Error Message in the JsonInputData Assign Acitivity:

image

Are you using a C# project or a VB?

The Assign activity should look like this:

What are your Studio and UiPath.System.Activities versions?

i am Using VB

And my UiPath Version is:

2022.12.0

Should i Use C+?

If you want to achieve the same with the provided payload in this example, yes, you need a C# project language.

For a VB project, you need to readapt the payload.

Hello!

So i have changed to C#, now the Error is gone.

But now i receive a new Error:
image

image

Get my zip file and check there

i had, but i cant open it properly.

So i receive a lot of Errors

Okey i have solved the Problem

Hey!

I wanna Try out another Model but i have a problem with the JsonInputData

Could you help me Please with this model?

{
“model”: “text-davinci-003”,
“prompt”: “Say this is a test”,
“max_tokens”: 7,
“temperature”: 0,
“top_p”: 1,
“n”: 1,
“stream”: false,
“logprobs”: null,
“stop”: “\n”
}

Install Postman → test the endpoint, and if it is successful (200 OK) → generate the code in the needed language and use the body payload from the provided example

1 Like

Thanks alot!

This Tool is amazing, so now i have tried it and get a Status; 200 OK

But i dont receive any Answer:

Am i doing something wrong?

Double-check the API documentation as I think the value that you provided in the prompt is not correct.

Hi @marian.platonov,

I tried to do that same but getting some error we mentioned below.
please look it.

{
“error”: {
“message”: “You exceeded your current quota, please check your plan and billing details.”,
“type”: “insufficient_quota”,
“param”: null,
“code”: null
}
}

Assign: Object reference not set to an instance of an object.

The issue description is in the message: “You exceeded your current quota, please check your plan and billing details.”

  • Make sure you have created an account and you have your own API key.
  • Check your Credit balance and reduce the max_tokens value in your payload.
  • You’ll need billing details to access all the other models and tools like Playground Pricing

If you need more help with OpenAi API(s) and other resources, open a discussion directly on their community

1 Like

Hi @marian.platonov ,
Is it paid API or Free some Request???

At the creation, they will give you 18$ to test the API requests. After that, you need to pay.

1 Like

Previous issue solved but now getting this error

{
“error”: {
“message”: “You didn’t provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you’re accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://beta.openai.com.”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null
}
}

Create the API key and use it in the header:

In Postman:

In the Authorization section

or directly in the Header section

In the UiPath Studio project, in the HTTP Request activity in the Headers section