はじめに
OpenAI のToken を節約で考えられる方法のひとつとして、日本語を英語に翻訳してからOpenAIにクエリを送信する方法が考えられます。
特に日本語は英語よりもTokenを消費します。これを解消する方法です。
例えば、「こんにちは」この挨拶のTokenを計算してみます。
Tokenは6になります。これを「Hello」で入力すると、Tokenは1です。
単純に日本語の場合、Tokenは6倍になっていることが分かります。
そこでLogic Appsを利用して日本語を英語に翻訳してから OpenAIにクエリを送信する方法を紹介します。
前回は図のようにLogic Appsで OpenAIにクエリを送信する方法は紹介しました。

これを応用して途中で翻訳しOpenAIにクエリを送信します。

注意すべきこととして英訳したものを再度和訳するため、どうしても日本語としてのニュアンスなど、精度が落ちるような気がします。
Microsoft Translator を準備
Logic AppsではAzure Cognitive Service のテキスト翻訳(Microsoft Translator)と連携し、英訳を行います。
必要事項を入力して作成します。テキスト翻訳の準備はこれで完了です。

Logic Apps
全体像は下記です。
HTTPでクエリをGETで受け取ったあと、日本語(分岐左側)と英語(分岐右側)でぞれぞれOpenAIにクエリを送信しています。これはTokenの比較を行うためです。
最後に応答として結果を表示しています。

最初の部分と右側の部分は下記で構成したものと同じものです。いらないと思うので削除すると良いでしょう。
英語(分岐右側)でクエリを送信する方を紹介していきます。
テキスト翻訳を追加していきます。
Translatorと検索するとMicrosoft Translator V2が出てきます。これを選択します。

アクションとしてテキストの翻訳を先約します。

次に接続を設定します。前項で作成したテキスト翻訳の名前を入力します。

あとは、英語に翻訳する設定をおこない、入力する文字を選択します。

以下のOpenAIへの送信、JSONの解析は下記から詳細を確認してください。
翻訳されたPromptをOpenAIに送信します。ここでは送信するものは上記で翻訳された文章です。

JSON の解析と作成を行います。

出力されたものを再度和訳します。

最後にこれらを応答として出力します。
例ではTokenがわかりやすいように表示しています。

---------------------------------
入力された文字
@{body('JSON_の解析')?['body']}
---------------------------------
日本語ままで利用した場合のPromptトークン
@{body('JSON_の解析_3')?['usage']?['prompt_tokens']}
日本語ままで利用した場合のトータルトークン
@{body('JSON_の解析_3')?['usage']?['total_tokens']}
---------------------------------
翻訳された入力文字
@{body('テキストの翻訳')}
翻訳された場合のPromptトークン
@{body('JSON_の解析_2')?['usage']?['prompt_tokens']}
翻訳された場合のトータルトークン
@{body('JSON_の解析_2')?['usage']?['total_tokens']}
---------------------------------
日本語入力された場合のOpenAIからの回答
@{outputs('作成_3')}
翻訳された入力文字の場合のOpenAIからの回答
@{outputs('作成_2')}
翻訳された回答
@{body('テキストの翻訳_2')}
これでLogic Appsの作成は完了です。
実際にアクセスして確認してみます。英訳した場合はTokenが節約できることが分かります。

