0
0

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 AI Foundry プロンプトフロー(Prompt Flow) 処理フロー作成

Posted at

Azure AI Foundryのプロンプトフロー(Prompt Flow)を利用した処理実行について、以下の処理フローを作成し動作することを確認します。

処理 概要
単純処理 input・処理・outputのみの最も簡潔なフロー
並列処理と結合 1つのinputをそれぞれ2つの処理を行い、2つ結果を結合してoutputするフロー
処理分岐と結合 条件分岐が発生し、分岐後に実行された処理結果をoutputするフロー

image.png

Azure AI Foundry/Prompt Flowを使用するために以下を事前に行っています。

https://qiita.com/Higemal/items/704a245125b71994b563
→Azure OpenAI Serviceのリソース作成と生成AIモデルのデプロイまで

https://qiita.com/Higemal/items/b79a2707739efe3b5479
→Azure AI Foundryのプロジェクト作成まで

本投稿では処理フローの作成に重点を置くため、処理はすべてpythonとし、LLMは使用しません。

1. 単純処理

最もシンプルなフローとして、input/処理/outputだけのフローを作成します。
image.png

処理概要

処理 概要
input int型の変数numberを準備し、変数に整数を代入
process1 inputの内容によって異なった文字列をreturnする処理
output 処理process1のreturnを受け取る変数colorを配置

処理詳細

以下3か所について記述します。
・input
・process1
・output

input

入力部にはinputとして利用する変数と、その変数値を入力します。

名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 1

image.png

process1

処理本体と、先行処理とのインプット連携を行います。

①コード

コード本体になります。

process1
from promptflow import tool

@tool

def get_color(number: int) -> str:
    if number == 1:
        return "red"
    elif number == 2:
        return "blue"
    elif number == 3:
        return "green"
    else:
        return "black"
②入力

どの入力内容を処理のインプットとするかを定義する箇所です。
こちらを定義することで、グラフ上の矢印が引かれます。

名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 ${inputs.number}

${inputs.number} と表現することで、input部の変数nubmerを入力値とすることを指定しています。

image.png

output

出力部にはoutputとして利用する変数と、その入力元を指定します。

名称 用途
名前 変数名 color
変数の値 ${process1.output}

${process1.output} と表現することで、process1部のreturnを入力値とすることを指定しています。

image.png

処理結果

処理を実行し、結果を確認します。

入力変数number1に対し、出力変数colorredを得ることができました。

image.png



2. 並列処理と結合

次に、1つの入力から別の2つの処理を行い、2つの処理結果を結合して1の出力とする並列・結合のフローを作成します。
image.png

処理概要

処理 概要
input int型の変数numberを準備し、変数に整数を代入
process1 inputの内容によって異なった文字列をreturnする処理
process2 inputの内容によって異なった文字列をreturnする処理
join1 process1とprocess2の処理結果を結合する処理
output 処理join1のreturnを受け取る変数colorを配置

処理詳細

以下4か所について記述します。
・input
・process1/process2
・join
・output

input

入力部にはinputとして利用する変数と、その変数値を入力します。

名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 2

image.png

process1/process2

処理本体と、先行処理とのインプット連携を行います。
process1とprocess2の違いはコードだけとなり、入力部は同様とすることでグラフ上の並列分岐を実現しています。

①コード
process1
from promptflow import tool

@tool

def get_color(number: int) -> str:
    if number == 1:
        return "red"
    elif number == 2:
        return "blue"
    elif number == 3:
        return "green"
    else:
        return "black"
process2
from promptflow import tool

@tool

def get_color(number: int) -> str:
    if number == 1:
        return "book"
    elif number == 2:
        return "sky"
    elif number == 3:
        return "apple"
    else:
        return "lotus"
②入力
名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 ${inputs.number}

image.png
image.png

join1

先行処理の結果を結合する処理になります。

①コード
join1
from promptflow import tool

@tool

def join_str(output1: str,output2: str) -> str:
    return output1 + " " + output2
②入力

2つの処理結果について、2つの入力変数を準備することでグラフ上の結合を表現しています。

