LoginSignup
6
4

[2024.03.12更新!]Azure OpenAIで画像生成AI「DALL-E」がAPIで実行できるようになったので試す

Last updated at Posted at 2023-09-06

2024.03.12Update!!

SNSで仲良くさせていただいている方から、質問でこの記事の情報がDALL E3には対応していないことがわかりました。

せっかくなのでDALL-E 3のフローを追記したいと思います。

2024.03.12時点の実証情報です

ざっくり手順

  1. DALL-E 3に対応したリージョンでモデルをデプロイ(手順は省きます)
  2. Power Automateの設定

DALL-E 3に対応している地域(2024.03.12時点)

  • SwedenCentral リージョンに作成された Azure OpenAI リソース

DALL-E 3の場合、APIを利用した際の戻り値が異なります。
以前の私の記事で進めると、ここが罠になりますのでご注意ください。

シンプルなPower Automateの例

  1. Power AppsからPromptを送信
  2. 作成アクションでエンドポイントを設定
    1. Secure InputsをOn
  3. 作成アクションでAPIキーを設定
    1. Secure InputsをOn
  4. Azure OpenAIにHTTP要求を送信
  5. JSONの解析
  6. Power Appsに、生成された画像のURLを渡す

image.png

DALL-E 2との違い

APIの戻り値のみ異なります。HTTP 詳細は割愛。
DALL-E 2は一度要求を送信し、画像が生成されるまでステータスを確認し、作成完了まで待機する必要がありましたが、DALL-E 3 は不要になります!

上記が起因し、フローも非常にシンプルです。

スキーマはこちら!

Shema.json
{
    "type": "object",
    "properties": {
        "created": {
            "type": "integer"
        },
        "data": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "content_filter_results": {
                        "type": "object",
                        "properties": {
                            "hate": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            },
                            "self_harm": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            },
                            "sexual": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            },
                            "violence": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    },
                    "prompt_filter_results": {
                        "type": "object",
                        "properties": {
                            "hate": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            },
                            "profanity": {
                                "type": "object",
                                "properties": {
                                    "detected": {
                                        "type": "boolean"
                                    },
                                    "filtered": {
                                        "type": "boolean"
                                    }
                                }
                            },
                            "self_harm": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            },
                            "sexual": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            },
                            "violence": {
                                "type": "object",
                                "properties": {
                                    "filtered": {
                                        "type": "boolean"
                                    },
                                    "severity": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    },
                    "revised_prompt": {
                        "type": "string"
                    },
                    "url": {
                        "type": "string"
                    }
                },
                "required": [
                    "content_filter_results",
                    "prompt_filter_results",
                    "revised_prompt",
                    "url"
                ]
            }
        }
    }
}

Power Appsに生成された画像のURLを返す場合、戻り値が原則Arrayになっていますので、
一枚物を想定し、下記の通り実証では設定しています。

image.png

outputs('JSON_の解析')?['body']?['data'][0]?['url']

上記をイメージコントロールに設定すれば、Power Appsでも表示されます。
是非試してみてください!

2024.03.12更新分はここまで!

■ 以前の記事はこちらから!

以下の記事は DALL E2に対応

2023.09.05に発表されたようですが、画像生成AI「DALL-E」がAPIで実行できるようになりました。

2023.09.05時点ではプレビュー機能です

さっそくなので公式レファレンスを読み解いて、Power Automateで実装したいと思います。

1. Azure OpenAIをデプロイ

