當您在應用程式中使用 OpenAI API 時,您應該測試應用程式如何處理 API 錯誤。 Dev Proxy 可讓您使用 GenericRandomErrorPlugin,模擬任何 OpenAI API 上的錯誤。
提示
在命令提示字元中執行 devproxy config get openai-throttling以下載此預設。
在 [開發 Proxy 安裝] 資料夾中,找出 [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
}
}
}
]
}
]
}
使用組態檔啟動 Dev Proxy:
devproxy --config-file "~appFolder/config/openai-errors.json"
當您使用呼叫 OpenAI API 的應用程式時,Dev Proxy 會隨機傳回您在 errors-openai.json 檔案中定義的其中一個錯誤回應。
深入瞭解 GenericRandomErrorPlugin。