5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure OpenAIの「GPT-4o」モデルとPower Appsで領収書読み取りアプリを作ってみる

Last updated at Posted at 2024-05-23

はじめに

Microsoft Build 2024、期待通り熱気が凄い!

Power AutomatePower Appsのアップデートもめちゃくちゃ熱いですが、私はやはり
Azure OpenAIで「GPT-4o」のGA! が胸熱🔥

GPT-4oは凄いと言い続ける驚き屋になっていますが、領収書読み取りアプリをテーマに、GPT-4oの読み取り性能を見ていきましょう。

■ 驚き屋 実績

Power Apps & GPT-4oを使って超高速で画像解析アプリを作る! は、かつてないほど読んでいただきました!
大感謝です🐟✨

記事は2024.05.23時点のものです
恐ろしいほど、技術進歩が速いため、すぐに陳腐化する恐れがあります

Azure OpenAI 「GPT-4o」GA!

Microsoft Build 2024の目玉発表で、今話題の「GPT-4o」がAzure OpenAIでも使用可能であることが、発表されました。

GPT-4oは、OpenAIの最新のモデルであり、文字列と画像に対応する凄まじいモデルです。

GPT3の時から驚きっぱなしですが、間違いなくこのモデルは、それ以上の凄まじい性能を持ち合わせています。

■ 主な比較ポイント

項目 GPT-4o GPT-4 GPT-3.5
音声モードの応答速度 最速232ミリ秒、平均320ミリ秒 平均5.4秒 平均2.8秒
音声モードの仕組み 直接処理可能 音声をテキストに変換後、テキストを処理 音声をテキストに変換後、テキストを処理
テキスト性能(英語とコード) GPT-4 Turboと同等 高い すごかった
テキスト性能(非英語) 凄い 高い すごかった
API 速度 凄い 標準 標準
API コスト GPT-4より50%安い 標準 安い

とにかく凄いんです!まさに革命!
これがAzure OpenAIでも使えるのであれば、デプロイまったなし!

なお対応しているリージョンは限定されています。

リージョン 日本語
eastus 米国東部
eastus2 米国東部2
northcentralus 米国中北部
southcentralus 米国南中部
westus 米国西部
westus3 米国西部3

とりあえずデプロイしましょう!
そして、Azure Key Vaultに、シークレットをイン!

ソリューションでAzure Key Vault シークレットの環境変数を使用

Power Apps / Power Automateで、エンドポイントAPIキーの管理は、気になるところです。
なんとなく、セキュアな入出力をONにしても、「これで果たして大丈夫なのか・・・」と不安を感じます。

Power Apps / Power Automateで出来ること自体、管理が難しいことは否めません。
そこで活躍するAzureのサービスが、Azure Key Vaultです。

Power Platformの環境に、Azure Key Vaultを設定することで、ソリューションの環境変数に、機密情報を格納できます。

手順はMicrosoft learnを参考にしてください。

Azure ロールベースのアクセス制御 の場合、Dataverseキーコンテナの参照権限があり、
Azure Key Vault に、シークレットを管理できる権限を自分に付与できればクリア!

ソリューションに、シークレットを環境変数として設定できます。
シークレットとして設定した環境変数はMicrosoft Dataverseコネクタで、バインドしていないアクションを実行するアクションから、値を参照することができます。

アクション名は、RetrieveEnvironmentVariableSecretValue
EnvironmentVariableNameに、環境変数の名前 (表示名ではない)を設定することで、値を呼び出します。

image.png

セキュリティで保護された入力セキュリティで保護された入力、それぞれ設定することで、情報の安全性も向上します。

image.png

上記のアクションを採用して、Azure OpenAIエンドポイントAPI キーデプロイ名を環境から呼び出し、リソースにHTTP要求を送信して、値を受け取るフローは、下記のようになります。

image.png

  • プレミアムコネクタ使用可能であるライセンスが前提。
  • 応答で値をPower Appsに渡します
    • 理由は、配列をPower Appsに戻すため

Power Apps

サクっとつくっちゃいましょう。

ツリー図は下記の通り

