For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主导航

使用 Terraform 管理速率限制与支出

使项目速率限制与配置保持一致,并配置支出警报。

本指南介绍如何管理现有项目速率限制并创建月度支出警报。速率限制用于约束项目在一定时间内的模型用量。支出警报会在月度用量达到阈值时通知您的团队,但不会阻止 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 为每个可用的模型速率限制包含一个对象,其中包括该记录的 idmodel 和适用的限制值。
  • 执行 terraform planterraform 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,确认此配置不会产生进一步变更。如果显示存在漂移,请先确定是否有其他管理员或自动化更改了速率限制或支出警报,再应用下一次更新。