本指南說明如何管理現有的專案速率限制,以及建立每月支出警示。速率限制會約束專案在一定時間內的模型用量。支出警示會在每月用量達到門檻時通知團隊,但不會停止 API 請求,也不會強制執行支出上限。
完成主要工作流程後,你將擁有可重複使用的組態,能夠:
- 讀取現有專案可用的速率限制記錄。
- 管理一個模型的請求數與 Token 數限制。
- 在專案每月支出達到門檻時,傳送電子郵件警示。
開始之前
完成 Terraform 供應器設定,並將管理 API 金鑰匯出為環境變數 OPENAI_ADMIN_KEY。你還需要:
- 現有專案的 ID。
- 至少一個用來接收支出警示的電子郵件地址。
評估此工作流程時,請使用測試專案。下一節將說明如何找出文字模型的速率限制記錄。專案可用的速率限制記錄由 OpenAI 建立;Terraform 會更新這些記錄,而不會建立新記錄。
查詢專案速率限制
讀取專案可用的速率限制記錄:
data "openai_project_rate_limits" "current" {
project_id = "proj_123"
}
output "project_rate_limits" {
value = data.openai_project_rate_limits.current.rate_limits
}
資料來源會發出唯讀請求:
project_id用來選擇要檢查的專案。rate_limits包含每個可用模型速率限制所對應的物件,其中列出其id、model及適用的限制值。- 執行
terraform plan或terraform apply後,即可透過輸出查看這些記錄。
請使用 model 與你要控管的模型相符的記錄。複製該記錄的 id;下一個資源會將此值用作 rate_limit_id。請將此 ID 保留為明確指定的輸入值,避免供應器或 API 變更後選到不同的記錄。
管理現有速率限制
管理所選文字模型記錄的請求數與 Token 數限制:
resource "openai_project_rate_limit" "application" {
project_id = "proj_123"
rate_limit_id = "rl-gpt-3.5-turbo"
max_requests_per_1_minute = 500
max_tokens_per_1_minute = 200000
}
各引數的用途如下:
project_id用來識別要變更速率限制的專案。rate_limit_id用來識別現有的模型速率限制記錄,並不是模型 ID。max_requests_per_1_minute限制專案每分鐘可對該模型傳送的請求數。max_tokens_per_1_minute限制專案每分鐘可透過該模型處理的 Token 數。
請只設定適用於所選記錄的欄位。其他記錄類型可能提供每分鐘圖像數、每分鐘音訊百萬位元組數、每日請求數,或每日批次處理輸入 Token 數等限制。設定值不得超過組織與專案可用的上限。
雖然首次 Terraform 執行計畫會將此資源顯示為新增項目,但供應器實際上會更新現有的速率限制記錄,然後將其儲存至 Terraform 狀態。變更已設定的限制值時,會再次傳送更新。
從組態中移除 openai_project_rate_limit,會將該記錄
從 Terraform 狀態中移除,但不會重設或刪除遠端速率限制。
如果之後將由其他工作流程管理該記錄,
請先將遠端值設為所需的值,再移除資源。
設定專案支出警示
建立專案每月支出警示:
resource "openai_project_spend_alert" "monthly" {
project_id = "proj_123"
threshold_amount = 20000
currency = "USD"
interval = "month"
notification_channel_type = "email"
notification_channel_recipients = ["platform-alerts@example.com"]
notification_channel_subject_prefix = "OpenAI project spend"
}
警示定義結合了支出條件與通知管道:
project_id將警示範圍限定為單一專案的支出。threshold_amount是以美分為單位的每月門檻。20000代表 200 美元。currency必須為USD。interval必須為month。notification_channel_type必須為email。notification_channel_recipients必須包含至少一位收件者。notification_channel_subject_prefix是可選的文字,會加到警示電子郵件的主旨中。
Terraform 會建立警示,並儲存產生的 alert_id。變更門檻或通知欄位會更新警示。移除資源則會刪除遠端警示。
支出警示僅提供通知,不會強制限制支出。請針對各個門檻制定事件應變或管理處置方式,並另外使用速率限制來約束請求量。
設定組織支出警示
如果門檻應涵蓋整個組織的支出,請使用組織警示:
resource "openai_organization_spend_alert" "monthly" {
threshold_amount = 100000
currency = "USD"
interval = "month"
notification_channel_type = "email"
notification_channel_recipients = ["platform-alerts@example.com"]
}
此資源使用的門檻單位、間隔、幣別與通知欄位都與專案警示相同。由於計算的是整個組織的支出,因此不接受 project_id。此範例會在組織每月支出達到 1,000 美元後傳送電子郵件。
你可以同時管理專案與組織警示。如果各範圍由不同團隊負責處理,請分別設定不同的門檻與收件者。
執行完整範例
前面的個別範例使用具體數值來說明各個資源。完整組態則以變數取代環境特定值,並整合專案速率限制查詢、一項受管理的速率限制,以及一項專案支出警示。
將下列組態儲存為 main.tf:
terraform {
required_version = ">= 1.0"
required_providers {
openai = {
source = "openai/openai"
version = ">= 1.0.0"
}
}
}
provider "openai" {}
variable "project_id" {
type = string
}
variable "rate_limit_id" {
type = string
description = "Existing rate-limit record for the text model to manage."
}
variable "max_requests_per_minute" {
type = number
}
variable "max_tokens_per_minute" {
type = number
}
variable "project_spend_threshold_cents" {
type = number
description = "Monthly project spend threshold in cents."
validation {
condition = var.project_spend_threshold_cents > 0
error_message = "The project spend threshold must be greater than zero."
}
}
variable "alert_recipients" {
type = list(string)
validation {
condition = length(var.alert_recipients) > 0
error_message = "Provide at least one spend-alert recipient."
}
}
data "openai_project_rate_limits" "current" {
project_id = var.project_id
}
resource "openai_project_rate_limit" "application" {
project_id = var.project_id
rate_limit_id = var.rate_limit_id
max_requests_per_1_minute = var.max_requests_per_minute
max_tokens_per_1_minute = var.max_tokens_per_minute
}
resource "openai_project_spend_alert" "monthly" {
project_id = var.project_id
threshold_amount = var.project_spend_threshold_cents
currency = "USD"
interval = "month"
notification_channel_type = "email"
notification_channel_recipients = var.alert_recipients
notification_channel_subject_prefix = "OpenAI project spend"
}
output "available_rate_limits" {
value = data.openai_project_rate_limits.current.rate_limits
}
output "managed_rate_limit_model" {
value = openai_project_rate_limit.application.model
}
output "project_spend_alert_id" {
value = openai_project_spend_alert.monthly.alert_id
}
建立 terraform.tfvars,填入現有專案 ID、你查到的文字模型速率限制記錄 ID、經核准的限制值、以美分為單位的門檻,以及警示收件者:
project_id = "proj_123"
rate_limit_id = "rl-gpt-3.5-turbo"
max_requests_per_minute = 500
max_tokens_per_minute = 200000
project_spend_threshold_cents = 20000
alert_recipients = ["platform-alerts@example.com"]
選擇的請求數與 Token 數值不得超過專案目前可用的上限。執行計畫中的 available_rate_limits 輸出會顯示目前的記錄與數值,供你比較。
初始化 Terraform,然後審查並套用已儲存的執行計畫:
terraform init
terraform fmt
terraform validate
terraform plan -out=tfplan
terraform show tfplan
terraform apply tfplan
首次執行計畫應包含兩項待新增的資源。Terraform 會將速率限制資源描述為要新增至狀態的項目,但套用時會更新現有的 OpenAI 速率限制記錄。另一個新增項目則會建立專案支出警示。套用完成後,terraform output 會列印可用的速率限制、受管理記錄所關聯的模型,以及警示 ID。
再次執行 terraform plan,確認此組態不會產生進一步的變更。如果顯示狀態漂移,請先確認是否有其他管理員或自動化變更了速率限制或支出警示,再套用下一次更新。