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" }}'const response = await fetch("https://api.sakuranbo.ai/v1/estimate", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ "model": "sakuranbo/cherryimage-1.0-pro", "input": { "resolution": "2k" } }),});const data = await response.json();console.log(data);import requests
response = requests.post( "https://api.sakuranbo.ai/v1/estimate", json={ "model": "sakuranbo/cherryimage-1.0-pro", "input": { "resolution": "2k", }, },)print(response.json())有两个可选字段有助于预估:
- 文本模型接受
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 数。失败、已取消以及被内容政策拒绝的生成不会计费。例外是在模型开始生成输出(包括推理输出)之后中断的流式对话:已生成的输出会计费。详见对话。
错误
章节标题“错误”400invalid_request:请求体不是有效的 JSON、字段类型有误,或输入值超出范围。error.param会指出字段名;当可接受的值是固定集合时,error.details.allowed会列出这些值;当值为数字时,error.details.minimum和error.details.maximum会给出允许的范围。404model_not_found:目录中不存在该模型 ID。413payload_too_large:请求体超过 256 KiB。
完整列表请参阅错误。
所有价格均以 USD 计价,标价已包含我们的加价。