名称 用途
名前 変数名 output1
種類 変数の型 string
変数の値 ${process1.output}
名称 用途
名前 変数名 output2
種類 変数の型 string
変数の値 ${process2.output}

image.png

output

出力部にはoutputとして利用する変数と、その入力元を指定します。

名称 用途
名前 変数名 words
変数の値 ${join1.output}

image.png

処理結果

処理を実行し、結果を確認します。

入力変数number2に対し、出力変数wordsblue skyを得ることができました。

image.png



3. 処理分岐と結合

最後に、1つの入力から処理分岐を行い、分岐経路の結果を出力するフローを作成します。
image.png

処理分岐とバイパスについて

プロンプトフローで処理分岐を行う際は、各処理の構成のアクティブ化を利用するかたちとなります。
構成のアクティブ化は、各処理がアクティブとなる条件を記述しておくものとなり、その条件を満たす場合は処理を、満たさない場合はbypass扱いとなり処理が実行されません。
構成のアクティブ化が付与された処理は、再生ボタンのようなマークがグラフ内に表現されます)
image.png

そのため、今回は構成のアクティブ化の判定ロジックとなる処理check1を準備してフロー連携を実現します。

処理概要

処理 概要
input int型の変数numberを準備し、変数に整数を代入
check1 int型の変数numberによって、true/falseを出力する処理
process1 inputの内容によって異なった文字列をreturnする処理、ただしcheck1処理により処理/bypassが決まる
process2 inputの内容によって異なった文字列をreturnする処理、ただしcheck1処理により処理/bypassが決まる
join1 process1とprocess2の処理結果を結合する処理
output 処理join1のreturnを受け取る変数colorを配置

処理詳細

以下5か所について記述します。
・input
・check1
・process1/process2
・join
・output

input

入力部にはinputとして利用する変数と、その変数値を入力します。

名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 3

image.png

check1

後続処理の実行判定/構成のアクティブ化のためのロジックになります。
入力数値が2以下の場合はtrue、そうでなけれはfalseを応答します。

①コード
check1
from promptflow import tool

@tool

def check(n: int) -> bool:
    return n <= 2
②入力
名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 ${inputs.number}

process1/process2

処理本体と、先行処理とのインプット連携を行います。
どちらの処理も入力値はinput部から、構成のアクティブ化はcheck1部から得ます。

①コード
process1
from promptflow import tool

@tool

def get_color(number: int) -> str:
    if number == 1:
        return "red"
    elif number == 2:
        return "blue"
    elif number == 3:
        return "green"
    else:
        return "black"
process2
from promptflow import tool

@tool

def get_color(number: int) -> str:
    if number == 1:
        return "book"
    elif number == 2:
        return "sky"
    elif number == 3:
        return "apple"
    else:
        return "lotus"
②入力
名称 用途
名前 変数名 number
種類 変数の型 int
変数の値 ${inputs.number}
③構成のアクティブ化

<process1>

名称 用途
タイミング ${check1.output}
(型) bool
(値) True

<process2>

名称 用途
タイミング ${check1.output}
(型) bool
(値) False

image.png
image.png

join1

先行処理の結果を結合する処理になります。
条件分岐により、2つの入力のうち片方はnull(None)となるため、非nullの入力を回答するような処理になります。

①コード
join1
from promptflow import tool

@tool

def join_str(output1: str,output2: str) -> str:
    if output1 is not None:
        return output1
    else:
        return output2
②入力

2つの処理結果について、2つの入力変数を準備することでグラフ上の結合を表現しています。

名称 用途
名前 変数名 output1
種類 変数の型 string
変数の値 ${process1.output}
名称 用途
名前 変数名 output2
種類 変数の型 string
変数の値 ${process2.output}

image.png

output

出力部にはoutputとして利用する変数と、その入力元を指定します。

名称 用途
名前 変数名 out
変数の値 ${join1.output}

image.png

処理結果

処理を実行し、結果を確認します。

入力変数number3に対し、出力変数outappleを得ることができました。

image.png


また、条件分岐によりprocess1bypassedとなっていることも確認できました。
image.png

最後に

条件分岐については改善の余地がある気がするので、思いついたら実践してみようと思います。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?