跳转到内容

费用预估 API

POST /v1/estimate 根据模型和输入计算一次生成的价格。它使用与 Playground 相同的计算方式,最终费用由服务器计算。无需 API 密钥,请求体最大为 256 KiB。完整的结构见 OpenAPI 定义。

请求体为 JSON:

  • model(必填):vendor/model 形式的模型 ID,例如 sakuranbo/cherryimage-1.0-pro。
  • input(可选):生成的输入。只会读取影响价格的值,省略的值使用模型默认值。各模型的输入字段列在其模型页面上。
终端窗口
curl -X POST "https://api.sakuranbo.ai/v1/estimate" \
-H "Content-Type: application/json" \
-d '{
"model": "sakuranbo/cherryimage-1.0-pro",
"input": {
"resolution": "2k"
}
}'

有两个可选字段有助于预估:

  • 文本模型接受 prompt_tokens,取值为 0 到模型上下文长度之间的整数。省略时,会根据 messages 近似得出输入大小。不是整数或超出该范围的值会返回 400,不会被悄悄近似或修正。
  • 图像模型接受 n,即图像数量,取值为模型页面上列出范围内的整数,默认为 1。超出范围的值会返回 400。

响应包含 object: "estimate"、model、currency(USD)、以 total_micro_usd 和 total_usd 表示的合计、每个价格项一条的 lines 数组,以及说明预估依据的 assumptions 数组。

{
"object": "estimate",
"model": "sakuranbo/cherryimage-1.0-pro",
"currency": "USD",
"total_micro_usd": 90000,
"total_usd": "0.09",
"lines": [
{
"dimension": "output_image",
"unit": "per_image",
"quantity": 1,
"unit_price_micro_usd": 90000,
"unit_price_usd": "0.09",
"amount_micro_usd": 90000,
"amount_usd": "0.09",
"conditions": {
"resolution": "2k"
}
}
],
"assumptions": [
"Failed, cancelled, expired, and policy-rejected generations are not billed, except in the rare case where it cannot be confirmed that the request reached the model provider. The reserved amount then stays reserved until it is confirmed whether the request is billed."
]
}

对于文本模型,输出部分按 max_tokens(模型最多能输出的 token 数)预估。输入部分在传入 prompt_tokens 时使用该值,未传入时根据 messages 近似得出,因此合计不保证是上限。若要把合计当作上限使用,请通过 prompt_tokens 传入实际的输入 token 数。失败、已取消以及被内容政策拒绝的生成不会计费。例外是在模型开始生成输出(包括推理输出)之后中断的流式对话:已生成的输出会计费。详见对话。

  • 400 invalid_request:请求体不是有效的 JSON、字段类型有误,或输入值超出范围。error.param 会指出字段名;当可接受的值是固定集合时,error.details.allowed 会列出这些值;当值为数字时,error.details.minimum 和 error.details.maximum 会给出允许的范围。
  • 404 model_not_found:目录中不存在该模型 ID。
  • 413 payload_too_large:请求体超过 256 KiB。

完整列表请参阅错误。