跳到正文

AI 运行时

核心 LLM 推理(对话、嵌入、视觉、图像生成、语音 TTS/ASR)与 OpenAI 兼容:用官方 openai SDK 指向 infrai 的 base_url 即可调用。此处列出的是 infrai 原生扩展:批处理、成本/Token 工具、rerank、图像超分、TTS 音色与实时语音会话。

OpenAI 兼容端点

核心 AI 推理(对话、embedding、文生图、语音合成、语音识别、内容审核、模型目录)走标准 OpenAI API 接口——把 OpenAI SDK 指向 infrai 的 base URL 即可。完整示例 → 在找 chat、embeddings、图像或语音?它们由 OpenAI 兼容 API 提供——把 OpenAI SDK 指向 infrai →

端点infrai 能力用途
POST /v1/chat/completionsai.chat对话补全——文本、视觉与工具调用,支持流式。
POST /v1/embeddingsai.embed文本向量,用于检索与 RAG。
POST /v1/images/generationsai.image根据文本提示生成图像。
POST /v1/audio/speechai.tts文本转语音——合成语音音频。
POST /v1/audio/transcriptionsai.asr语音转文本——转写音频文件。
POST /v1/moderationsai.moderation判定内容是否违反使用政策。
GET /v1/modelsai.models.list列出所有已验证、可路由厂商提供的模型。
GET /v1/models/{model}ai.models.get获取单个模型的详情(定价、上下文、可用性)。

1. 概览

基础路径: https://api.infrai.cc/v1/ai
鉴权头: Authorization: Bearer $INFRAI_API_KEY
bash
# Call any /v1/ai capability over raw HTTP — no SDK to install.
# curl:
curl https://api.infrai.cc/v1/ai/... \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json"

2. 方法 (11)

2.1ai.batch.submit

POST /v1/ai/batch/submit

提交一批 AI 请求作为单个异步批处理任务,批量模式按成本原价透传。

参数

名称类型必填说明
requestsBatchRequestItem[]
必填
批处理请求条目数组,每条含 capability 与 body。
≥ 1 item
batch_timeoutstring可选批处理的最长完成时限(如 24h),超时未完成的条目按失败处理。
default: "24h"e.g. 1h
metadataRecord<string, unknown>可选附加到批处理任务上的自定义键值元数据。
idempotency_keystring可选可选去重 key;相同重试将返回同一结果。

返回

BatchSubmitResult { batch_id, state, total_count, estimated_eta?, estimated_cost_usd? }
名称类型说明
batch_idstringbatch job的唯一标识符
pattern: ^batch_[A-Za-z0-9]{20,}$
state"connecting" | "queued" | "in_progress" | "completed" | "failed" | "expired" | "cancelled"当前生命周期状态
total_countinteger跨所有页的总条目数
≥ 1
estimated_etastring | null批处理预计剩余时间
format: date-time
estimated_cost_usdnumber | null批处理预估总费用(美元)
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/batch/submit \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"requests": [{}]}'

2.2ai.batch.status

GET /v1/ai/batch/status/{id}

查询批处理任务的状态、进度与已完成/失败计数。

参数

名称类型必填说明
batch_idstring
必填
目标批处理任务的 ID。

返回

BatchStatus { batch_id, state, progress, total_count, completed_count, failed_count, created_at, total_cost_usd? }
名称类型说明
batch_idstringbatch job的唯一标识符
pattern: ^batch_[A-Za-z0-9]{20,}$
state"connecting" | "queued" | "in_progress" | "completed" | "failed" | "expired" | "cancelled"当前生命周期状态
progressnumber异步操作进度(0.0 到 1.0)
0–1
total_countinteger跨所有页的总条目数
≥ 0
completed_countinteger批处理中已完成条目数
≥ 0
failed_countinteger批处理中失败条目数
≥ 0
created_atstring资源创建时间(ISO 8601)
format: date-time
estimated_completion_atstring | null批处理预计完成时间(ISO 8601)
format: date-time
actual_completion_atstring | null批处理实际完成时间(ISO 8601)
format: date-time
total_cost_usdnumber | null批处理总费用(美元)
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/ai/batch/status/ID \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.3ai.batch.results

