OpenAI API からのエラーをシミュレートする

概要
目標: OpenAI API エラー処理をテストする
時間: 10 分
Plugins:GenericRandomErrorPlugin、 RetryAfterPlugin
前提条件:開発プロキシを設定する

アプリで OpenAI API を使用する場合は、アプリが API エラーを処理する方法をテストする必要があります。 開発プロキシを使用すると、 GenericRandomErrorPlugin を使用して、任意の OpenAI API でエラーをシミュレートできます。 また、RetryAfterPlugin では、Dev Proxy は、アプリが API を再度呼び出す前に、Retry-After ヘッダーで指定された時間だけ待機することも確認します。

ヒント

コマンド プロンプト devproxy config get openai-throttlingで実行して、このプリセットをダウンロードします。

プロジェクト フォルダーに、 devproxyrc.jsonという名前の新しいファイルを作成します。 コード エディターでファイルを開きます。

pluginsを参照するGenericRandomErrorPlugin配列に新しいオブジェクトを作成します。 Dev Proxy 用の OpenAI API URL を定義し、プラグイン構成への参照を追加します。

ファイル: devproxyrc.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/rc.schema.json",
  "plugins": [
    {
      "name": "GenericRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "openAIAPI"
    }
  ],
  "urlsToWatch": [
    "https://api.openai.com/*"
  ]
}

RetryAfterPluginを追加し、プラグイン構成オブジェクトを作成して、エラー応答の場所と失敗する要求の割合をGenericRandomErrorPluginに提供します。

ファイル: devproxyrc.json (完全な構成)

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/rc.schema.json",
  "plugins": [
    {
      "name": "RetryAfterPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
    },
    {
      "name": "GenericRandomErrorPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "openAIAPI"
    }
  ],
  "urlsToWatch": [
    "https://api.openai.com/*"
  ],
  "openAIAPI": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/genericrandomerrorplugin.schema.json",
    "errorsFile": "openai-errors.json",
    "rate": 90
  }
}

Caution

構成ファイルのRetryAfterPluginの前にGenericRandomErrorPluginを追加します。 後で追加した場合、RetryAfterPlugin で確認できるようになる前に、GenericRandomErrorPlugin が要求を失敗させます。

同じフォルダーに、 openai-errors.json ファイルを作成します。 このファイルには、要求が失敗したときに Dev Proxy が選択するエラー応答が含まれています。 これらは、OpenAI API が返すエラーと一致します。

Status error.code シミュレーションする内容
429 rate_limit_exceeded アプリは、1 分あたりのトークン (TPM) または 1 分あたりの要求数 (RPM) の制限に達しました。
429 slow_down アプリの要求レートの増加が速すぎます。
429 credit_balance_exhausted 組織にプリペイド クレジットが残っていません。 再試行しても役に立ちません。
503 server_is_overloaded モデルは一時的に過負荷状態です。

これらのエラーの詳細については、OpenAI ドキュメントの エラー コード を参照してください。

ファイル: openai-errors.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v3.3.1/genericrandomerrorplugin.errorsfile.schema.json",
  "errors": [
    {
      "request": {
        "url": "https://api.openai.com/*"
      },
      "responses": [
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            },
            {
              "name": "Retry-After",
              "value": "@dynamic"
            }
          ],
          "body": {
            "error": {
              "message": "Rate limit reached for gpt-4.1 in organization org-K7hT684bLccDbBRnySOoK9f2 on tokens per min (TPM): Limit 30000, Used 30000, Requested 1200. Please try again in 2.4s. Visit https://platform.openai.com/settings/organization/limits to learn more.",
              "type": "tokens",
              "param": null,
              "code": "rate_limit_exceeded"
            }
          }
        },
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            },
            {
              "name": "Retry-After",
              "value": "@dynamic"
            }
          ],
          "body": {
            "error": {
              "message": "Rate limit reached for gpt-4.1 in organization org-K7hT684bLccDbBRnySOoK9f2 on requests per min (RPM): Limit 500, Used 500, Requested 1. Please try again in 120ms. Visit https://platform.openai.com/settings/organization/limits to learn more.",
              "type": "requests",
              "param": null,
              "code": "rate_limit_exceeded"
            }
          }
        },
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            },
            {
              "name": "Retry-After",
              "value": "@dynamic"
            }
          ],
          "body": {
            "error": {
              "message": "Your request rate increased too quickly. Reduce your request rate and increase it gradually.",
              "type": "rate_limit_error",
              "param": null,
              "code": "slow_down"
            }
          }
        },
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            }
          ],
          "body": {
            "error": {
              "message": "Your organization has no prepaid credits remaining. Add credits to continue using the API. For more information on this error, read the docs: https://developers.openai.com/api/docs/guides/error-codes.",
              "type": "insufficient_quota",
              "param": null,
              "code": "credit_balance_exhausted"
            }
          }
        },
        {
          "statusCode": 503,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            }
          ],
          "body": {
            "error": {
              "message": "The requested model is temporarily overloaded. Please try again later.",
              "type": "service_unavailable_error",
              "param": null,
              "code": "server_is_overloaded"
            }
          }
        }
      ]
    }
  ]
}

@dynamic 値は Retry-After ヘッダーを設定し、アプリが待機する必要のある期間を RetryAfterPlugin に伝えます。 待機しても修正されないため、 credit_balance_exhausted 応答には Retry-After ヘッダーがありません。

プロジェクト フォルダーで Dev Proxy を起動:

devproxy

アプリが OpenAI API を呼び出すと、Dev Proxy は openai-errors.json ファイルのランダムなエラーで要求の 90% を失敗させます。 アプリが Retry-After ヘッダーの時刻より前に API を呼び出した場合、RetryAfterPlugin はそれを報告し、要求をスロットルします。

お使いのアプリについて次の内容を確認してください。

  • slow_downまたはRetry-Afterエラーが発生した後、rate_limit_exceeded時間待機します。
  • 再試行するのではなく、 credit_balance_exhausted エラーの後に API の呼び出しを停止します。
  • server_is_overloaded エラーの後に遅延を伴う再試行を行い、再試行回数を使い切ったときに明確なメッセージを表示します。

GenericRandomErrorPlugin の詳細を確認します。

こちらも参照ください