はじめに
みなさん、こんにちは。俊介です。
今回はAzure OpenAIをAPIを使ってWorkato経由で操作できるようカスタムコネクターをWorkato上で作成しました。
これを実装することで今までWorkatoで作成した業務のプロセスに生成AIを組み込む事ができ、さらなる業務の効率化が広がります。
簡単な例で言うと、SlackとAzure OpenAIを繋げることがで、本家ChatGPTのウェブアプリを開かなくてもSlackで生成AIに質問を投げれるようになります。
前提として
Azure OpenAI Serviceはセットアップ済みであること
Workato Connector SDKで実際に作成していく
Workato Connector SDKとは
Workatoで標準で用意していないConnectionを自身で作成することが出来ます。(Rubyで書いていくことになります。)
全体のブロック構成
それぞれのブロックの内容は後続で紹介します。
{ title: "カスタムコネクターの名前", connection: {"APIとの接続の確立に関わる情報を定義"}, test: "connectionキーと連携し、接続が正常に確立されたことを確認", object_definitions: {"入力項目や出力項目を定義。項目は、アクションやトリガーにおいて参照可"}, actions: {"コネクターに含まれるすべてのアクションの定義"} }
connectionブロック
connection: { fields: [ { name: "api_key", hint: "Azure OpenAIで作成したAPIキーを設定してください", label: "Api key" }, { name: "deployment_id", hint: "deployment-idか、engineを指定してください", }, { name: "openai_api_base", hint: "Azure OpenAI名を設定してください" } ], authorization: { type: "custom_auth", apply: lambda do |connection| headers("api-key": "#{connection["api_key"]}") end }, base_uri: lambda do |connection| "https://#{connection["openai_api_base"]}/openai/" end }
Azure OpenAI APIの認証方法は2つあります。
- API キー認証:api-key HTTP ヘッダーに API キーを含めてリクエストを投げる
- Azure Active Directory 認証(OAuth認証):ユーザーの資格情報を使用してリクエストを投げる
今回はAPIキーでの認証を採用しました。
▼Workato DebugのConnection画面

▼Connectionに項目を埋める為のAzure AD側の設定確認画面

testブロック
test: lambda do |connection| post("deployments/#{connection["deployment_id"]}/chat/completions?api-version=2023-05-15"). payload( {messages:[{role: "system", content: "You are a helpful assistant."}]} ) end
▼testブロックまで書いたら認証が通るか「Connect」した結果

object_definitionsブロック
object_definitions: { ## Azure OpenAIのチャットをAPI経由で行うためのinput_fieldsを定義 create_chat_completion_input_fields: { fields: lambda do |_connection, object_definitions| [ { name: "messages", type: "array", of: "object", hint: "An array of messages to generate chat completions for", properties: [ { name: "role", default: "user", optional: false, hint: "either 'system', 'user', or 'assistant'" }, { name: "content", optional: false, hint: "The message to generate a chat completion for" } ] } ] end }, ## Azure OpenAIのチャットをAPI経由で行うためのoutput_fieldsを定義 create_chat_completion_output_fields: { fields: lambda do |_connection, object_definitions| [ { control_type: "text", label: "ID", type: "string", name: "id" }, { control_type: "text", label: "Object", type: "string", name: "object" }, { control_type: "number", label: "Created", parse_output: "float_conversion", type: "number", name: "created" }, { control_type: "text", label: "Model", type: "string", name: "model" }, { name: "choices", type: "array", of: "object", label: "Choices", properties: [ { control_type: "number", label: "Index", parse_output: "float_conversion", type: "number", name: "index" }, { control_type: "text", label: "Finish reason", type: "string", name: "finish_reason" }, { properties: [ { control_type: "text", label: "Role", type: "string", name: "role" }, { control_type: "text", label: "Content", type: "string", name: "content" } ], label: "Message", type: "object", name: "message" } ] }, { properties: [ { control_type: "number", label: "Completion tokens", parse_output: "float_conversion", type: "number", name: "completion_tokens" }, { control_type: "number", label: "Prompt tokens", parse_output: "float_conversion", type: "number", name: "prompt_tokens" }, { control_type: "number", label: "Total tokens", parse_output: "float_conversion", type: "number", name: "total_tokens" } ], label: "Usage", type: "object", name: "usage" } ] end } }
actionsブロック
actions: { ## Azure OpenAIのチャットをAPI経由で行うためのaction chat: { title: "Azure OpenAIにテキストを投げる", description: "ChatGPT および GPT-4 モデルを使用してチャット メッセージの補完を作成します。", input_fields: lambda do |object_definitions| object_definitions["create_chat_completion_input_fields"] end, execute: lambda do |connection, input| post("deployments/#{connection["deployment_id"]}/chat/completions?api-version=2023-05-15", input) end, output_fields: lambda do |object_definitions| object_definitions["create_chat_completion_output_fields"] end } }
▼Workato DebugのActions画面

▼実際にDebugからテスト実行する時の画面

▼テスト実行後のConsle画面に結果が返ってくる

実際に使ってみた
作成したカスタムコネクターを実際にレシピに利用してみたいと思います。
流れは簡単です。
- SlackでApp Botをメンションして質問を投げます
- Workatoが発火し、フローが開始します
- Azure OpenAIにリクエストを投げます
- 返ってきたレスポンス結果をSlackに投稿させます

例1

例2

おわりに
今回はWorkatoでもAzure OpenAIの操作を出来るようにカスタムコネクターを作成しました。
カスタムコネクターを作っておけば、Slackだけでなく、他アプリとの連携も簡単になるので、いろんな自動化にも組み込みやすくなると思います!
次のブログからは今回のカスタムコネクターを利用して、色々なSaaSと連携させてAzure OpenAIを活用した事例を紹介していきます。