ささみ学習帳 - sasami's study book

ささみ学習帳

Microsoft365 や Power Platform について学んだこと・アイデアのメモ

Microsoft365 管理センターのサービス正常性のメール通知を翻訳してTeamsチャネルに投稿する PowerAutomate クラウドフロー (2/3) 💎OpenAI API Completion にお任せ版

 

はじめに

こちらの続きです。

sasami-axis.hatenablog.com

 

前回作成したフローを修正し、OpenAI APIで細かい処理をChatGPTにまるっと任せてフロー作成を効率化してみたバージョンです。

 

このフローの概要

前回のフローではメール本文のレイアウトがほんの少しでも変わるとフローの修正が必要になってしまいます。しかし、今回のOpenAIにまるっとお任せであれば、ある程度は融通を効かせて抽出してくれます。

反面、今回のフローでは稀に日本語訳がされなかったり、Detailが要約されてしまうといった出力が安定しない事があります。この辺りはまだまだ要調整ですが障害通知であれば許容できる範囲かと思います。

 

完成イメージ

※新しいチャネルエクスペリエンスに切り替わってから、アダプティブカードが全幅にならなくなってしまいました。

モバイルアプリの通知で内容がちょっと見えるのでヒヤッとします。

 

フロー全体

赤枠部分が前回フローからの変更点です。

 

フローの解説

前回フローからの変化点の赤い枠の部分のみの解説です。

プロンプトの作成

 

Open AI API のCompletion に指示するプロンプトのテキストはこちらです。(見よう見まねで作ったので改善の余地あり)

Summaryに要約して日本語訳した文章を格納していますが、原文もDetailとして出力させています。

---
@{body('Html_からテキスト')}
---
上記の文章から下記の項目を抽出してください。
・IDTitleStatusImpacted ServicesDetailsURLSummary

###回答の条件
・回答が不明な場合は「回答不明」と回答する
・Statusは原文のまま抽出する
・Impacted Servicesは原文のまま抽出する
・抽出した文字列に含まれる"は'に置換する
・URLIDの近くにある'IncidentMail'を含むものを選択する
・Titleは抽出したものを日本語に翻訳する
・Details'Details'の行の次から'Are you experiencing this issue?'の直前の行までの複数行を省略することなく原文の言語のまま抽出する
・SummaryDetailsを5行以内に要約し日本語に翻訳する

###次のJSON形式で回答する
{
"ID": "",
"Title": "",
"Status": "",
"Impacted Services": "",
"Details": "",
"URL":"",
"Summary":""
}

 

OpenAI APIの呼び出し

Open AI API のCompletionを呼び出します。

APIリファレンスはこちらです。

platform.openai.com

  • 方法
    • POST
  • URI
  • ヘッダー
    • Content-type application/json
    • Authorization Bearer [OpenAI API key]
  • 本文
    • {
        "model": "gpt-3.5-turbo-0613",
        "messages": [
          {
            "role": "user",
            "content": "@{outputs('作成-prompt')}"
          }
        ]
      }

下記のように出力されます。

choicesの中に指定したjsonで回答されている事がわかります。

{
  "id": "chatcmpl-7vJNCQGVuTfGvuxCngBJebpVfyint",
  "object": "chat.completion",
  "created": 1687839623,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "{\n\"ID\": \"SP600157\",\n\"Title\": \"SharePoint Onlineクラシックエクスペリエンスで特定のWebパーツを表示できません\",\n\"Status\": \"Service Restored\",\n\"Impacted Services\": \"SharePoint Online\",\n\"Details\": \"Title: Users are unable to render certain web parts in the SharePoint Online classic experience\\n\\nUser impact: Users were unable to render certain web parts in the SharePoint Online classic experience.\\n\\nMore info: Specifically, users were unable to render the 'DataFormWebPart' and Extensible Stylesheet Language Transformation (XSLT) web parts in the SharePoint Online classic experience.\\n\\nFinal status: We've completed deploying the fix to the impacted environment, and we received confirmation from some users who initially reported the issue that the impact is remediated.\\n\\nScope of impact: Impact may have occurred for some users in which they were unable to render the 'DataFormWebPart' and XSLT web parts in the SharePoint Online classic experience.\",\n\"URL\":\"https://admin.microsoft.com/AdminPortal/home#/servicehealth/:/alerts/SP600157?shdlinksource=IncidentMail\",\n\"Summary\":\"SharePoint Onlineクラシックエクスペリエンスでは、特定のWebパーツを表示できませんでした。具体的には、ユーザーはSharePoint Onlineクラシックエクスペリエンスで'DataFormWebPart'およびExtensible Stylesheet Language Transformation(XSLT)Webパーツを表示できませんでした。当該環境への修正の展開が完了し、最初に問題を報告した一部のユーザーから影響が改善されたことが確認されました。影響は、SharePoint Onlineクラシックエクスペリエンスで'DataFormWebPart'およびXSLT Webパーツを表示できなかった場合に限定されます。\"\n}"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 2292,
    "completion_tokens": 465,
    "total_tokens": 2757
  }
}

 

