studio
2024.10
true
UiPath logo, featuring letters U and I in white
Studio User Guide
Last updated Nov 18, 2024

Integrating OpenAI with Coded Workflows

This tutorial guides you through the process of automating the retrieval of the description of UiPath's Wikipedia article and then sending it to OpenAI’s Chat GPT to make it longer.
Prerequisites
  1. Create a Coded workflow by selecting New, and then Coded Workflow from the File group.
  2. Create an API key for your OpenAI and save it. Add the API key as a system environment variable. Restart your machine to ensure that the changes are saved.
  3. In Studio, before the Execute method, create a private static readonly string to retrieve the OpenAI API key from your system environment variables using the following code:
    private static readonly string OpenAiApiKey = Environment.GetEnvironmentVariable("OPEN_AI_API_KEY");private static readonly string OpenAiApiKey = Environment.GetEnvironmentVariable("OPEN_AI_API_KEY");
    Note: Make sure that the name of the variable from the OpenAiApiKey method is the same as the one you added to your system environment.
  4. Retrieve the UI elements that you need to automate from Wikipedia. Go to the Object Repository tab and create an application named Wikipedia.
  5. Create two screens for the Wikipedia application:
    1. WikiMainScreen – the landing page where you perform the search. For this screen create two elements:
      • SearchBar
      • SearchButton
    2. UiPathWikiPage – the resulting Wikipedia article about UiPath. For this screen, create the UiPathDescription element, which indicates the first paragraph in the article.


  6. Use the UiAutomation service, along with the Open API, to open the Wikipedia landing page using the following code:
    var wikiScreen= uiAutomation.Open(ObjectRepository.Descriptors.WikiMainScreen);var wikiScreen= uiAutomation.Open(ObjectRepository.Descriptors.WikiMainScreen);
  7. Search for UiPath in the Wikipedia search, using the Type Into API.
    wikiScreen.TypeInto(ObjectRepository.Descriptors.WikiMainScreen.SearchBar, "UiPath");wikiScreen.TypeInto(ObjectRepository.Descriptors.WikiMainScreen.SearchBar, "UiPath");
     
  8. Click the search button to perform the search, using the Click API.
    wikiScreen.Click(ObjectRepository.Descriptors.WikiMainScreen.SearchButton);wikiScreen.Click(ObjectRepository.Descriptors.WikiMainScreen.SearchButton);
  9. Get the description about UiPath from the Wikipedia article, using the Attach and Get Text APIs. The Attach API behaves similarly to the Use Application/Browser activity, allowing you to focus on an already open Application/Browser and automate it. Use the following code:
    var uipathWikiPage = uiAutomation.Attach(ObjectRepository.Descriptors.UiPathWikiPage);
    var uipathWikiDescription = uipathWikiPage.GetText(ObjectRepository.Descriptors.UiPathWikiPage.UiPathDescription);
    Log("This is the UiPath's wikipedia description: " + uipathWikiDescription);var uipathWikiPage = uiAutomation.Attach(ObjectRepository.Descriptors.UiPathWikiPage);
    var uipathWikiDescription = uipathWikiPage.GetText(ObjectRepository.Descriptors.UiPathWikiPage.UiPathDescription);
    Log("This is the UiPath's wikipedia description: " + uipathWikiDescription);
    
  10. Send the description to ChatGPT using your OpenAI account
    1. Create a variable named api to initialize your OpenAI account that you can access using the provided API key. Use the following variable:
      var api = new OpenAIAPI(OpenAiApiKey);var api = new OpenAIAPI(OpenAiApiKey);
    2. Create a variable named chatResult, where you initialize the new chat and sent a prompt to ChatGPT. Use the following code:
      var chatResult = api.Chat.CreateChatCompletionAsync("Please rewrite the following description about UiPath: '" + uipathWikiDescription + "', and make it longer").Result;var chatResult = api.Chat.CreateChatCompletionAsync("Please rewrite the following description about UiPath: '" + uipathWikiDescription + "', and make it longer").Result;
  11. Display Chat GPT’s response inside a message box, using the following expression:
    MessageBox.Show(chatResult.Choices[0].Message.Content);MessageBox.Show(chatResult.Choices[0].Message.Content);


Sample project

To follow the steps and try out the tutorial yourself, download the following sample project: Integrating OpenAI with coded workflows.

  • Sample project

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.