模拟来自 OpenAI API 的错误

在应用中使用 OpenAI API 时,应测试应用如何处理 API 错误。 开发人员代理允许使用 GenericRandomErrorPlugin 模拟任何 OpenAI API 上的错误。

小窍门

通过在命令提示符 devproxy config get openai-throttling中运行来下载此预设。

在“开发代理安装”文件夹中,找到该 config 文件夹。 在 config 文件夹中,创建一个名为 openai-errors.json 的新文件。 在代码编辑器中打开该文件。

plugins数组中创建一个新对象,该数组引用GenericRandomErrorPlugin。 定义插件的 OpenAI API URL,以便监视并添加插件配置引用。

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

创建插件配置对象,以便为插件提供错误响应的位置。

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

在同一文件夹中,创建 errors-openai.json 文件。 此文件包含插件发送错误响应时可以返回的可能错误响应。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/genericrandomerrorplugin.schema.json",
  "errors": [
    {
      "request": {
        "url": "https://api.openai.com/*"
      },
      "responses": [
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            }
          ],
          "body": {
            "error": {
              "message": "Rate limit reached for default-text-davinci-003 in organization org-K7hT684bLccDbBRnySOoK9f2 on tokens per min. Limit: 150000.000000 / min. Current: 160000.000000 / min. Contact support@openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://beta.openai.com/account/billing to add a payment method.",
              "type": "tokens",
              "param": null,
              "code": null
            }
          }
        },
        {
          "statusCode": 429,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            }
          ],
          "body": {
            "error": {
              "message": "Rate limit reached for default-text-davinci-003 in organization org-K7hT684bLccDbBRnySOoK9f2 on requests per min. Limit: 60.000000 / min. Current: 70.000000 / min. Contact support@openai.com if you continue to have issues. Please add a payment method to your account to increase your rate limit. Visit https://beta.openai.com/account/billing to add a payment method.",
              "type": "requests",
              "param": null,
              "code": null
            }
          }
        },
        {
          "statusCode": 429,
          "addDynamicRetryAfter": true,
          "headers": [
            {
              "name": "content-type",
              "value": "application/json; charset=utf-8"
            }
          ],
          "body": {
            "error": {
              "message": "The engine is currently overloaded, please try again later.",
              "type": "requests",
              "param": null,
              "code": null
            }
          }
        }
      ]
    }
  ]
}

使用配置文件启动开发代理:

devproxy --config-file "~appFolder/config/openai-errors.json"

使用调用 OpenAI API 的应用时,开发代理会随机返回文件中定义的 errors-openai.json 错误响应之一。

详细了解 GenericRandomErrorPlugin。