GET /v1/ai/batch/results/{id}

分页拉取批处理任务的逐条结果。

参数

名称类型必填说明
batch_idstring
必填
目标批处理任务的 ID。
cursorstring可选分页游标:传入上一页返回的 next_cursor 获取下一页。
limitnumber可选单页返回条数上限。

返回

BatchResultsPage { items, total_count, next_cursor?, download_url? }
名称类型说明
itemsobject[]本页结果条目数组
items[].request_indexinteger在原始 requests 数组中的索引。
≥ 0
items[].okboolean操作是否成功
items[].resultanyok=true 时的能力结果(ChatResult / EmbeddingResult)。
items[].errorobject | null操作失败时的错误消息
items[].error.codestring来自 registry.yaml 的稳定标识符。
items[].error.http_statusintegerWebhook 投递尝试的 HTTP 状态码
400–599
items[].error.messagestring可读提示。
items[].error.docs_urlstring此错误码的文档 URL
format: uri
items[].error.code_detailstring子分类(如钱包上限原因)。
items[].error.retryableboolean错误是否可重试
items[].error.retry_after_msinteger建议重试延迟(毫秒)
≥ 0
items[].error.paramstring验证失败的参数(如适用)。
items[].error.trace_idstring分布式追踪标识符
items[].error.request_idstring服务端分配的追踪请求标识符
total_countinteger跨所有页的总条目数
≥ 0
next_cursorstring | null传入 results() 以获取下一页;null 表示结束。
download_urlstring | null大型结果集的批量下载引用(JSONL);结果内联时为 null。
format: uri

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/ai/batch/results/ID \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.4ai.batch.list

GET /v1/ai/batch/list

分页列出本账户的全部批处理任务。

参数

名称类型必填说明
cursorstring可选分页游标:传入上一页返回的 next_cursor 获取下一页。
limitnumber可选单页返回条数上限。

返回

BatchListResult { batches, total_count, next_cursor? }
名称类型说明
batchesobject[]batch summary objects列表
batches[].batch_idstringbatch job的唯一标识符
pattern: ^batch_[A-Za-z0-9]{20,}$
batches[].state"connecting" | "queued" | "in_progress" | "completed" | "failed" | "expired" | "cancelled"此资源当前的生命周期状态
batches[].progressnumber异步操作进度(0.0 到 1.0)
0–1
batches[].total_countinteger所有页的条目总数
≥ 0
batches[].completed_countintegercompleted items in the batch数量
≥ 0
batches[].failed_countintegerfailed items in the batch数量
≥ 0
batches[].created_atstring此资源创建的 ISO 8601 时间戳
format: date-time
batches[].estimated_completion_atstring | null批处理预计完成时间(ISO 8601)
format: date-time
batches[].actual_completion_atstring | null批处理实际完成的 ISO 8601 时间戳
format: date-time
batches[].total_cost_usdnumber | null批处理总成本(美元)
≥ 0
total_countinteger跨所有页的总条目数
≥ 0
next_cursorstring | null传入 list() 以获取下一页;null 表示结束。

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/ai/batch/list \
  -H "Authorization: Bearer $INFRAI_API_KEY"

2.5ai.batch.cancel

POST /v1/ai/batch/cancel/{id}

取消一个进行中的批处理任务。

参数

名称类型必填说明
batch_idstring
必填
要取消的批处理任务的 ID。
idempotency_keystring可选可选去重 key;相同重试将返回同一结果。

返回

BatchStatus { batch_id, state, ... }
名称类型说明
batch_idstringbatch job的唯一标识符
pattern: ^batch_[A-Za-z0-9]{20,}$
state"connecting" | "queued" | "in_progress" | "completed" | "failed" | "expired" | "cancelled"当前生命周期状态
progressnumber异步操作进度(0.0 到 1.0)
0–1
total_countinteger跨所有页的总条目数
≥ 0
completed_countinteger批处理中已完成条目数
≥ 0
failed_countinteger批处理中失败条目数
≥ 0
created_atstring资源创建时间(ISO 8601)
format: date-time
estimated_completion_atstring | null批处理预计完成时间(ISO 8601)
format: date-time
actual_completion_atstring | null批处理实际完成时间(ISO 8601)
format: date-time
total_cost_usdnumber | null批处理总费用(美元)
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/batch/cancel/ID \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"batch_id": "sample"}'

