Condividi tramite


Simulare gli errori dalle API OpenAI

Quando usi le API OpenAI nella tua app, devi testare il modo in cui l'app gestisce gli errori api. Dev Proxy consente di simulare gli errori in qualsiasi API OpenAI usando il GenericRandomErrorPlugin.

Suggerimento

Scarica questo preset eseguendo il comando nel prompt dei comandi devproxy config get openai-throttling.

Nella cartella di installazione di Dev Proxy individuare la cartella config. Nella cartella config creare un nuovo file denominato openai-errors.json. Aprire il file in un editor di codice.

Creare un nuovo oggetto nella matrice di plugins che fa riferimento all'GenericRandomErrorPlugin. Definire l'URL dell'API OpenAI per il plug-in da monitorare e aggiungere un riferimento nella configurazione del plug-in.

{
  "$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/*"
      ]
    }
  ]
}

Creare l'oggetto di configurazione del plug-in per fornire al plug-in la posizione delle risposte agli errori.

{
  "$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"
  }
}

Nella stessa cartella creare il file errors-openai.json. Questo file contiene le possibili risposte di errore che possono essere restituite quando il plug-in invia una risposta di errore.

{
  "$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
            }
          }
        }
      ]
    }
  ]
}

Avviare Dev Proxy con il file di configurazione:

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

Quando usi l'app che chiama le API OpenAI, Dev Proxy restituisce in modo casuale una delle risposte di errore definite nel file errors-openai.json.

Altre informazioni su GenericRandomErrorPlugin.