GPTsにDeepL APIを搭載する方法をわかりやすく説明します!

ChatGPT

GPTsは非常に便利なツールですがActionsを使った外部との連携方法についてはあまり記事にされていません。

この記事では、BetterActionsGPTの助けを借りて、DeepL APIのエラーとその解決法についての解説します。この解説から得られた教訓が、あなたの技術的な問題解決に役立つことを願っています。

スポンサーリンク

BetterActionsGPTとは

BetterActionsGPTは、OpenAPI 3.0仕様をYAMLやJSON形式で作成するGPTsです。この GPT はAPIの仕様を書いたURLを貼るだけで、OpenAPI仕様のSchemaを出力します。

このツールを使用して、私はDeepL APIの仕様書をもとにActionsを作成しました。

GPTsの作り方は以下を参考ください。

事前準備 API Keyの取得方法

DeepL APIを利用する前には、まずDeepLからAPI Keyを取得する必要があります

VBAに関する記事ですがAPI Keyの取得方法については以下を参照ください

最初の一歩:Schema作成

BetterActionsGPTは以下の様にJSON,YAMLの指定とURLを記載することでOpenAPI仕様のSchemaを作成してくれます。

Make JSON to get translation: https://www.deepl.com/ja/docs-api/api-access/openapi

できたSchemaは以下です。

Schema

{
“openapi”: “3.0.0”,
“info”: {
“title”: “DeepL Translation API”,
“description”: “DeepL API for integrating translation capabilities into applications.”,
“version”: “1.0.0”
},
“servers”: [
{
“url”: “https://api.deepl.com/v2”,
“description”: “DeepL API server”
}
],
“paths”: {
“/translate”: {
“post”: {
“operationId”: “translateText”,
“summary”: “Translate text from one language to another”,
“description”: “Translates the provided text to the specified target language using DeepL’s translation capabilities.”,
“parameters”: [],
“requestBody”: {
“required”: true,
“content”: {
“application/json”: {
“schema”: {
“type”: “object”,
“properties”: {
“text”: {
“type”: “string”,
“description”: “Text to be translated”
},
“target_lang”: {
“type”: “string”,
“description”: “Target language code”
}
},
“required”: [“text”, “target_lang”]
}
}
}
},
“responses”: {
“200”: {
“description”: “Successful translation”,
“content”: {
“application/json”: {
“schema”: {
“type”: “object”,
“properties”: {
“translations”: {
“type”: “array”,
“items”: {
“type”: “object”,
“properties”: {
“detected_source_language”: {
“type”: “string”,
“description”: “Detected source language code”
},
“text”: {
“type”: “string”,
“description”: “Translated text”
}
}
}
}
}
}
}
}
},
“401”: {
“description”: “Unauthorized access, invalid API key”
},
“429”: {
“description”: “Too many requests, rate limit exceeded”
},
“500”: {
“description”: “Internal server error”
}
},
“security”: [
{
“DeepLAuth”: []
}
]
}
}
},
“components”: {
“securitySchemes”: {
“DeepLAuth”: {
“type”: “apiKey”,
“in”: “header”,
“name”: “Authorization”,
“description”: “API key needed to authorize requests.”
}
}
}
}

しかしこれをActionsのShcemaに貼り付けるだけではうまくいきません。

Schema下に「In components section, schemas subsection is not an object」と出ます。

components schemasをオブジェクトにする

In components section, schemas subsection is not an objectに対しては

「In components section, schemas subsection is not an objectと出ます」

とBetterActionsGPTに記載することで対応可能です。

最初に作ったSchemaにはcomponentsのサブセクションにschemasが存在しません。
しかしこの指示によりcomponentsセクションの下にSchemasセクションを追加してもらえます

できたコードは以下になります。

Schema

{
“openapi”: “3.0.0”,
“info”: {
“title”: “DeepL Translation API”,
“description”: “DeepL API for integrating translation capabilities into applications.”,
“version”: “1.0.0”
},
“servers”: [
{
“url”: “https://api.deepl.com/v2”,
“description”: “DeepL API server”
}
],
“paths”: {
“/translate”: {
“post”: {
“operationId”: “translateText”,
“summary”: “Translate text from one language to another”,
“description”: “Translates the provided text to the specified target language using DeepL’s translation capabilities.”,
“requestBody”: {
“required”: true,
“content”: {
“application/json”: {
“schema”: {
“$ref”: “#/components/schemas/TranslationRequest”
}
}
}
},
“responses”: {
“200”: {
“description”: “Successful translation”,
“content”: {
“application/json”: {
“schema”: {
“$ref”: “#/components/schemas/TranslationResponse”
}
}
}
},
“401”: {
“description”: “Unauthorized access, invalid API key”
},
“429”: {
“description”: “Too many requests, rate limit exceeded”
},
“500”: {
“description”: “Internal server error”
}
},
“security”: [
{
“DeepLAuth”: []
}
]
}
}
},
“components”: {
“schemas”: {
“TranslationRequest”: {
“type”: “object”,
“properties”: {
“text”: {
“type”: “string”,
“description”: “Text to be translated”
},
“target_lang”: {
“type”: “string”,
“description”: “Target language code”
}
},
“required”: [“text”, “target_lang”]
},
“TranslationResponse”: {
“type”: “object”,
“properties”: {
“translations”: {
“type”: “array”,
“items”: {
“$ref”: “#/components/schemas/Translation”
}
}
}
},
“Translation”: {
“type”: “object”,
“properties”: {
“detected_source_language”: {
“type”: “string”,
“description”: “Detected source language code”
},
“text”: {
“type”: “string”,
“description”: “Translated text”
}
}
}
},
“securitySchemes”: {
“DeepLAuth”: {
“type”: “apiKey”,
“in”: “header”,
“name”: “Authorization”,
“description”: “API key needed to authorize requests.”
}
}
}
}