2.6ai.batch.export

POST /v1/ai/batch/export/{id}

将批处理结果导出为 jsonl 或 csv 文件并返回下载任务。

参数

名称类型必填说明
batch_idstring
必填
要导出的批处理任务的 ID。
pattern: ^batch_[A-Za-z0-9]{20,}$
format"jsonl" | "csv"可选导出文件格式:jsonl 或 csv。
default: "jsonl"
conversation_idstring可选可选,仅导出指定会话的结果。
metadataRecord<string, unknown>可选附加到导出任务上的自定义键值元数据。
idempotency_keystring可选可选去重 key;相同重试将返回同一结果。

返回

ExportJob { export_id, state, format?, download_url?, expires_at?, created_at }
名称类型说明
export_idstringexport job的唯一标识符
pattern: ^exp_[A-Za-z0-9]{20,}$
state"queued" | "in_progress" | "completed" | "failed" | "expired"当前生命周期状态
format"jsonl" | "csv"输出或输入格式(如 json、csv、png)
default: "jsonl"
download_urlstring | nullstate=completed 时填充。
format: uri
expires_atstring | null资源或令牌过期时间(ISO 8601)
format: date-time
created_atstring资源创建时间(ISO 8601)
format: date-time
errorobject | null操作失败时的错误消息
error.codestring来自 registry.yaml 的稳定标识符。
error.http_statusintegerWebhook 投递尝试的 HTTP 状态码
400–599
error.messagestring可读提示。
error.docs_urlstring此错误码的文档 URL
format: uri
error.code_detailstring子分类(如钱包上限原因)。
error.retryableboolean错误是否可重试
error.retry_after_msinteger建议重试延迟(毫秒)
≥ 0
error.paramstring验证失败的参数(如适用)。
error.trace_idstring分布式追踪标识符
error.request_idstring服务端分配的追踪请求标识符

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/batch/export/ID \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'

2.7ai.cost.estimate

POST /v1/ai/cost/estimate

在调用前估算一次 AI 请求的 token 数与分项成本。

参数

名称类型必填说明
messagesstring | ChatMessage[]
必填
用于估算的消息,可为字符串或消息数组。
≥ 1 item
modelstring可选显式模型 id;跳过 task/prefer 路由。
default: "openai/gpt-4o"
expected_output_tokensnumber可选预期的输出 token 数,用于估算输出侧成本。
≥ 0default: 500
cache_strategy"vendor" | "infrai" | "none"可选使用哪一层缓存。
default: "vendor"
batch_modeboolean可选以批处理任务运行以获得折扣价。
default: false
toolsTool[]可选工具/函数调用定义。

返回

CostEstimate { model, vendor, vendor_region?, prompt_tokens, expected_output_tokens, breakdown }
名称类型说明
modelstring此估算的目标已解析模型 ID。
vendorstring将服务此请求的供应商。
vendor_region"china" | "western"服务供应商所在地区(决定加价幅度)。
prompt_tokensintegertokens in the prompt数量
≥ 0
expected_output_tokensinteger预估的输出 token 数
≥ 0
breakdownobject按组件的成本分解
breakdown.currency"USD" | "CNY"ISO 4217 货币代码(如 USD、CNY)
breakdown.input_costnumber输入 token 成本(美元)
≥ 0
breakdown.output_costnumber输出 token 成本(美元)
≥ 0
breakdown.markupnumber供应商成本之上的加价
≥ 0
breakdown.cache_discountnumber缓存命中折扣(美元)
≥ 0
breakdown.batch_discountnumber批处理折扣(美元)
≥ 0
breakdown.failover_adjustmentnumber供应商故障转移导致的成本调整
breakdown.finalnumber所有调整后的最终成本(美元)
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/cost/estimate \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "system"}]}'

2.8ai.cost.compare

POST /v1/ai/cost/compare

对同一请求在多个模型上估算并对比成本。

参数