まず最初にAzure OpenAIをAzureでデプロイする必要があります。
対象地域は現在eastusのみ(重要

ここらへんは割愛して、デプロイ後から解説します。

まずはKeyとEndPointを取得する

デプロイが完結したら、まずはKeyEndPointを控えます。

image.png

情報漏れを起こさないように注意しましょう

2. レファレンスからAPIの呼び出し方を読み解く

さて、公式レファレンスを読み解いていきましょう。

image.png

Pythonのサンプルコードがあるので、引用・転記します。

import requests
import time
import os

api_base = '<your_endpoint>'  # Enter your endpoint here
api_key = '<your_key>'        # Enter your API key here

# Assign the API version (DALL-E is currently supported for the 2023-06-01-preview API version only)
api_version = '2023-06-01-preview'

# Define the prompt for the image generation
url = f"{api_base}openai/images/generations:submit?api-version={api_version}"
headers= { "api-key": api_key, "Content-Type": "application/json" }
body = {
    "prompt": "a multi-colored umbrella on the beach, disposable camera",  # Enter your prompt text here
    "size": "1024x1024",
    "n": 1
}
submission = requests.post(url, headers=headers, json=body)

# Call the API to generate the image and retrieve the response
operation_location = submission.headers['operation-location']
status = ""
while (status != "succeeded"):
    time.sleep(1)
    response = requests.get(operation_location, headers=headers)
    status = response.json()['status']
image_url = response.json()['result']['data'][0]['url']

print(image_url)

ここから読み解ける手順としては下記の通り。

  1. Azure OpenAIのEndPointを組み合わせたURLにPOSTメソッドでAPIをたたく
  2. 結果となる画像データは即座には出てこないので、opelation_locationに対してGETメソッドを実行し、完了するまで繰り返す。(完了していない場合は待機する)

というイメージになります。

3. Power Automateに書き換えてみる

Power Appsに実装する前提でフローを作ってみます。
エラー処理を考えない、サンプルコードを起こしたフローがコチラ!

■ 全体図

image.png

■ Do Untilの中身

image.png

1. Power AppsからPromptを受け取る

image.png

まずは画像生成AIの肝となるテキストを取得します。
画像の出力する枚数やサイズ感も指定できますが、一旦例として挙げるのはpromptのみ引数とする方法とします。

2. API Keyを変数に格納する

情報漏れにお気をつけください。

image.png

3. 画像生成AI「DALL-E」を実行する

HTTP要求を送信します。

image.png

方法POST

肝心なところはURLですね。

{EndPoint}openai/images/generations:submit?api-version=2023-06-01-preview

{EndPoint}は、AzureのKey and Endpointで控えたエンドポイントに置き換えてください。
この形で組み立てるURLが画像生成AI用のURLになります。

上記の値をそのままURLに全て入れてクエリの部分は無くしても良いですし、?api~を削って、クエリに書く方法でも好みで構いません。

■ ヘッダー

Key Value
api-key 控えたKey
Content-Type application/json

■ クエリ(URLから?api~を省く場合

Key Value
api-varsion 2023-06-01-preview

■ 本文

body
{
  "prompt": "@{triggerBody()['text']}",
  "size": "1024x1024",
  "n": 1
}

Keyに対する値については、レファレンスで見つけきれないため、断定は避けます。
試しながら値を調整してみてください。

画像は即座に生成されない

ある種その通りですが、promptを渡し、画像が生成されるまで一定の時間がかかります
そして結果を取得するために、今回POSTした要求から、operation_locationという値を控えておく必要があります。

この値はヘッダーに含まれる値です。

4. 結果を取得するためのURLを変数に格納する

operation-location
@{outputs('HTTP DALL-Eの開始')['headers']?['operation-location']}

image.png

5. Do Untilの状態を表す変数を宣言する

そして、後続のDo Untilの状態を判定する変数を先に宣言しておきます。

image.png

6. Do Until 画像が生成されるまで待機と処理を繰り返す

operation-locationに繰り返し、HTTP要求を送信し、処理が完了するまでGETメソッドを繰り返します。

image.png

待機時間は別に1秒でなくてもいいと思います。1枚の画像に対して4秒で完了する印象です。

6-1. 結果を取得する

image.png

GETメソッドでoperation-locationに要求を送信します。ヘッダーは前回と同様です。

こちらの戻り値(body)で

@{body('HTTP_Result')?['status']}

statusの値がsucceededになった場合に、処理が完了となります。

7. 結果を返す

結果の画像URL
@body('HTTP_Result')?['result']?['data'][0]?['url']

resultdataに配列で画像のURLが返ってきます。こちらからデータを取得します。
今回はPower Appsで活用することを見込んで、一番最初の値を取得しています。

挙動のイメージ

dall.gif

Power AppsのImageコントロールにURLを設定することで生成された画像を参照できます。
生成AIとPower Appsのコラボ実現ですね!

最後に

いかがでしたでしょうか!?本日レファレンスを見て、
うわ、来てるじゃん! と思ってビックリしました!
進化が早いですね!Function Callingと合わせるとさらに面白そうです。

良いPower Lifeを! Good night!

6
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
6
4