创建嵌入
curl --request POST \
--url https://api.example.com/v1/embeddings \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"input": [
"<string>"
],
"encoding_format": "<string>",
"dimensions": 123
}
'import requests
url = "https://api.example.com/v1/embeddings"
payload = {
"model": "<string>",
"input": ["<string>"],
"encoding_format": "<string>",
"dimensions": 123
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
input: ['<string>'],
encoding_format: '<string>',
dimensions: 123
})
};
fetch('https://api.example.com/v1/embeddings', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/v1/embeddings",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'input' => [
'<string>'
],
'encoding_format' => '<string>',
'dimensions' => 123
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/v1/embeddings"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"input\": [\n \"<string>\"\n ],\n \"encoding_format\": \"<string>\",\n \"dimensions\": 123\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/v1/embeddings")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"input\": [\n \"<string>\"\n ],\n \"encoding_format\": \"<string>\",\n \"dimensions\": 123\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/v1/embeddings")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"input\": [\n \"<string>\"\n ],\n \"encoding_format\": \"<string>\",\n \"dimensions\": 123\n}"
response = http.request(request)
puts response.read_body嵌入 & 重排序
创建嵌入
将文本转换为向量表示
POST
/
v1
/
embeddings
创建嵌入
curl --request POST \
--url https://api.example.com/v1/embeddings \
--header 'Content-Type: application/json' \
--data '
{
"model": "<string>",
"input": [
"<string>"
],
"encoding_format": "<string>",
"dimensions": 123
}
'import requests
url = "https://api.example.com/v1/embeddings"
payload = {
"model": "<string>",
"input": ["<string>"],
"encoding_format": "<string>",
"dimensions": 123
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
model: '<string>',
input: ['<string>'],
encoding_format: '<string>',
dimensions: 123
})
};
fetch('https://api.example.com/v1/embeddings', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.example.com/v1/embeddings",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => '<string>',
'input' => [
'<string>'
],
'encoding_format' => '<string>',
'dimensions' => 123
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.example.com/v1/embeddings"
payload := strings.NewReader("{\n \"model\": \"<string>\",\n \"input\": [\n \"<string>\"\n ],\n \"encoding_format\": \"<string>\",\n \"dimensions\": 123\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.example.com/v1/embeddings")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"<string>\",\n \"input\": [\n \"<string>\"\n ],\n \"encoding_format\": \"<string>\",\n \"dimensions\": 123\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.example.com/v1/embeddings")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"<string>\",\n \"input\": [\n \"<string>\"\n ],\n \"encoding_format\": \"<string>\",\n \"dimensions\": 123\n}"
response = http.request(request)
puts response.read_body更新日期:2026-06-06
接口说明
将输入文本转换为高维向量,用于语义搜索、聚类、推荐等场景。完全兼容 OpenAI Embeddings API 格式。支持模型
| 模型 | 维度 | 说明 |
|---|---|---|
text-embedding-3-large | 3072 | 高精度,推荐用于生产环境 |
text-embedding-3-small | 1536 | 性价比高 |
text-embedding-ada-002 | 1536 | 经典模型 |
请求参数
嵌入模型名称,如
text-embedding-3-large要嵌入的文本,支持单个字符串或字符串数组
返回格式:
float 或 base64输出向量维度(仅
text-embedding-3-* 支持)响应格式
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [0.0023064255, -0.009327292, ...]
}
],
"model": "text-embedding-3-large",
"usage": {
"prompt_tokens": 8,
"total_tokens": 8
}
}
代码示例
from openai import OpenAI
client = OpenAI(
api_key="sk-xxx",
base_url="https://api.crazyrouter.com/v1"
)
response = client.embeddings.create(
model="text-embedding-3-large",
input="Crazyrouter 是一个 AI 模型网关"
)
embedding = response.data[0].embedding
print(f"向量维度: {len(embedding)}")
print(f"前5个值: {embedding[:5]}")
response = client.embeddings.create(
model="text-embedding-3-large",
input=[
"第一段文本",
"第二段文本",
"第三段文本"
]
)
for item in response.data:
print(f"索引 {item.index}: 维度 {len(item.embedding)}")
curl -X POST https://api.crazyrouter.com/v1/embeddings \
-H "Authorization: Bearer sk-xxx" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-large",
"input": "Crazyrouter 是一个 AI 模型网关"
}'
批量请求时,单次最多支持 2048 条文本。建议每条文本不超过 8191 tokens。
⌘I