名称类型必填说明
messagesstring | ChatMessage[]
必填
用于对比估算的消息,可为字符串或消息数组。
≥ 1 item
modelsstring[]可选参与成本对比的模型 ID 列表。
expected_output_tokensnumber可选预期的输出 token 数,用于估算输出侧成本。
≥ 0default: 500
cache_strategy"vendor" | "infrai" | "none"可选使用哪一层缓存。
default: "vendor"
batch_modeboolean可选以批处理任务运行以获得折扣价。
default: false
toolsTool[]可选工具/函数调用定义。

返回

{ estimates: CostEstimate[] }

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/cost/compare \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "system"}]}'

2.9ai.tokens.count

POST /v1/ai/tokens/count

统计一组消息在指定模型下的输入 token 数。

参数

名称类型必填说明
messagesstring | ChatMessage[]
必填
要统计 token 数的消息,可为字符串或消息数组。
≥ 1 item
modelstring可选显式模型 id;跳过 task/prefer 路由。
toolsTool[]可选工具/函数调用定义。

返回

TokenCountResult { prompt_tokens, model }
名称类型说明
prompt_tokensinteger总提示 token,含工具定义开销。
≥ 0
modelstring计数所使用的分词模型。

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/tokens/count \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "system"}]}'

2.10ai.image.upscale

POST /v1/ai/image/upscale

将图片按 2 倍或 4 倍无损放大。

参数

名称类型必填说明
imagestring
必填
待放大的源图片,URL 或 base64。
e.g. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==
factor2 | 4可选放大倍数,2 或 4。
default: 2
idempotency_keystring可选可选去重 key;相同重试将返回同一结果。

返回

ImageUpscaleResult { image, original_size, new_size }
名称类型说明
imageobject用于处理的图片数据或引用
image.b64_jsonstring放大后图片的 Base64 编码字节(内联;无状态透传)。
image.widthinteger-
≥ 1
image.heightinteger-
≥ 1
image.mime_typestring-
e.g. image/png
image.image_idstring | null仅当调用方传入 store:true 时设置;默认无状态转换时为 null。
image.urlstring | null存储后生成的 Infrai CDN URL;默认 null。
format: uri
original_sizestring变换前的原始图片尺寸
e.g. 1024x1024
new_sizestring变换后的新图片尺寸
e.g. 2048x2048

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X POST https://api.infrai.cc/v1/ai/image/upscale \
  -H "Authorization: Bearer $INFRAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="}'

2.11ai.tts.voices

GET /v1/ai/tts/voices

分页列出文本转语音可用的音色列表。

参数

名称类型必填说明
vendorstring可选固定使用某个供应商,而非自动路由。

返回

VoiceListResult { items, count, next_cursor? }
名称类型说明
itemsobject[]本页结果条目数组
items[].voice_idstring-
items[].vendorstring-
items[].namestring | null-
items[].modelstring | null此语音合成时应搭配的供应商模型。
items[].languagestring | nullBCP-47 语言标签。
items[].languagesstring[] | null供应商为此语音公布的支持语言或地区。
items[].genderstring | null-
items[].preview_urlstring | null-
format: uri
items[].is_defaultboolean未指定语音时,synthesize() 是否使用此供应商默认语音。
next_cursorstring | null传入 voices() 以获取下一页;null 表示结束。
countinteger跨所有页的总条目数
≥ 0

示例

一次性前置(每个范例都假定已完成):

bash
# No SDK to install — every call is a plain HTTPS request.
# Get a project key by signing in at https://infrai.cc/login (Google/GitHub gives
# you $2 free credit; email sign-in starts at $0). On 402 INSUFFICIENT_CREDIT, add
# funds at https://infrai.cc/billing (or POST /v1/account/topup and open the
# returned checkout_url).
export INFRAI_API_KEY="ifr_..."
bash
curl -X GET https://api.infrai.cc/v1/ai/tts/voices \
  -H "Authorization: Bearer $INFRAI_API_KEY"
高级:指定 vendor

默认情况下 infrai 会把每次调用智能路由到最佳可用供应商——无需自己挑选 vendor。作为高级逃生口,本能力支持可选的 vendor 入参以锁定某个供应商。本能力当前所有可用 vendor 可通过该能力 id 对应的 discovery 端点实时获取——参见 discovery API

