How to use GPT API with UiPath
ChatGPT is an innovative tool that, much like bots, we should integrate into our daily routines to enhance efficiency.
Below, I outline two distinct methods to bridge GPT with UiPath
1. Using InternalLabs.GPT.Activities package
Opting for this method offers simplicity as it employs pre-built activities. To initiate, two essential packages must be incorporated into our projects:
InteligentOCR.Activities
InternalLabs.GPT.Activities
Through this package, we gain access to the "Ask GPT" activity. Its operation is straightforward: you provide the text from which you want to extract information, along with a set of queries for the model.
Use Case Example: Imagine you have a task that involves reading different files from a specified folder, each containing varied invoices from multiple vendors. The "Digitalize" activity from the IntelligentOCR package can be employed to convert the invoice image into text. This converted text becomes the value input for the "Text" property of the Ask GPT activity.
Your list of questions can dictate the specifics you aim to extract.
For instance, my query was:
Extract Invoice Number, Invoice Date, Due Date, Bill To, Ship To, Item, Quantity, Rate, Amount, Total Price and present the responses separated by |.
Here is a sample of the invoice that we want to extract the data from.
This is the extracted value
Result: The extracted output is impressively precise. With the segmented output, it's really easy to transition the data into a Data Table.
The advantage of using this method is that it can be implemented quickly, but we don't have the flexibility that we would have if we used the 2nd method.
1. Using HTTP Request to call GPT API directly
The first step is definitely to go to the ChatGPT documentation and to understand how to work with its API
After that, we need to generate and get the API Key. In the Authentication part, we can take the API Key that we need (it is necessary that we have logged in with our account before that).
The second step is to create a new project within UiPath Studio. It is necessary to install the UiPath.WebAPI.Activities package.
After that, the HTTP Request activity will be available to us, with which we use to call Chat GPT through its API.
领英推荐
Also, we need to import Newtonsoft.Json and Newtonsoft.Json.Linq namespaces in order to work with JSON objects.
The third step is the configuration of the HTTP Request activity itself.
If we go back to the ChatGPT documentation (https://platform.openai.com/docs/api-reference/chat) we can see what the query should look like and what URL is.
In our case, we need to make a POST request to the URL https://api.openai.com/v1/chat/completions.
On the same page, we can see what JSON body should look like. We are going to design this request inside UiPath Studio
Within UiPath we need to configure the following parameters of the HTTP Request activity:
RequestMethod = POST RequestURL = https://api.openai.com/v1/chat/completions
Body = JObject.FromObject( New With { key .model=”text-davinci-003", key .prompt= in_param1, key .max_tokens=in_param2, key .temperature= 0 } ).ToString
Body Format = application/json ResponseContent = strResponse
Once everything is configured, we are ready to call ChatGPT. (I also modified the timeout period as I am not currently paying for ChatGPT so my response time is a bit slower)
The fourth step is parsing the response that ChatGPT returns. It should be noted that the HTTP Request activity returns a response in String format, so we need to transfer that string to JSON format.
We do this by parsing the response through a JObject
jsonResponse = JObject.Parse(strResponse)
In this way, we have a much better view of the answer we received.
And then we’ll parse that JSON and pull the response from ChatGPT
strChatGPTResponse = jsonResponse(“choices”)(0)(“text”).ToString
As we can see, ChatGPT has given us a detailed answer to our question. The whole sequence looks like the image below:
This is how we can connect UiPath and ChatGPT. Some use cases for ChatGPT and UiPath (or RPA in general):
If you have any questions, or if you have any problems during your implementation, feel free to contact me.
Until next time, happy automation!
RPA Developer | UiPath | UiARD Certified
1 年Great article Sr?an Su?, thanks for mentioning examples of the GPT and Uipath integration in which the data we send is not sensitive.