Bagikan melalui


Mensimulasikan kesalahan dari API OpenAI

Sekilas
Tujuan: Menguji Penanganan Kesalahan API OpenAI
Waktu: 10 menit
Plugins:GenericRandomErrorPlugin
Prasyarat:Menyiapkan Proksi Dev

Saat menggunakan API OpenAI di aplikasi, Anda harus menguji bagaimana aplikasi Anda menangani kesalahan API. Dev Proxy memungkinkan Anda mensimulasikan kesalahan pada API OpenAI apa pun menggunakan GenericRandomErrorPlugin.

Ujung

Unduh prasetel ini dengan menjalankannya di prompt perintah devproxy config get openai-throttling.

Di folder penginstalan Proksi Dev, temukan folder config. Di folder config, buat file baru bernama openai-errors.json. Buka file di editor kode.

Buat objek baru di array plugins yang mereferensikan GenericRandomErrorPlugin. Tentukan URL API OpenAI yang dipantau oleh plugin dan tambahkan referensi ke dalam konfigurasi plugin.

File:openai-errors.json

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

Buat objek konfigurasi plugin untuk menyediakan plugin dengan lokasi respons kesalahan.

File:openai-errors.json (konfigurasi lengkap)

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/rc.schema.json",
  "plugins": [    
    {
      "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/v2.1.0/genericrandomerrorplugin.schema.json",
    "errorsFile": "errors-openai.json"
  }
}

Di folder yang sama, buat file errors-openai.json. File ini berisi kemungkinan respons kesalahan yang dapat dikembalikan ketika plugin mengirim respons kesalahan.

File:errors-openai.json

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/genericrandomerrorplugin.errorsfile.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
            }
          }
        }
      ]
    }
  ]
}

Mulai Proksi Dev dengan file konfigurasi:

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

Saat Anda menggunakan aplikasi yang memanggil API OpenAI, Dev Proxy secara acak mengembalikan salah satu respons kesalahan yang Anda tentukan dalam file errors-openai.json.

Pelajari selengkapnya tentang GenericRandomErrorPlugin.

Lihat juga