FileMaker と ChatGPT を連携してみた (画像編)
OpenAI が展開している ChatGPT などの AI が巷を賑わせている。
今回は、Claris FileMaker で作成されたカスタム App に格納されている画像を ChatGPT を利用して類推してみたという備忘録です。
1. OpenAI のアカウントの作成
![](https://assets.st-note.com/img/1714124428532-sdHcVqq6Ho.png?width=1200)
![](https://assets.st-note.com/img/1714124680725-SWiKRN2Afj.png?width=1200)
![](https://assets.st-note.com/img/1714124839115-eGcWjb5Ezp.png?width=1200)
![](https://assets.st-note.com/img/1714124926228-5BNfcM3aG2.png?width=1200)
![](https://assets.st-note.com/img/1714125091383-bQgKGBvkcM.png?width=1200)
![](https://assets.st-note.com/img/1714125280217-14X6FI6bsq.png?width=1200)
![](https://assets.st-note.com/img/1714125384231-CIwXrn8YYU.jpg?width=1200)
![](https://assets.st-note.com/img/1714125531386-yrbO8jRWKK.jpg?width=1200)
![](https://assets.st-note.com/img/1714125700983-4w7AyhBtxe.png?width=1200)
![](https://assets.st-note.com/img/1714125817772-acTEDi3brN.jpg?width=1200)
![](https://assets.st-note.com/img/1714128632004-EKwQW85YZy.png?width=1200)
2. 支払い情報の設定
![](https://assets.st-note.com/img/1714127241252-A4zI0PWlF1.png?width=1200)
![](https://assets.st-note.com/img/1714127342857-WCYUjVu19Z.png?width=1200)
![](https://assets.st-note.com/img/1714127376826-s9ZOuhQ4mL.png?width=1200)
![](https://assets.st-note.com/img/1714127499786-bLEEoKPhmF.png?width=1200)
![](https://assets.st-note.com/img/1714127651335-0dBNPupiLK.png?width=1200)
![](https://assets.st-note.com/img/1714127699794-GDbUYBw7um.png?width=1200)
3. SECRET KEYの取得
![](https://assets.st-note.com/img/1714128281058-403xE83k0x.png?width=1200)
![](https://assets.st-note.com/img/1714128339106-ZO2iaJZgE6.png?width=1200)
![](https://assets.st-note.com/img/1714128387181-NMigju4Rbh.png?width=1200)
![](https://assets.st-note.com/img/1714128456978-l5F3ETcZPC.png?width=1200)
4. FileMaker Pro でカスタム App を作成
![](https://assets.st-note.com/img/1714128895215-67I6FLY7oR.png?width=1200)
カスタム関数の作成 (任意)
![](https://assets.st-note.com/img/1714129040290-SNyX3PTa6v.png?width=1200)
スクリプトの作成
# 画像を認識させるスクリプト in file 画像を認識させる
# ChatGPT に画像を判断してもらう。
# 20240426 p388cell
#
#
ウインドウの固定
ユーザによる強制終了を許可 [ オン ]
#
変数を設定 [ $json ; 値: Get ( スクリプト引数 ) ]
変数を設定 [ $prompt ; 値: JSONGetElement ( $json ; "prompt" ) ]
変数を設定 [ $field_name ; 値: JSONGetElement ( $json ; "field_name" ) ]
変数を設定 [ $base64 ; 値: JSONGetElement ( $json ; "base64" ) ]
変数を設定 [ $OPENAI_API_KEY ; 値: CF_API_KEY ]
#
変数を設定 [ $model ; 値: "gpt-4-vision-preview" ]
変数を設定 [ $temperature ; 値: .7 ]
変数を設定 [ $role ; 値: "user" ]
変数を設定 [ $extension ; 値: Let( [ ~filename = GetContainerAttribute( 画像を認識させる::画像ファイル; "filename" ) ]; GetValue( Substitute( ~filename; "."; ¶); PatternCount( ~filename; ".") + 1 ) ) ]
#
#
#
If [ IsEmpty ( $json ) ]
カスタムダイアログを表示 [ "確認" ; "スクリプト引数が設定されていません。" ]
現在のスクリプト終了 [ テキスト結果: False ]
Else If [ IsEmpty ( $prompt ) ]
カスタムダイアログを表示 [ "確認" ; "プロンプトが設定されていません。" ]
現在のスクリプト終了 [ テキスト結果: False ]
Else If [ IsEmpty ( $field_name ) ]
カスタムダイアログを表示 [ "確認" ; "結果を格納するフィールドが指定されていません。" ]
現在のスクリプト終了 [ テキスト結果: False ]
End If
#
Loop [ フラッシュ: 常に ]
Exit Loop If [ not IsEmpty ( $OPENAI_API_KEY ) or $last_message_choice = 2 ]
カスタムダイアログを表示 [ "確認" ; "OPENAI_API_KEYが設定されていません。openai のサイトに移動して、API key を取得してください。" ; $OPENAI_API_KEY ]
変数を設定 [ $last_message_choice ; 値: Get ( 最終メッセージ選択 ) ]
End Loop
#
If [ $last_message_choice = 2 ]
現在のスクリプト終了 [ テキスト結果: False ]
End If
#
変数を設定 [ $url ; 値: "https://api.openai.com/v1/chat/completions" ]
変数を設定 [ $payload ; 値: JSONSetElement( $payload; [ "model"; $model; JSONString ]) ]
変数を設定 [ $payload ; 値: JSONSetElement( $payload;[ "temperature"; $temperature; JSONNumber ]) ]
If [ IsEmpty ( $base64 ) ]
変数を設定 [ $payload ; 値: JSONSetElement( $payload; [ "messages[0]role"; $role; JSONString ]; [ "messages[0]content"; $prompt; JSONString ] ) ]
Else
変数を設定 [ $payload ; 値: JSONSetElement( $payload; [ "messages[0]role"; $role; JSONString ]; [ "messages[0]content[0]type"; "text"; JSONString ]; [ "messages[0]content[0]text"; $prompt; JSONString ]; [ "messages[0]content[1]type"; "image_url"; JSO… ]
End If
#
テキストを挿入 [ 選択 ; ターゲット: $cURL ; 「curl --location --request POST 'https://api.openai.com/v1/completions' \ --header "Content-Type: application/json" \ --header "Authorization: Bearer $OPENAI_API_KEY" \ -d @$payload」 ]
変数を設定 [ $cURL ; 値: Substitute( $cURL; [ "$OPENAI_API_KEY"; $OPENAI_API_KEY ]; [ "_____prompt_____"; $prompt ] ) ]
URL から挿入 [ 選択 ; ダイアログあり: オフ ; ターゲット: $result ; $url ; cURL オプション: $cURL ]
#
変数を設定 [ $error ; 値: JSONGetElement ( $result ; "error.message" ) ]
#
If [ not IsEmpty ( $error ) ]
カスタムダイアログを表示 [ $error ]
現在のスクリプト終了 [ テキスト結果: False ]
End If
#
フィールドを名前で設定 [ $field_name ; JSONGetElement ( $result ; "choices[0].message.content" ) ]
#
レコード/検索条件確定 [ ダイアログあり: オフ ]
#
現在のスクリプト終了 [ テキスト結果: True ]
#
#
#
#
#
#
#
#
引数は次のように渡します。
![](https://assets.st-note.com/img/1714130522940-Pu7D5sVA0j.png?width=1200)
JSONSetElement(
"";
[
"prompt";
"この画像について説明してください。";
JSONString
];
[
"field_name";
GetFieldName( 画像を認識させる::結果);
JSONString
];
[
"base64";
Base64Encode( 画像を認識させる::画像ファイル);
JSONString
]
)
![](https://assets.st-note.com/img/1714130745818-paU3opRvjw.png?width=1200)
サンプル
SECRET KEY は自分のものを利用してください。
参考にさせていただいたサイト
その後見つけた情報
ドンピシャ(ChatGPTとFileMakerと画像の連携)の情報でした。
お金
まだ少ししか試していない。意外とコストは低いかもしれない?
![](https://assets.st-note.com/img/1714131256389-ENJBfW3Bhs.png?width=1200)
以上、備忘録でした。
この記事が気に入ったらサポートをしてみませんか?