Unify API
保留 embeddings 代码,只改两行。
用 OpenAI embeddings 格式把文本转成向量,与聊天调用共用同一个 key。
POST /api/v1/embeddings
import osfrom openai import OpenAIclient = OpenAI(removed: api_key=os.environ["OPENAI_API_KEY"],added: base_url="https://anyrouter.dev/api/v1",added: api_key=os.environ["ANYROUTER_API_KEY"],)emb = client.embeddings.create(removed: model="text-embedding-3-small",added: model="nvidia/nemotron-3.5-embed-1b", input=["Hello", "world"],)print(len(emb.data[0].embedding))nvidia/nemotron-3.5-embed-1b由平台上游免费提供,任何 key 都能运行。
返回了什么
上面的 cURL 标签页,于 2026-10-31 在生产环境用 Go 套餐 key 运行。只保留显示的字段,过长的值用 "…" 截断。
captured from prod
HTTP 200
x-anyrouter-model: nvidia/nemotron-3-embed-1b
x-anyrouter-provider-chain: kilo
x-anyrouter-credential-source: platform
x-request-id: req_cf24697f3ec441879f0adb82
{
"object": "list",
"model": "nvidia/nemotron-3-embed-1b:free",
"data": [
{
"index": 0,
"embedding": [
0.0022859701,
-0.080008954,
-0.013601524,
"…"
]
},
{
"index": 1,
"embedding": [
0.0038007896,
-0.077580824,
0.024034403,
"…"
]
}
],
"usage": {
"prompt_tokens": 6,
"total_tokens": 6,
"cost": 0
}
}- x-anyrouter-model
- 实际回答的模型。anyrouter/free 等预设会在这里写出所选模型;指定模型 id 时则原样返回你的 id。
- x-anyrouter-provider-chain
- 按顺序尝试过的上游。这里只有一跳;某一跳失败时由下一跳回答,header 变为 a->b。
- x-anyrouter-credential-source
- 由谁的上游 key 付费:platform(平台)、own_byok(你自己的)或 pool(捐赠的 key)。
- x-request-id
- 联系支持时提供它,或在日志中查找该请求。
- usage.cost
- 本次请求的费用(美元),写在 body 里。免费模型为 0。
已支持
- 输入可为字符串或数组
- encoding_format 支持 float 或 base64
Decisions 模型
不是聊天。发送一个情境和带固定选项的问题,返回每个选项的概率。Decisions 在 Go 及以上套餐免费,Free 套餐可试用 5 次请求。
POST /api/v1/decisions
curl https://anyrouter.dev/api/v1/decisions \
-H "Authorization: Bearer $ANYROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "hal-x/thx-01", "state": "My card was charged twice",
"questions": {"team": {"type": "choice", "criteria": {"billing": "Payments", "tech": "API problems"}}}}'
# {"answers": {"team": {"type": "choice", "choice": "billing", "probabilities": {"billing": 0.97, "tech": 0.03}}}, "model": "hal-x/thx-01", "status": "completed"}