componentsセクションの下にschemasオブジェクトが追加されているのがわかると思います。

これでschema記述の際のエラーはなくなるはずです

認証に必要な情報を入れる

Schemaが完成したのでプレビュー画面で実行します。例えばこんな感じです。

今日はいい天気ですねを英語にして

ただ以下の様に通信エラーが出ます。

>をクリックすると詳細な結果を見ることができます

これは403エラーというものです。エラーが出る原因はいくつかあるのですが今回はAPI Keyを入力していないのでエラーが返ってきています。

調査の結果、AuthorizationヘッダーにDeepL-Auth-Keyを接頭語として付けた後にAPIキーを追加する必要があることが判明しました。

その為ActionsのAuthenticationの中身の設定を以下の様にしました。

Authentication Typeは「API Key」,
API Keyは「DeepL-Auth-Key あなたのAPIキー」,
Auth Typeは「Custom」、Header Nameは「Authorization」になります。

DeepL-Auth-Key あなたのAPIキーのKeyの後は半角スペースを入れることを忘れないでください。

これで認証に関する問題は解消されます。

エンドポイントの変更

再度プロンプトを入れて実行しますが以下のエラーが出ます。

このエラーはDeepL APIの無料版を使用している場合に出るエラーです。

無料版と有料版のDeepL APIではエンドポイントが異なるため、今回は無料版のエンドポイントに変更する必要があります。

以下のServers セクションのURLを https://api.deepl.com から https://api-free.deepl.com に変更することで、この問題は解決しました。

Schema

“servers”: [
{
“url”: “https://api-free.deepl.com”,
“description”: “DeepL API server”
}
],

テキストパラメーターのタイプを配列に

エンドポイントを変更した後、再度プロンプトを実行しますが以下のエラーが出ます。

Schema

{
“response_data”: {
“message”: “Invalid request: Expected ‘text’ parameter to be an array of strings”
},
“status_code”: 400,
“action_id”: “g-ac24426776272d4fc8e8c4a60191af5ec28c5c33”
}

このエラーは、textパラメータが文字列の配列であることが期待されているにも関わらず、その形式で送信していなかったために発生しました

BetterActionsGPTに「このエラーを解消して」と記載することで以下のコードを作成してもらいました。。

Schema

{
“openapi”: “3.0.0”,
“info”: {
“title”: “DeepL Translation API”,
“description”: “DeepL API for integrating translation capabilities into applications.”,
“version”: “1.0.0”
},
“servers”: [
{
“url”: “https://api-free.deepl.com/v2”,
“description”: “DeepL API Free server”
}
],
“paths”: {
“/translate”: {
“post”: {
“operationId”: “translateText”,
“summary”: “Translate text from one language to another”,
“description”: “Translates the provided text array to the specified target language using DeepL’s translation capabilities.”,
“requestBody”: {
“required”: true,
“content”: {
“application/json”: {
“schema”: {
“$ref”: “#/components/schemas/TranslationRequest”
}
}
}
},
“responses”: {
“200”: {
“description”: “Successful translation”,
“content”: {
“application/json”: {
“schema”: {
“$ref”: “#/components/schemas/TranslationResponse”
}
}
}
},
“400”: {
“description”: “Invalid request”
},
“403”: {
“description”: “Forbidden request”
}
},
“security”: [
{
“DeepLAuth”: []
}
]
}
}
},
“components”: {
“schemas”: {
“TranslationRequest”: {
“type”: “object”,
“properties”: {
“text”: {
“type”: “array”,
“items”: {
“type”: “string”
},
“description”: “Array of text strings to be translated”
},
“target_lang”: {
“type”: “string”,
“description”: “Target language code”
}
},
“required”: [“text”, “target_lang”]
},
“TranslationResponse”: {
“type”: “object”,
“properties”: {
“translations”: {
“type”: “array”,
“items”: {
“$ref”: “#/components/schemas/Translation”
}
}
}
},
“Translation”: {
“type”: “object”,
“properties”: {
“detected_source_language”: {
“type”: “string”
},
“text”: {
“type”: “string”
}
}
}
},
“securitySchemes”: {
“DeepLAuth”: {
“type”: “apiKey”,
“in”: “header”,
“name”: “Authorization”,
“description”: “API key needed to authorize requests. Prefix with ‘DeepL-Auth-Key’.”
}
}
}
}

これが最終Schemaになります。

まとめ

今回は以下の4点を修正しました。

  1. components schemasをオブジェクトにする
    最初のSchemaにはschemasが存在していませんでした。その為BetterActionsGPTに指示してcomponents ->にschemasを追加します。
  2. 認証に必要な情報を入れる
    AuthenticationにDeepL APIに必要な情報を入れます。Auth Typeが他と違うので注意が必要です。
  3. エンドポイントURLの変更:
    servers セクションのURLを https://api.deepl.com/v2 から https://api-free.deepl.com/v2 に変更しました。これは、DeepLの無料版APIに対応するための変更です。
  4. リクエストボディのtextパラメータの形式変更:
    paths -> /translate -> post -> requestBody -> content -> application/json -> schema の部分で、text パラメータを文字列から文字列の配列に変更しました。これは、エラーメッセージで指摘された「text パラメータが文字列の配列であることが期待される」問題に対応するための変更です。

これらの変更により、DeepL APIの無料版エンドポイントに対応し、text パラメータを文字列の配列として扱う形式に仕様を合わせました。また、APIキーの使用方法に関する説明も明確にしました。これにより、APIの利用時に発生した問題が解決されました。

この記事が皆さんの役に立てば幸いです。

コメント

タイトルとURLをコピーしました