JSONの解析-OpenAI

HTTPアクションの出力を解析しています。

スキーマはこちらを使用しています。

{
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "object": {
            "type": "string"
        },
        "created": {
            "type": "integer"
        },
        "model": {
            "type": "string"
        },
        "choices": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "index": {
                        "type": "integer"
                    },
                    "message": {
                        "type": "object",
                        "properties": {
                            "role": {
                                "type": "string"
                            },
                            "content": {
                                "type": "string"
                            }
                        }
                    },
                    "finish_reason": {
                        "type": "string"
                    }
                },
                "required": [
                    "index",
                    "message",
                    "finish_reason"
                ]
            }
        },
        "usage": {
            "type": "object",
            "properties": {
                "prompt_tokens": {
                    "type": "integer"
                },
                "completion_tokens": {
                    "type": "integer"
                },
                "total_tokens": {
                    "type": "integer"
                }
            }
        }
    }
}

 

作成-contents

APIの出力から、回答部分を抽出しています。

常にアレイで返されるので、first関数で先頭を取得しています・

first(body('JSON_の解析-OpenAI')?['choices'])?['message/content']
{
"ID": "SP600157",
"Title": "SharePoint Onlineクラシックエクスペリエンスで特定のWebパーツを表示できません",
"Status": "Service Restored",
"Impacted Services": "SharePoint Online",
"Details": "Title: Users are unable to render certain web parts in the SharePoint Online classic experience\n\nUser impact: Users were unable to render certain web parts in the SharePoint Online classic experience.\n\nMore info: Specifically, users were unable to render the 'DataFormWebPart' and Extensible Stylesheet Language Transformation (XSLT) web parts in the SharePoint Online classic experience.\n\nFinal status: We've completed deploying the fix to the impacted environment, and we received confirmation from some users who initially reported the issue that the impact is remediated.\n\nScope of impact: Impact may have occurred for some users in which they were unable to render the 'DataFormWebPart' and XSLT web parts in the SharePoint Online classic experience.",
"URL":"https://admin.microsoft.com/AdminPortal/home#/servicehealth/:/alerts/SP600157?shdlinksource=IncidentMail",
"Summary":"SharePoint Onlineクラシックエクスペリエンスでは、特定のWebパーツを表示できませんでした。具体的には、ユーザーはSharePoint Onlineクラシックエクスペリエンスで'DataFormWebPart'およびExtensible Stylesheet Language Transformation(XSLT)Webパーツを表示できませんでした。当該環境への修正の展開が完了し、最初に問題を報告した一部のユーザーから影響が改善されたことが確認されました。影響は、SharePoint Onlineクラシックエクスペリエンスで'DataFormWebPart'およびXSLT Webパーツを表示できなかった場合に限定されます。"
}

JSONの解析-content

分かりやすさ重視で再びJSONの解析をしています

スキーマはこちら

{
    "type": "object",
    "properties": {
        "ID": {
            "type": "string"
        },
        "Title": {
            "type": "string"
        },
        "Status": {
            "type": "string"
        },
        "Impacted Services": {
            "type": "string"
        },
        "Details": {
            "type": "string"
        },
        "URL": {
            "type": "string"
        },
        "Summary": {
            "type": "string"
        }
    }
}

 

Teamsチャネルで通知する

取得できたデータをTeamsチャネルに投稿する処理は前回とほぼ同じなので省略します。

 

メールを移動する

今回フローの内容とは直接関係しないのですが、処理が完了したメールを別フォルダに移動するように変更しています。

 

OpenAI APIは従量課金だけれどこのフローはどれくらいかかる?

OpenAI APIは従量課金制です。入力と出力のトークン数で課金されます。今回のフローでどれくらいの費用が発生するか、についてです。

直近で実際に動作させたフローのトークン数は下記のようになっています。

参考値として、2023年5月のメール通知数は115通でしたので、上記のトークン数×115でざっくり計算しても現在のOpenAPIの価格であれば1か月あたり0.5$程度の課金で済みます。それほど費用は気にしなくても良いかと思います。

 

さいごに

OpenAI APIを課金したものの活用アイデアが浮かばず....とりあえず思いついたものを作ってみました。AI使ってます!的な派手なものではないですが、地味に楽をできるのもよいですね。

さて、今回のフローではOpenAI APIを使うためにプレミアムコネクタであるHTTPコネクタが必要になります。そしてHTTPコネクタが使えるのであれば、メール本文の情報を抽出するよりもメール件名に含まれるIDでGraph APIでサービス正常性のデータを検索したほうがよりも詳細な情報が取得できます。より正確性を求めるのであればGraphAPIで取得したほうが良い結果となるかと思います。

その辺りこちらに投稿しています。

sasami-axis.hatenablog.com

 

 

 

サービス正常性のメール通知に関しては「Azure OpenAI Service編」に続きます。

sasami-axis.hatenablog.com