# 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.

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
* UiPath Chrome Extension
* [UiAutomation.Activities](https://docs.uipath.com/activities/other/latest/ui-automation/ui-automation-apis) 23.10
* Lofcz.Forks.OpenAI 1.8.3
1. Create a Coded workflow by selecting **New**, and then **Coded Workflow** from the **File** group.
2. Create an [API key for your OpenAI](https://platform.openai.com/account/api-keys) 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");
   ```
   :::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](https://docs.uipath.com/studio/standalone/2025.10/user-guide/creating-an-object-repository#creating-an-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.

   ![docs image](https://dev-assets.cms.uipath.com/assets/images/studio/studio-docs-image-269104-0c10d356.webp)

6. Use the UiAutomation service, along with the [Open coded automation API](https://docs.uipath.com/activities/other/latest/user-guide/n-application-card), to open the Wikipedia landing page using the following code:
   ```
   var wikiScreen= uiAutomation.Open(ObjectRepository.Descriptors.WikiMainScreen);
   ```
7. Search for UiPath in the Wikipedia search, using the [Type Into coded automation API](https://docs.uipath.com/activities/other/latest/user-guide/n-type-into).
   ```
   wikiScreen.TypeInto(ObjectRepository.Descriptors.WikiMainScreen.SearchBar, "UiPath");
   ```
8. Click the search button to perform the search, using the [Click coded automation API](https://docs.uipath.com/activities/other/latest/user-guide/n-click).
   ```
   wikiScreen.Click(ObjectRepository.Descriptors.WikiMainScreen.SearchButton);
   ```
9. Get the description about UiPath from the Wikipedia article, using the **Attach** and [Get Text](https://docs.uipath.com/activities/other/latest/user-guide/n-get-text) coded automation APIs. The **Attach** coded automation API behaves similarly to the [Use Application/Browser](https://docs.uipath.com/activities/other/latest/user-guide/n-application-card) 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);
   ```
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);
       ```
    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;
       ```
11. Display Chat GPT’s response inside a message box, using the following expression:
    ```
    MessageBox.Show(chatResult.Choices[0].Message.Content);
    ```

![docs image](https://dev-assets.cms.uipath.com/assets/images/studio/studio-docs-image-271451-e04bbea3.webp)

## Sample project

To follow the steps and try out the tutorial yourself, download the following sample project: [Integrating OpenAI with coded workflows](https://documentationexamplerepo.blob.core.windows.net/examples/CodedAutomations/IntegratingOpenAIwithCodedWorkflows23_10.zip).
