Studio
2024.10
Studio ガイド - プレビュー
Last updated 2024年6月10日

OpenAI とコード化されたワークフローの統合

このチュートリアルでは、Wikipedia から UiPath の記事の説明を取得し、それを OpenAI の ChatGPT に送信して記述を追加する処理を自動化するプロセスについて説明します。
前提条件
  1. [新規] を選択してコード化されたワークフローを作成し、[ファイル] グループから [コード化されたワークフロー] を選択します。
  2. OpenAI の API キーを作成して保存します。API キーをシステム環境変数として追加します。マシンを再起動して、変更が保存されていることを確認します。
  3. Studio で以下のコードを使用して、システム環境変数から OpenAI API キーを取得するための読み取り専用のプライベートな静的文字列Execute メソッドの前に作成します。
    private static readonly string OpenAiApiKey = Environment.GetEnvironmentVariable("OPEN_AI_API_KEY");private static readonly string OpenAiApiKey = Environment.GetEnvironmentVariable("OPEN_AI_API_KEY");
    注: OpenAiApiKey メソッドの変数の名前が、システム環境に追加した名前と同じであることを確認します。
  4. 自動化する必要のある UI 要素を Wikipedia から取得します。[オブジェクト リポジトリ] タブに移動し、Wikipedia という名前のアプリケーションを作成します。
  5. Wikipedia アプリケーション用に 2 つの画面を作成します。
    1. WikiMainScreen – 検索を実行するランディングページ。この画面では、次の 2 つの要素を作成します。
      • SearchBar
      • SearchButton
    2. UiPathWikiPage – UiPath に関する Wikipedia の記事に表示される内容です。この画面では、記事の最初の段落を示す UiPathDescription 要素を作成します。


  6. UI Automation サービスと Open API を使用し、以下のコードを使用して Wikipedia のランディング ページを開きます。
    var wikiScreen= uiAutomation.Open(ObjectRepository.Descriptors.WikiMainScreen);var wikiScreen= uiAutomation.Open(ObjectRepository.Descriptors.WikiMainScreen);
  7. TypeInto API を使用し、Wikipedia の検索機能で UiPath を検索します。
    wikiScreen.TypeInto(ObjectRepository.Descriptors.WikiMainScreen.SearchBar, "UiPath");wikiScreen.TypeInto(ObjectRepository.Descriptors.WikiMainScreen.SearchBar, "UiPath");
     
  8. 検索ボタンをクリックし、Click API を使用して検索を実行します。
    wikiScreen.Click(ObjectRepository.Descriptors.WikiMainScreen.SearchButton);wikiScreen.Click(ObjectRepository.Descriptors.WikiMainScreen.SearchButton);
  9. Attach および Get Text API を使用して、Wikipedia の記事から UiPath の説明を取得します。Attach API は [アプリケーション/ブラウザーを使用] アクティビティと同様に動作するため、既に開いているアプリケーション/ブラウザーにフォーカスして自動化できます。次のコードを使用します。
    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. OpenAI のアカウントを使用して ChatGPT に説明を送信します
    1. api という名前の変数を作成し、提供された API キーを使用してアクセスできる OpenAI アカウントを初期化します。以下の変数を使用します。
      var api = new OpenAIAPI(OpenAiApiKey);var api = new OpenAIAPI(OpenAiApiKey);
    2. chatResult という名前の変数を作成し、新しいチャットを初期化して ChatGPT にプロンプトを送信します。以下のコードを使用します。
      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. 以下の式を使用して、メッセージ ボックス内に ChatGPT の応答を表示します。
    MessageBox.Show(chatResult.Choices[0].Message.Content);MessageBox.Show(chatResult.Choices[0].Message.Content);


サンプル プロジェクト

この手順に従ってチュートリアルを自分で試してみるには、サンプル プロジェクト「Integrating OpenAI with coded workflows (OpenAI をコード化されたワークフローと連携させる」をダウンロードします。

  • サンプル プロジェクト

Was this page helpful?

サポートを受ける
RPA について学ぶ - オートメーション コース
UiPath コミュニティ フォーラム
UiPath ロゴ (白)
信頼とセキュリティ
© 2005-2024 UiPath. All rights reserved.