Power Apps コントロールのツリー図
Main
├─ grpComplete
│  ├─ btnComplete
│  ├─ txtComplete
│  └─ backgroundComplete
├─ grpProgress
│  ├─ txtProgress
│  ├─ imgProgress
│  ├─ Progress
│  └─ backgroundProgress
├─ ScreenContainer
│  └─ HeaderContainer
│     ├─ imgTop
│     ├─ lblTitle
│     └─ Avatar
└─ BottomContainer
│  └─ SidebarContainer
│     └─ conMenu
│        ├─ icoMenu
│        ├─ lblMode
│        ├─ conBadge
│        │  └─ Badge
│        ├─ InfoContainer
│        │  ├─ InfoButton
│        │  └─ lblInfo
│        └─ conPicture
│           ├─ ImageContainer
│           │  ├─ AddPicture
│           │  └─ Image
│           └─ ButtonPicture // ここに関数
└─ MainContainer
   ├─ conLogo // 読み取りセクションのヘッダー
   │  ├─ LogoImage
   │  ├─ lblDetail
   │  └─ barHeaderMain
   ├─ conDate // 領収書の購入日付
   │  ├─ BadgeDate
   │  └─ DatePickerCanvas
   ├─ conStore // 領収書の購入先
   │  ├─ BadgeStore
   │  └─ TextStore
   ├─ conAmount // 領収書の合計
   │  ├─ BadgeAmount
   │  └─ NumberAmount // モダン コントロールの数値入力
   └─ Table // モダン コントロールのテーブル

image.png

今までどおりですね。
関数を設定するところはButtonPicture

OnSelect
Set(Response,'AzureOpenAI-GPT4oReceiptRead'.Run(Substitute(JSON(Image.Image,JSONFormat.IncludeBinaryData),"""","")));

詳細はPower Apps & GPT-4oを使って超高速で画像解析アプリを作る!を見てください!!
泣いて喜びます!

グローバル変数であるResponseにオブジェクトを返します。
モダン コントロールのテーブルに、明細を表示させたいため、応答で、値を取得しています。

Power Automate

あとはプロンプトの改修で、事足ります。

OpenAIのAPIと異なり、modelのパラメーターが不要です。

■ HTTP

キー
URI @{body('GetSecret_ENDPOINT')?['EnvironmentVariableSecretValue']}openai/deployments/@{body('GetSecret_DEPLOYMENT')?['EnvironmentVariableSecretValue']}/chat/completions?api-version=2024-02-15-preview
Method POST

ヘッダー

キー
Content-Type application/json
Authorization @{body('GetSecret_APIKEY')?['EnvironmentVariableSecretValue']}

今回はsystemプロンプトを除き、下記のように設定しました。

HTTP送信、本文
{
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": {
            "url": "@{triggerBody()?['text']}",
            "detail": "high"
          }
        },
        {
          "type": "text",
          "text": "Read the following receipt image and extract the relevant details. The details should be structured in a JSON format as follows: ## JSON Schema  { \"date\": \"\", \"total_amount\": \"\", \"store_name\": \"\", \"items\": [ { \"name\": \"\", \"quantity\": \"\", \"price\": \"\" } ] }"
        }
      ]
    }
  ],
  "max_tokens": 800,
  "temperature": 0.7,
  "frequency_penalty": 0,
  "presence_penalty": 0,
  "top_p": 0.95,
  "response_format": {
    "type": "json_object"
  }
}

{triggerBody()?['text']}に、base64文字列を送信します。

戻り値の設定は、

 ## JSON Schema  { \"date\": \"\", \"total_amount\": \"\", \"store_name\": \"\", \"items\": [ { \"name\": \"\", \"quantity\": \"\", \"price\": \"\" } ] }"

上記で完了しています。
上記の中で、total_amountや、quantitypriceといった属性は、データ型の評価がstringnumber、場合によって変わるため、応答アクションのスキーマを工夫します。

Schema
{
    "type": "object",
    "properties": {
        "date": {
            "type": "string"
        },
        "total_amount": {
            "type": "string"
        },
        "store_name": {
            "type": "string"
        },
        "items": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "name": {
                        "type": "string"
                    },
                    "quantity": {
                        "type": [
                            "string",
                            "number",
                            "null"
                        ]
                    },
                    "price": {
                        "type": [
                            "string",
                            "number",
                            "null"
                        ]
                    }
                },
                "required": [
                    "name",
                    "quantity",
                    "price"
                ]
            }
        }
    }
}

値の評価を、nullnumberOKに設定しています。
これによって、エラーを防ぐことにつながります。

結構、精度が良さげ

シンプルにGPT-4oに、画像を渡しているだけですが、意外と読み取れていることがわかります。
ポイントは事前準備が、ほぼないことです。

恐ろしい精度!
いくつか領収書を使って、どれだけのパターンに耐えうるか、調査してみたいと思います。

OCRビジネスが、ひっくり返る可能性もありますね!ではまた!

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?