请求体 modelstring必选 调用的模型名称,参考模型概览表格中的模型名称进行选择。 inputarray<string> 或 string 或 file必选 输入待处理的文本。可以是字符串(string)、字符串列表(array)或文件(file)。不同模型版本支持的文本长度和批量大小不同,具体如下:
-
qwen3.7-text-embedding 模型:
- 输入为字符串:最长支持 128,000 Token。
- 输入为字符串列表或文件:最多支持 20 条(行),每条(行)最长支持 128,000 Token。
-
text-embedding-v3 / v4 模型:
- 输入为字符串:最长支持 8,192 Token。
- 输入为字符串列表或文件:最多支持 10 条(行),每条(行)最长支持 8,192 Token。
dimensions integer 可选 指定的向量维度,必须为以下值之一:2560(仅适用于qwen3.7-text-embedding)、2048(仅适用于text-embedding-v4)、1536(仅适用于text-embedding-v4)1024、768、512、256、128 或 64,默认值为1024。 encoding_format string 可选 用于控制返回的Embedding格式,入参支持float和base64,出参的Embedding数据也支持float和base64两种格式。实际返回格式取决于请求路由到的网关: | 输入字符串Pythonimport os
from openai import OpenAI
client = OpenAI(
# 如果使用华北2(北京)地域的模型,需要使用华北2(北京)地域的 API KEY,获取链接:https://modelstudio.console.alibabacloud.com/model/settings/api-key
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
# 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
completion = client.embeddings.create(
model="qwen3.7-text-embedding",
input='The clothes are of good quality and look good, definitely worth the wait. I love them.',
dimensions=1024,
encoding_format="float"
)
print(completion.model_dump_json())
Javaimport java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.dashscope.utils.JsonUtils;
public final class Main {
public static void main(String[] args) {
// 如果使用华北2(北京)地域的模型,需要使用华北2(北京)地域的 API KEY,获取链接:https://modelstudio.console.alibabacloud.com/model/settings/api-key
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
// 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
String baseUrl = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "qwen3.7-text-embedding");
requestBody.put("input", "风急天高猿啸哀,渚清沙白鸟飞回,无边落木萧萧下,不尽长江滚滚来");
requestBody.put("dimensions", 1024);
requestBody.put("encoding_format", "float");
try {
String requestBodyString = JsonUtils.toJson(requestBody);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Response: " + response.body());
} else {
System.out.printf("Failed to retrieve response, status code: %d, response: %s%n", response.statusCode(), response.body());
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
curl
如果使用华北2(北京)地域的模型,请使用华北2(北京)地域的 API KEY,并将url替换为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/embeddings。以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3.7-text-embedding",
"input": "风急天高猿啸哀,渚清沙白鸟飞回,无边落木萧萧下,不尽长江滚滚来",
"dimensions": 1024,
"encoding_format": "float"
}'
输入字符串列表Pythonimport os
from openai import OpenAI
client = OpenAI(
# 如果使用华北2(北京)地域的模型,需要使用华北2(北京)地域的 API KEY,获取链接:https://modelstudio.console.alibabacloud.com/model/settings/api-key
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
# 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
completion = client.embeddings.create(
model="qwen3.7-text-embedding",
input=['风急天高猿啸哀', '渚清沙白鸟飞回', '无边落木萧萧下', '不尽长江滚滚来'],
dimensions=1024,
encoding_format="float"
)
print(completion.model_dump_json())
Javaimport java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Arrays;
import com.alibaba.dashscope.utils.JsonUtils;
public final class Main {
public static void main(String[] args) {
/** 从环境变量中获取 API Key,如果未配置,请直接替换为您的 API Key*/
// 如果使用华北2(北京)地域的模型,需要使用华北2(北京)地域的 API KEY,获取链接:https://modelstudio.console.alibabacloud.com/model/settings/api-key
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
// 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
String baseUrl = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "qwen3.7-text-embedding");
List<String> inputList = Arrays.asList("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来");
requestBody.put("input", inputList);
requestBody.put("encoding_format", "float");
try {
/** 将请求体转换为 JSON 字符串*/
String requestBodyString = JsonUtils.toJson(requestBody);
/**构建 HTTP 请求*/
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
.build();
/**发送请求并接收响应*/
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Response: " + response.body());
} else {
System.out.printf("Failed to retrieve response, status code: %d, response: %s%n", response.statusCode(), response.body());
}
} catch (Exception e) {
/** 捕获并打印异常*/
System.err.println("Error: " + e.getMessage());
}
}
}
curl
如果使用华北2(北京)地域的模型,请使用华北2(北京)地域的 API KEY,并将url替换为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/embeddings。以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3.7-text-embedding",
"input": [
"风急天高猿啸哀",
"渚清沙白鸟飞回",
"无边落木萧萧下",
"不尽长江滚滚来"
],
"dimensions": 1024,
"encoding_format": "float"
}'
输入文件Pythonimport os
from openai import OpenAI
client = OpenAI(
# 如果使用华北2(北京)地域的模型,需要使用华北2(北京)地域的 API KEY,获取链接:https://modelstudio.console.alibabacloud.com/model/settings/api-key
api_key=os.getenv("DASHSCOPE_API_KEY"), # 如果您没有配置环境变量,请在此处用您的API Key进行替换
# 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# 确保将 'texts_to_embedding.txt' 替换为您自己的文件名或路径
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
completion = client.embeddings.create(
model="qwen3.7-text-embedding",
input=f,
encoding_format="float"
)
print(completion.model_dump_json())
Javaimport java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.alibaba.dashscope.utils.JsonUtils;
public class Main {
public static void main(String[] args) {
/** 从环境变量中获取 API Key,如果未配置,请直接替换为您的 API Key*/
// 如果使用华北2(北京)地域的模型,需要使用华北2(北京)地域的 API KEY,获取链接:https://modelstudio.console.alibabacloud.com/model/settings/api-key
String apiKey = System.getenv("DASHSCOPE_API_KEY");
if (apiKey == null) {
System.out.println("DASHSCOPE_API_KEY not found in environment variables");
return;
}
// 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
String baseUrl = "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings";
HttpClient client = HttpClient.newHttpClient();
/** 读取输入文件*/
StringBuilder inputText = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader("<文件所来自的内容根的路径>"))) {
String line;
while ((line = reader.readLine()) != null) {
inputText.append(line).append("\n");
}
} catch (IOException e) {
System.err.println("Error reading input file: " + e.getMessage());
return;
}
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "qwen3.7-text-embedding");
requestBody.put("input", inputText.toString().trim());
requestBody.put("dimensions", 1024);
requestBody.put("encoding_format", "float");
try {
String requestBodyString = JsonUtils.toJson(requestBody);
/**构建 HTTP 请求*/
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(baseUrl))
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + apiKey)
.POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
System.out.println("Response: " + response.body());
} else {
System.out.printf("Failed to retrieve response, status code: %d, response: %s%n", response.statusCode(), response.body());
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
curl
如果使用华北2(北京)地域的模型,请使用华北2(北京)地域的 API KEY,并将url替换为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1/embeddings。以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
确保将 'texts_to_embedding.txt' 替换为您自己的文件名或路径
FILE_CONTENT=$(cat texts_to_embedding.txt | jq -Rs .)
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3.7-text-embedding",
"input": ['"$FILE_CONTENT"'],
"dimensions": 1024,
"encoding_format": "float"
}'
|