まとめ
Logic Appsと Cognitive Serviceを組み合わせてTokenを節約する方法を紹介しました。簡単に節約できる分、精度が落ちる可能性があることも考慮する必要があります。また、Tokenの費用と Cognitive Serviceの費用で釣り合わない場合はそのまま日本語で入力して利用した方が良いと思います。
これは、あくまで方策のひとつと考えてもらえたらを思います。
参考
コードビューです。
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"HTTP": {
"inputs": {
"body": {
"frequency_penalty": 0,
"max_tokens": 800,
"messages": [
{
"content": "@{body('テキストの翻訳')}",
"role": "user"
}
],
"presence_penalty": 0,
"stop": null,
"temperature": 0.7,
"top_p": 0.95
},
"headers": {
"Content-Type": "application/json",
"api-key": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"method": "POST",
"uri": "https://xxxxxxxxxx.openai.azure.com/openai/deployments/xxxxxxx/chat/completions?api-version=2023-03-15-preview"
},
"runAfter": {
"テキストの翻訳": [
"Succeeded"
]
},
"type": "Http"
},
"HTTP_2": {
"inputs": {
"body": {
"frequency_penalty": 0,
"max_tokens": 800,
"messages": [
{
"content": "@{body('JSON_の解析')?['body']}",
"role": "user"
}
],
"presence_penalty": 0,
"stop": null,
"temperature": 0.7,
"top_p": 0.95
},
"headers": {
"Content-Type": "application/json",
"api-key": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"method": "POST",
"uri": "https://xxxxxxxxx.openai.azure.com/openai/deployments/xxxxxx/chat/completions?api-version=2023-03-15-preview"
},
"runAfter": {
"JSON_の解析": [
"Succeeded"
]
},
"type": "Http"
},
"JSON_の解析": {
"inputs": {
"content": "@outputs('作成')",
"schema": {
"properties": {
"body": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"作成": [
"Succeeded"
]
},
"type": "ParseJson"
},
"JSON_の解析_2": {
"inputs": {
"content": "@body('HTTP')",
"schema": {
"properties": {
"choices": {
"items": {
"properties": {
"finish_reason": {
"type": "string"
},
"index": {
"type": "integer"
},
"message": {
"properties": {
"content": {
"type": "string"
},
"role": {
"type": "string"
}
},
"type": "object"
}
},
"required": [
"index",
"finish_reason",
"message"
],
"type": "object"
},
"type": "array"
},
"created": {
"type": "integer"
},
"id": {
"type": "string"
},
"model": {
"type": "string"
},
"object": {
"type": "string"
},
"usage": {
"properties": {
"completion_tokens": {
"type": "integer"
},
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
}
},
"runAfter": {
"HTTP": [
"Succeeded"
]
},
"type": "ParseJson"
},
"JSON_の解析_3": {
"inputs": {
"content": "@body('HTTP_2')",
"schema": {
"properties": {
"choices": {
"items": {
"properties": {
"finish_reason": {},
"index": {
"type": "integer"
},
"message": {
"properties": {
"content": {
"type": "string"
},
"role": {
"type": "string"
}
},
"type": "object"
}
},
"required": [
"index",
"finish_reason",
"message"
],
"type": "object"
},
"type": "array"
},
"created": {
"type": "integer"
},
"id": {
"type": "string"
},
"model": {
"type": "string"
},
"object": {
"type": "string"
},
"usage": {
"properties": {
"completion_tokens": {
"type": "integer"
},
"prompt_tokens": {
"type": "integer"
},
"total_tokens": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
}
},
"runAfter": {
"HTTP_2": [
"Succeeded"
]
},
"type": "ParseJson"
},
"テキストの翻訳": {
"inputs": {
"body": {
"Text": "@body('JSON_の解析')?['body']"
},
"host": {
"connection": {
"name": "@parameters('$connections')['translatorv2']['connectionId']"
}
},
"method": "post",
"path": "/Translate",
"queries": {
"to": "en"
}
},
"runAfter": {
"JSON_の解析": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"テキストの翻訳_2": {
"inputs": {
"body": {
"Text": "@{outputs('作成_2')}"
},
"host": {
"connection": {
"name": "@parameters('$connections')['translatorv2']['connectionId']"
}
},
"method": "post",
"path": "/Translate",
"queries": {
"to": "ja"
}
},
"runAfter": {
"作成_2": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"作成": {
"inputs": "@triggerOutputs()['queries']",
"runAfter": {},
"type": "Compose"
},
"作成_2": {
"inputs": "@trim(outputs('JSON_の解析_2')?['body']?['choices'][0]?['message']?['content'])",
"runAfter": {
"JSON_の解析_2": [
"Succeeded"
]
},
"type": "Compose"
},
"作成_3": {
"inputs": "@trim(outputs('JSON_の解析_3')?['body']?['choices'][0]?['message']?['content'])",
"runAfter": {
"JSON_の解析_3": [
"Succeeded"
]
},
"type": "Compose"
},
"応答": {
"inputs": {
"body": "---------------------------------\n入力された文字\n@{body('JSON_の解析')?['body']}\n---------------------------------\n\n日本語ままで利用した場合のPromptトークン\n@{body('JSON_の解析_3')?['usage']?['prompt_tokens']}\n\n日本語ままで利用した場合のトータルトークン\n@{body('JSON_の解析_3')?['usage']?['total_tokens']}\n\n---------------------------------\n翻訳された入力文字\n@{body('テキストの翻訳')}\n\n翻訳された場合のPromptトークン\n@{body('JSON_の解析_2')?['usage']?['prompt_tokens']}\n\n翻訳された場合のトータルトークン\n@{body('JSON_の解析_2')?['usage']?['total_tokens']}\n\n---------------------------------\n\n日本語入力された場合のOpenAIからの回答\n@{outputs('作成_3')}\n\n翻訳された入力文字の場合のOpenAIからの回答\n@{outputs('作成_2')}\n\n翻訳された回答\n@{body('テキストの翻訳_2')}\n",
"statusCode": 200
},
"kind": "Http",
"runAfter": {
"テキストの翻訳_2": [
"Succeeded"
],
"作成_3": [
"Succeeded"
]
},
"type": "Response"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"request": {
"inputs": {
"method": "GET",
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"translatorv2": {
"connectionId": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ChatLogicApps/providers/Microsoft.Web/connections/translatorv2",
"connectionName": "translatorv2",
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Web/locations/eastus/managedApis/translatorv2"
}
}
}
}
}
