模拟 OpenAI API 中的错误
在应用中使用 OpenAI API 时,应测试应用如何处理 API 错误。 开发代理允许使用 GenericRandomErrorPlugin 在任何 OpenAI API 上模拟错误。
提示
通过在命令提示符 devproxy preset get openai-throttling
中运行 来下载此预设。
在“开发代理安装”文件夹中,找到 该 presets
文件夹。 在 presets
文件夹中,创建名为 openai-errors.json
的新文件。 在代码编辑器中打开文件。
在引用 GenericRandomErrorPlugin
的数组中plugins
创建新对象。 定义要为其watch插件的 OpenAI API URL,并添加对插件配置的引用。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/rc.schema.json",
"plugins": [
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
"configSection": "openAIAPI",
"urlsToWatch": [
"https://api.openai.com/*"
]
}
]
}
创建插件配置对象,为插件提供错误响应的位置。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.23.0/rc.schema.json",
"plugins": [
{
"name": "GenericRandomErrorPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-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/v0.23.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/presets/openai-errors.json"
使用调用 OpenAI API 的应用时,开发代理会随机返回文件中 errors-openai.json
定义的错误响应之一。
详细了解 GenericRandomErrorPlugin。