GET /v1/discovery/{capability}

ai.image.upscale

ai.tts.voices

3. 全部能力

本模块全部已路由能力——完整的对外 REST 契约。上方方法是带讲解的入门示例,此表是完整参考。

ai.asrPOST /v1/audio/transcriptions

Speech-to-text — transcribe an audio file (multipart or base64 JSON).

参数 (4)
名称类型必填说明
filestring必填Audio file to transcribe when using multipart/form-data.
format: binary
modelstring可选Model to use. 'auto' (default) lets infrai smart-route across healthy vendors; 'vendor/model' pins a specific vendor and model.
e.g. auto
languagestring可选ISO-639-1 language hint.
response_format"json" | "text" | "srt" | "verbose_json" | "vtt"可选
ai.batch.cancelPOST /v1/ai/batch/cancel/{id}

Cancel an in-progress batch job (idempotent).

参数 (3)
名称类型必填说明
idstring必填Path parameter.
batch_idstring必填Id of the batch job to cancel.
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
ai.batch.exportPOST /v1/ai/batch/export/{id}

Export batch results to a JSONL or CSV file and return a download job.

参数 (6)
名称类型必填说明
idstring必填Path parameter.
batch_idstring | null可选Target batch for ai.batch.export.
pattern: ^batch_[A-Za-z0-9]{20,}$
conversation_idstring | null可选Target conversation for ai.chat.history.export; null = all history.
format"jsonl" | "csv"可选Output or input format (e.g. json, csv, png)
default: "jsonl"
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
metadataobject | null可选Arbitrary key-value metadata attached to this resource
ai.batch.listGET /v1/ai/batch/list

List all batch jobs on the account with pagination.

无请求参数。

ai.batch.resultsGET /v1/ai/batch/results/{id}

Fetch per-item results of a batch job with pagination.

参数 (1)
名称类型必填说明
idstring必填Path parameter.
ai.batch.statusGET /v1/ai/batch/status/{id}

Get a batch job's status, progress, and completed/failed counts.

参数 (1)
名称类型必填说明
idstring必填Path parameter.
ai.batch.submitPOST /v1/ai/batch/submit

Submit a batch of AI requests as one async batch job (billed at cost pass-through with no markup).

参数 (5)
名称类型必填说明
requestsobject[]必填Each item is a ChatRequest / EmbeddingRequest payload.
≥ 1 item
batch_timeoutstring可选Maximum wall-clock the batch may run before expiring.
default: "24h"e.g. 1h
metadataobject | null可选Arbitrary key-value metadata attached to this resource
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
storeboolean可选Opt in to RETAIN the produced asset/record in Infrai's own store (default false = stateless passthrough: process, return inline, keep nothing). When true, Infrai persists the asset, bills a self-hosted storage line item and applies a retention TTL — and only then do the *.list/get/delete read caps see it.
default: false
ai.chatPOST /v1/chat/completions

Chat completions — text, vision and tool calls, with streaming.

参数 (8)
名称类型必填说明
modelstring可选Model to use. 'auto' (default) lets infrai smart-route across healthy vendors; 'vendor/model' pins a specific vendor and model.
e.g. auto
messagesobject[]必填OpenAI chat messages: a list of {role, content}.
temperaturenumber可选
max_tokensinteger可选
top_pnumber可选
streamboolean可选Server-Sent Events streaming when true.
toolsobject[]可选
response_formatobject可选
ai.cost.comparePOST /v1/ai/cost/compare

Estimate and compare the cost of the same request across multiple models.

参数 (7)
名称类型必填说明
messagesobject[]必填Chat messages to price (same shape as ai.chat); cost is estimated from their token count.
≥ 1 item
modelstring | null可选Single model to estimate. Used by ai.cost.estimate. Explicit model_id is allowed here (selection semantics).
default: "openai/gpt-4o"
modelsstring[] | null可选Models to compare. Used by ai.cost.compare; result is sorted ascending by final cost.
expected_output_tokensinteger可选Assumed completion length for the estimate.
≥ 0default: 500
cache_strategy"vendor" | "infrai" | "none"可选Cache strategy hint (none, exact, semantic)
default: "vendor"
batch_modeboolean可选When true, applies the 50% batch discount to the estimate.
default: false
toolsobject[] | null可选Tool/function definitions for the model to call
ai.cost.estimatePOST /v1/ai/cost/estimate

