接口说明
文本向量(Embedding)
shell
POST https://api.scnet.cn/api/llm/v1/embeddings
1
1.功能介绍
文本类数据提供高效的向量化转换服务,适用于RAG、文本分类、情感分析等自然语言处理任务。
2.请求参数
Header 参数
名称 | 类型 | 必填 | 示例值 |
---|---|---|---|
Content-Type | string | 是 | application/json |
Authorization | string | 是 | Bearer |
Body 参数
名称 | 类型 | 必填 | 默认值 | 描述 |
---|---|---|---|---|
model | string | 是 | \ | 调用模型的名称。例如:您可以使用Qwen3-Embedding-8B。 |
input | array<string>或 string | 是 | \ | 输入文本的基本信息,可以是字符串,字符串列表。 文本限制: ●文本数量: ○作为字符串时最长支持 8,192 Token。 ○作为字符串列表时最多支持 5条,每条最长支持 8,192 Token。 |
dimension | int | 否 | 4096 | 用于用户指定输出向量维度,Qwen3-Embedding-8B模型默认值为 4096,不支持修改为其他值。 |
encoding_format | string | 否 | \ | 用于控制返回的Embedding格式,当前支持float和base64格式。 |
3.响应参数
名称 | 类型 | 必填 | 描述 |
---|---|---|---|
data | array | 是 | 任务输出信息。 embedding list 本次调用返回object对象的value,类型是元素为float数据的数组,包含具体Embedding向量。 index int 本结构中的算法结果对应的输入文字在输入数组中的索引值。 object string 本次调用返回的object对象类型,默认为embedding。 |
model | string | 是 | 调用模型的名称。 |
object | string | 是 | 本次调用返回的data类型,默认为list。 |
model | string | 是 | 生成该 completion 的模型名。 |
usage | object | 是 | prompt_tokens integer 用户输入文本转换成Token后的长度。 total_tokens integer 本次请求输入内容的 Token 数目,算法的计量是根据用户输入字符串被模型Tokenizer解析之后对应的Token数目来进行。 |
id | string | 是 | 请求唯一标识。可用于请求明细溯源和问题排查。 |
4.请求示例
1. 请求示例(curl)
shell
curl --location 'https://api.scnet.cn/api/llm/v1/embeddings' \
--header "Authorization: Bearer <API Key>" \
--header 'Content-Type: application/json' \
--data '{
"model": "Qwen3-Embedding-8B",
"input": "风急天高猿啸哀,渚清沙白鸟飞回,无边落木萧萧下,不尽长江滚滚来",
"dimension": "4096",
"encoding_format": "float"
}'
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
2. 请求示例(python)
python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("<API Key>"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
base_url="https://api.scnet.cn/api/llm/v1/embeddings" # SCNet模型API服务的base_url
)
completion = client.embeddings.create(
model="Qwen3-Embedding-8B",
input='衣服的质量杠杠的,很漂亮,不枉我等了这么久啊,喜欢,以后还来这里买',
dimensions=4096, # 指定向量维度(仅 Qwen3-Embedding-8B模型支持该参数)
encoding_format="float"
)
print(completion.model_dump_json())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3. 请求示例(go)
go
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
)
func main() {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
fmt.Println("请设置OPENAI_API_KEY环境变量")
return
}
payload := map[string]interface{}{
"model": "Qwen3-Embedding-8B",
"input": "衣服的质量杠杠的,很漂亮,不枉我等了这么久啊,喜欢,以后还来这里买",
"dimensions": 4096,
"encoding_format": "float",
}
jsonData, err := json.Marshal(payload)
if err != nil {
fmt.Printf("JSON序列化失败: %v\n", err)
return
}
req, err := http.NewRequest("POST", "https://api.scnet.cn/api/llm/v1/embeddings", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Printf("创建请求失败: %v\n", err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("请求发送失败: %v\n", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("请求失败,状态码: %d\n", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("读取响应失败: %v\n", err)
return
}
var result EmbeddingResponse
if err := json.Unmarshal(body, &result); err != nil {
fmt.Printf("解析响应失败: %v\n", err)
return
}
formattedJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Println(string(formattedJSON))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
5.响应示例
json
{
"id": "embd-71d0820e81a3691140fb62967c6cc60a",
"object": "list",
"created": 1750930640,
"model": "Qwen3-Embedding-8B",
"data": [
{
"index": 0,
"object": "embedding",
"embedding": [-0.0016174316, -0.013320923, ..., 0.009742737]
}
],
"usage": {
"prompt_tokens": 37,
"completion_tokens": 0,
"total_tokens": 37,
"prompt_tokens_details": null,
"completion_tokens_details": null,
"prompt_cache_hit_tokens": null,
"prompt_cache_miss_tokens": null
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23