共用方式為


測試語言模型符號限制

建置使用語言模型的應用程式時,您應該測試應用程式如何處理權杖型速率限制。 Dev Proxy 可讓您使用 LanguageModelRateLimitingPlugin 模擬語言模型 API 的權杖限制。

模擬語言模型 API 的代幣限制

若要開始,請在組態檔中開啟 LanguageModelRateLimitingPlugin

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

小提示

該插件適用於任何與 OpenAI 兼容的 API,包括本地語言模型,如 Ollama。 請在 urlsToWatch 屬性中包含您要測試的所有語言模型 API 端點。

接下來,使用您想要的權杖限制和時間範圍來設定外掛程式。

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
  "plugins": [
    {
      "name": "LanguageModelRateLimitingPlugin",
      "enabled": true,
      "pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
      "configSection": "languageModelRateLimitingPlugin",
      "urlsToWatch": [
        "https://api.openai.com/*",
        "http://localhost:11434/*"
      ]
    }
  ],
  "languageModelRateLimitingPlugin": {
    "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/languagemodelratelimitingplugin.schema.json",
    "promptTokenLimit": 1000,
    "completionTokenLimit": 500,
    "resetTimeWindowSeconds": 60,
    "whenLimitExceeded": "Throttle"
  }
}

此設定允許在 60 秒的視窗內最多允許 1,000 個提示權杖和 500 個完成權杖。 當超過任一限制時,後續請求會以標準 429 回應進行節流。

使用組態檔啟動 Dev Proxy,並使用您的應用程式提出語言模型要求。 該插件會跟踪實際 API 響應的令牌消耗,並在超過限制時限制請求。

使用自訂錯誤回應進行測試

您也可以在超過權杖限制時設定自訂回應,方法是設定whenLimitExceededCustom並建立自訂回應檔案。

{
  "languageModelRateLimitingPlugin": {
    "promptTokenLimit": 500,
    "completionTokenLimit": 300,
    "resetTimeWindowSeconds": 120,
    "whenLimitExceeded": "Custom",
    "customResponseFile": "token-limit-response.json"
  }
}

使用您想要的錯誤格式建立自訂回應檔案:

{
  "$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/languagemodelratelimitingplugincustomresponse.schema.json",
  "statusCode": 429,
  "headers": [
    {
      "name": "retry-after",
      "value": "@dynamic"
    },
    {
      "name": "content-type",
      "value": "application/json"
    }
  ],
  "body": {
    "error": {
      "message": "Token quota exceeded. Your application has consumed all available tokens for this time period.",
      "type": "quota_exceeded",
      "code": "TOKENS_EXHAUSTED",
      "details": {
        "quota_type": "tokens",
        "retry_after_seconds": "@dynamic"
      }
    }
  }
}

@dynamic retry-after 標頭的值會自動計算距離 token 的限制重設還剩多少秒鐘。

測試不同的場景

場景 1:頻繁測試的代幣限制較低

{
  "languageModelRateLimitingPlugin": {
    "promptTokenLimit": 100,
    "completionTokenLimit": 50,
    "resetTimeWindowSeconds": 30
  }
}

使用具有較短時間範圍的較低限制,能在開發和測試期間快速觸發節流機制。

方案 2:仿真生產環境的限制

{
  "languageModelRateLimitingPlugin": {
    "promptTokenLimit": 10000,
    "completionTokenLimit": 5000,
    "resetTimeWindowSeconds": 3600
  }
}

若要測試實際的權杖取用模式,請設定類似於生產環境的限制。

案例 3:非對稱限制

{
  "languageModelRateLimitingPlugin": {
    "promptTokenLimit": 2000,
    "completionTokenLimit": 100,
    "resetTimeWindowSeconds": 300
  }
}

測試完成權杖限制低於提示限制的案例,模擬注重成本的 API 計劃。

後續步驟

了解更多關於 LanguageModelRateLimitingPlugin