Estimate token counts and itemized cost for an AI request before calling it.

参数 (7)
名称类型必填说明
messagesobject[]必填Chat messages to price (same shape as ai.chat); cost is estimated from their token count.
≥ 1 item
modelstring | null可选Single model to estimate. Used by ai.cost.estimate. Explicit model_id is allowed here (selection semantics).
default: "openai/gpt-4o"
modelsstring[] | null可选Models to compare. Used by ai.cost.compare; result is sorted ascending by final cost.
expected_output_tokensinteger可选Assumed completion length for the estimate.
≥ 0default: 500
cache_strategy"vendor" | "infrai" | "none"可选Cache strategy hint (none, exact, semantic)
default: "vendor"
batch_modeboolean可选When true, applies the 50% batch discount to the estimate.
default: false
toolsobject[] | null可选Tool/function definitions for the model to call
ai.embedPOST /v1/embeddings

Text embeddings for search and RAG.

参数 (4)
名称类型必填说明
modelstring可选Model to use. 'auto' (default) lets infrai smart-route across healthy vendors; 'vendor/model' pins a specific vendor and model.
e.g. auto
inputstring | string[]必填Text string or array of strings to embed.
encoding_format"float" | "base64"可选
dimensionsinteger可选
ai.imagePOST /v1/images/generations

Image generation from a text prompt.

参数 (5)
名称类型必填说明
modelstring可选Model to use. 'auto' (default) lets infrai smart-route across healthy vendors; 'vendor/model' pins a specific vendor and model.
e.g. auto
promptstring必填Text prompt describing the image.
ninteger可选
sizestring可选Image size such as '1024x1024'.
response_format"url" | "b64_json"可选
ai.image.upscalePOST /v1/ai/image/upscale

Upscale an image losslessly by 2x or 4x with an AI model (idempotent). To transform an EXISTING image otherwise use the image.* module.

参数 (8)
名称类型必填说明
imagestring必填URL / file path / base64 of the source image.
e.g. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==
factor2 | 4可选Upscale factor (e.g. 2 or 4)
default: 2
modelstring | null可选Model identifier to use for generation
vendorstring | null可选Vendor that handled or will handle this request
timeout_secondsinteger可选Maximum execution time in seconds
1–600default: 120
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry
metadataobject | null可选Arbitrary key-value metadata attached to this resource
storeboolean可选Opt in to RETAIN the produced asset/record in Infrai's own store (default false = stateless passthrough: process, return inline, keep nothing). When true, Infrai persists the asset, bills a self-hosted storage line item and applies a retention TTL — and only then do the *.list/get/delete read caps see it.
default: false
ai.models.getGET /v1/models/{model}

Retrieve one model's details (pricing, context, availability).

参数 (1)
名称类型必填说明
modelstring必填Path parameter.
ai.models.listGET /v1/models

List every model a verified, routable vendor serves.

无请求参数。

ai.moderationPOST /v1/moderations

Classify whether content violates usage policy.

参数 (2)
名称类型必填说明
modelstring可选Model to use. 'auto' (default) lets infrai smart-route across healthy vendors; 'vendor/model' pins a specific vendor and model.
e.g. auto
inputstring | string[]必填Text string or array of strings to moderate.
ai.rerankPOST /v1/ai/rerank

Reorder a list of candidate documents by relevance to a query, returning each candidate's original index and a vendor relevance score. Real vendor dispatch: Cohere rerank, Qwen/DashScope gte-rerank. Replaces the previous original-order mock.

参数 (5)
名称类型必填说明
querystring必填Search query string for reranking
≥ 1 chars
candidatesstring[]必填Documents to score against the query; the response returns them reordered by relevance.
≥ 1 item
top_kinteger可选Number of top results to return
≥ 1default: 10
modelstring可选Optional model pin (e.g. rerank-english-v3.0, gte-rerank).
vendor"cohere" | "jina" | "qwen"可选Optional vendor pin.
ai.tokens.countPOST /v1/ai/tokens/count

