Decisions
向托管 API 提供文本或图片,并提出判断、选择或评分问题。接口返回每个问题的结果及对应概率。视频可先提取画面帧,再作为图片输入。
Text
POST /v1/decisions
快速调用
客户端安装 requests 后,可直接运行 Python 示例;也可切换为 cURL。
- Python
- cURL
Python
import requests
response = requests.post(
"https://api.valen-team.workers.dev/v1/decisions",
headers={"Origin": "https://valen-team.github.io"},
json={
"model": "valen-2b",
"input": "A cat is sitting on the sofa.",
"questions": [
{
"name": "q0",
"type": "choice",
"instructions": "Which animal is present?",
"choices": [
{
"value": "cat",
"description": "A cat"
},
{
"value": "dog",
"description": "A dog"
}
]
}
],
"temperature": 1
},
timeout=120,
)
response.raise_for_status()
body = response.json()
answer = body["answers"][0]
print(answer["choice"], answer["confidence"])
for entry in answer["probabilities"]:
print(entry["value"], entry["probability"])
print("Model ID:", body.get("model_id"))
Bash
curl --request POST \
https://api.valen-team.workers.dev/v1/decisions \
--header 'Content-Type: application/json' \
--header 'Origin: https://valen-team.github.io' \
--data '{
"model": "valen-2b",
"input": "A cat is sitting on the sofa.",
"questions": [
{
"name": "q0",
"type": "choice",
"instructions": "Which animal is present?",
"choices": [
{
"value": "cat",
"description": "A cat"
},
{
"value": "dog",
"description": "A dog"
}
]
}
],
"temperature": 1
}'
请求头
| 字段 | 说明 |
|---|---|
Content-Type | 必填,application/json |
Origin | 可选;浏览器跨域请求自动携带。 |
Authorization | Key 入口必填,Bearer $VALEN_API_KEY |
请求体
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 托管模型 ID,例如 valen-2b;见 List Models。 |
input | string / messages[] | 是 | 所有问题共享的上下文。 |
questions | object[] | 是 | 问题列表,每项含 name、type 和 instructions。 |
temperature | number | 否 | Valen 校准扩展;有限正数,默认 1.0。 |
问题指令为非空字符串。请求校验由上游 API 执行。
问题类型
- predicate
- choice
- score
判断条件是否成立,返回 0–1 之间的概率。
请求:
JSON
{
"model": "valen-2b",
"temperature": 1.0,
"input": "A cat is on the sofa.",
"questions": [
{
"name": "q0",
"type": "predicate",
"instructions": "The cat is on the sofa."
}
]
}
答案示例:
JSON
{
"answers": [
{
"type": "predicate",
"name": "q0",
"probability": 0.98
}
]
}
从候选项中选择一个值,并返回候选概率分布。
请求:
JSON
{
"model": "valen-2b",
"temperature": 1.0,
"input": "A cat is on the sofa.",
"questions": [
{
"name": "q0",
"type": "choice",
"instructions": "Which animal is present?",
"choices": [
{
"value": "cat",
"description": "A cat"
},
{
"value": "dog",
"description": "A dog"
}
]
}
]
}
答案示例:
JSON
{
"answers": [
{
"type": "choice",
"name": "q0",
"choice": "cat",
"probabilities": [
{
"value": "cat",
"probability": 0.9
},
{
"value": "dog",
"probability": 0.1
}
],
"confidence": 0.8
}
]
}
按从低到高的等级评分。score 是零起始等级索引的概率加权平均值,三档范围为 0–2。
请求:
JSON
{
"model": "valen-2b",
"temperature": 1.0,
"input": "A cat is on the sofa.",
"questions": [
{
"name": "q0",
"type": "score",
"instructions": "How clearly is the animal identified?",
"levels": [
{
"label": "0",
"description": "Unclear"
},
{
"label": "1",
"description": "Partially clear"
},
{
"label": "2",
"description": "Clear"
}
]
}
]
}
答案示例:
JSON
{
"answers": [
{
"type": "score",
"name": "q0",
"score": 1.5,
"probabilities": [
{
"label": "0",
"value": 0,
"probability": 0.1
},
{
"label": "1",
"value": 1,
"probability": 0.3
},
{
"label": "2",
"value": 2,
"probability": 0.6
}
],
"confidence": 0.25
}
]
}
响应
请求成功时返回 HTTP 200,响应内容为 JSON。
| 字段 | 类型 | 说明 |
|---|---|---|
model | string | 是 |
answers | object[] | 按请求顺序返回,包含对应的 name。 |
usage | object | input_tokens 和 output_tokens。 |
confidence 是独立置信度,不等于最大候选概率。响应还可能包含 model_id、revision、internal_usage、timing 和 images。
图片输入
示例读取本地 PNG、JPEG 或 WebP 图片并完成编码和请求。将 image.png 替换为你的文件,MIME 类型会根据文件名自动识别。图片通过 input_image 内容块提供。
- Python
- cURL
Python
import requests
import base64
import mimetypes
from pathlib import Path
image_path = Path("image.png")
mime_type = mimetypes.guess_type(image_path.name)[0]
image_url = f"data:{mime_type};base64," + base64.b64encode(
image_path.read_bytes()
).decode("ascii")
response = requests.post(
"https://api.valen-team.workers.dev/v1/decisions",
headers={"Origin": "https://valen-team.github.io"},
json={
"model": "valen-2b",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Answer using only the visible image."
},
{
"type": "input_image",
"image_url": image_url
}
]
}
],
"questions": [
{
"name": "q0",
"type": "choice",
"instructions": "Which animal is present?",
"choices": [
{
"value": "cat",
"description": "A cat"
},
{
"value": "dog",
"description": "A dog"
}
]
}
],
"temperature": 1
},
timeout=120,
)
response.raise_for_status()
body = response.json()
answer = body["answers"][0]
print(answer["choice"], answer["confidence"])
for entry in answer["probabilities"]:
print(entry["value"], entry["probability"])
print("Model ID:", body.get("model_id"))
Bash
# Run in the folder containing your selected image.
python3 - <<'PY' > request.json
import json, sys
import base64
import mimetypes
from pathlib import Path
image_path = Path("image.png")
mime_type = mimetypes.guess_type(image_path.name)[0]
image_url = f"data:{mime_type};base64," + base64.b64encode(
image_path.read_bytes()
).decode("ascii")
payload = {
"model": "valen-2b",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Answer using only the visible image."
},
{
"type": "input_image",
"image_url": image_url
}
]
}
],
"questions": [
{
"name": "q0",
"type": "choice",
"instructions": "Which animal is present?",
"choices": [
{
"value": "cat",
"description": "A cat"
},
{
"value": "dog",
"description": "A dog"
}
]
}
],
"temperature": 1
}
json.dump(payload, sys.stdout)
PY
curl --request POST \
https://api.valen-team.workers.dev/v1/decisions \
--header 'Content-Type: application/json' \
--header 'Origin: https://valen-team.github.io' \
--data @request.json
错误
| 状态 | 说明 |
|---|---|
400 | 上游拒绝请求。 |
429 | 触发限流,请稍后重试。 |
协议参考:OpenAI Decisions.