跳到正文

API 参考

文本嵌入

生成文本向量。

仅覆盖已注册端点的文本请求子集。模型可用性及参数支持以当前令牌、控制台和具体模型为准。

POST
/embeddings

用可用的嵌入模型编码文本。

认证

BearerAuth
header认证Bearer <令牌>

请求体

application/json
  1. body
model*string

从控制台或 GET /v1/models 复制可用模型 ID。

input*|array<string>

文本或文本数组。

encoding_format?string

模型支持的向量编码格式。

Value in"float""base64"
dimensions?integer

输出维度,仅用于支持此选项的模型。

响应体

成功;以下结构展示非流式响应。字段与具体模型有关。

application/json
  1. response
object?string
model?string
data?array<>

参数表

认证:Authorization: Bearer API_KEY; Content-Type: application/json

参数类型必填说明
modelstring是从控制台或 GET /v1/models 复制可用模型 ID。
inputstring | array是文本或文本数组。
encoding_formatstring否模型支持的向量编码格式。
dimensionsinteger否输出维度,仅用于支持此选项的模型。

请求示例

先在本机环境设置 API_KEY;POST 请求还需设置 MODEL(从控制台复制)。messages 还需设置模型支持的 MAX_TOKENS。curl 的 POST 示例用 Python 标准库安全构造 JSON。

curl

Bash
export API_BASE_URL='https://xtemapi.com/v1'
: "${API_KEY:?Set API_KEY}"
: "${MODEL:?Set MODEL}"
PAYLOAD="$(python3 - <<'PY'
import json, os
print(json.dumps({"model":os.environ["MODEL"],"input":"Hello"}))
PY
)"
curl --fail-with-body "${API_BASE_URL}/embeddings" \
  -H "Authorization: Bearer ${API_KEY}" \
  -H "Content-Type: application/json" \
  --data "$PAYLOAD"

Python

Python
# Python standard library
import json, os
from urllib.request import Request, urlopen

url = "https://xtemapi.com/v1/embeddings"
headers = {"Authorization":"Bearer " + os.environ["API_KEY"]}
headers["Content-Type"] = "application/json"
payload = {"model":os.environ["MODEL"],"input":"Hello"}
request = Request(url, data=json.dumps(payload).encode(), headers=headers, method="POST")
with urlopen(request) as response:
    print(json.load(response))

Node.js

JavaScript
// Node.js with built-in fetch; save as example.mjs
if (!process.env.API_KEY || !process.env.MODEL) throw new Error("Set the required environment variables");
const response = await fetch("https://xtemapi.com/v1/embeddings", {
  method: "POST",
  headers: {"Authorization":"Bearer " + process.env.API_KEY,"Content-Type":"application/json"},
  body: JSON.stringify({"model":process.env.MODEL,"input":"Hello"}),
});
if (!response.ok) throw new Error(await response.text());
console.log(await response.json());

响应示例(结构示意)

占位文本与注释需替换为实际响应值。这里省略模型相关的用量等字段,不展示虚构的 token 数或向量。

jsonc
{
  "object": "list",
  "model": "<selected embedding model ID>",
  "data": [{
    "object": "embedding",
    "embedding": [/* actual vector values */],
    "index": /* actual input index */
  }]
}

错误处理

按实际 HTTP 状态与错误信息排查,参见报错码。