Count input tokens for a set of messages under a given model.

参数 (3)
名称类型必填说明
messagesobject[]必填Chat messages to tokenize (same shape as ai.chat), including tool-definition overhead.
≥ 1 item
modelstring | null可选Tokenizer model; null = default. Determines which encoding is used.
toolsobject[] | null可选Tool definitions; their serialized overhead is included in the count.
ai.ttsPOST /v1/audio/speech

Text-to-speech — synthesize spoken audio (binary body).

参数 (5)
名称类型必填说明
modelstring可选Model to use. 'auto' (default) lets infrai smart-route across healthy vendors; 'vendor/model' pins a specific vendor and model.
e.g. auto
inputstring必填The text to synthesize.
voicestring必填Voice id such as 'alloy'.
response_formatstring可选Audio format such as 'mp3' or 'wav'.
speednumber可选
ai.tts.voicesGET /v1/ai/tts/voices

List available text-to-speech voices with pagination.

无请求参数。

ai.voice.sessionPOST /v1/ai/voice/session

Mint a short-lived token for an AI realtime VOICE/multimodal conversation session (e.g. OpenAI Realtime). NOT pub/sub channel auth — see realtime.token.issue.

参数 (6)
名称类型必填说明
modelstring | null可选Realtime voice model; null lets the server pick a default.
voicestring | null可选Voice id/name to use for the session.
vendorstring | null可选Pin to a specific vendor; disables failover.
instructionsstring | null可选System instructions for the realtime session.
modalitiesstring[] | null可选Enabled modalities (e.g. text, audio).
idempotency_keystring | null可选Client-provided idempotency key; prevents duplicate execution on retry

4. 完整示例

本模块的生产级端到端范例:先一次性配置,再运行业务流程,尽量覆盖本模块的多数 API。

单文件可运行 Python 程序(仅标准库、无 SDK):拷贝后填入 INFRAI_API_KEY 运行,即可按真实业务流逐步体验本模块核心 API——每一步都真实调用并计费,后续步骤复用前一步返回的真实字段。12 行 helper 就是全部集成代码。

python
#!/usr/bin/env python3
"""Infrai · ai-runtime — runnable real-app example (single file, zero deps).

Copy this file, set your key, run it: every step is a REAL call to
api.infrai.cc, billed at the real (tiny) per-call price, printing the
live JSON response. Get a key at https://infrai.cc/login (Google/
GitHub sign-in grants $2 free credit); add funds at
https://infrai.cc/billing. No SDK — the 12-line helper below is the
entire integration."""
import json
import os
from urllib import error, request

KEY = os.environ.get("INFRAI_API_KEY") or "ifr_..."  # <- your key
BASE = "https://api.infrai.cc"


# Same raw HTTPS POST/GET as every per-method example on this page —
# wrapped once for reuse. There is nothing else to it: no SDK.
def infrai(method, path, body=None):
    req = request.Request(
        BASE + path, method=method,
        data=json.dumps(body).encode() if body is not None else None,
        headers={"Authorization": f"Bearer {KEY}",
                 "Content-Type": "application/json"})
    try:
        with request.urlopen(req, timeout=60) as r:
            return json.loads(r.read())
    except error.HTTPError as e:
        return json.loads(e.read())


def show(label, resp):
    print(f"\n== {label} ==")
    print(json.dumps(resp, indent=2, ensure_ascii=False))
    return resp


# 1) chat — POST /v1/chat/completions (OpenAI-compatible)
ask1 = input("Ask the model anything: ").strip()
r1 = show("chat", infrai("POST", "/v1/chat/completions", {"model":"auto","messages":[{"role":"user","content":ask1}]}))

# 2) embeddings — POST /v1/embeddings (OpenAI-compatible)
r2 = show("embeddings", infrai("POST", "/v1/embeddings", {"model":"auto","input":"Infrai is the standard library for AI-built apps."}))

# 3) ai.cost.estimate — POST /v1/ai/cost/estimate · Estimate token counts and itemized cost for an AI request before calling it.
r3 = show("ai.cost.estimate", infrai("POST", "/v1/ai/cost/estimate", {"messages":[{"role":"system"}]}))