请求体 modelstring必选 调用的模型,参考模型概览表格中的模型名称进行选择。 inputstring或array<string>必选 输入待处理的文本。可以是字符串(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。
text_type string可选
通过 HTTP 调用时,请将 text_type放入parameters对象中。
文本转换为向量后可以应用于检索、聚类、分类等下游任务,对检索这类非对称任务为了达到更好的检索效果建议区分查询文本(query)和底库文本(document)类型,入库、聚类、分类等对称任务可以不用特殊指定,采用系统默认值document即可。 dimension integer 可选
通过 HTTP 调用时,请将 dimension放入parameters对象中。
指定的向量维度,必须为以下值之一:2560(仅适用于qwen3.7-text-embedding)、2048(仅适用于text-embedding-v4)、1536(仅适用于text-embedding-v4)1024、768、512、256、128 或 64,默认值为1024。 output_type string 可选
通过 HTTP 调用时,请将 output_type放入parameters对象中。
用户指定输出离散向量表示只适用于qwen3.7-text-embedding、``text-embedding-v3与text-embedding-v4模型,取值在dense、sparse、dense&sparse之间,默认取dense,只输出连续向量。稀疏向量输出(sparse和dense&sparse)仅支持通过 DashScope 接口调用。 instruct string 可选 添加自定义任务说明,可用于指导模型理解查询意图。建议使用英文撰写,通常可带来约 1%–5% 的效果提升。 该参数仅在 text_type 取值为 query 时生效,并按输入文本的条数计入 Token 用量。text_type 取值为 document 或未设置 text_type 时,该参数不生效,也不计入 Token 用量。 | 输入字符串Pythonimport os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
resp = client.embeddings.create(
model="qwen3.7-text-embedding",
input='风急天高猿啸哀, 渚清沙白鸟飞回, 无边落木萧萧下, 不尽长江滚滚来',
dimension=1024, # Specify the vector dimension (only supported by qwen3.7-text-embedding, text-embedding-v3, and text-embedding-v4)
output_type="dense&sparse"
)
print(resp)
Javaimport java.util.Arrays;
import java.util.concurrent.Semaphore;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.embeddings.TextEmbeddingParam;
import com.alibaba.dashscope.embeddings.TextEmbeddingResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
public final class Main {
static {
// 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
}
public static void basicCall() throws ApiException, NoApiKeyException{
TextEmbeddingParam param = TextEmbeddingParam
.builder()
.model("qwen3.7-text-embedding")
.texts(Arrays.asList("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来")).build();
TextEmbedding textEmbedding = new TextEmbedding();
TextEmbeddingResult result = textEmbedding.call(param);
System.out.println(result);
}
public static void callWithCallback() throws ApiException, NoApiKeyException, InterruptedException{
TextEmbeddingParam param = TextEmbeddingParam
.builder()
.model(TextEmbedding.Models.TEXT_EMBEDDING_V3)
.texts(Arrays.asList("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来")).build();
TextEmbedding textEmbedding = new TextEmbedding();
Semaphore sem = new Semaphore(0);
textEmbedding.call(param, new ResultCallback<TextEmbeddingResult>() {
@Override
public void onEvent(TextEmbeddingResult message) {
System.out.println(message);
}
@Override
public void onComplete(){
sem.release();
}
@Override
public void onError(Exception err){
System.out.println(err.getMessage());
err.printStackTrace();
sem.release();
}
});
sem.acquire();
}
public static void main(String[] args){
try{
callWithCallback();
}catch(ApiException|NoApiKeyException|InterruptedException e){
e.printStackTrace();
System.out.println(e);
}
try {
basicCall();
} catch (ApiException | NoApiKeyException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}
curl
如果使用华北2(北京)地域的模型,请使用华北2(北京)地域的 API KEY,并将url替换为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding。以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3.7-text-embedding",
"input": {
"texts": [
"风急天高猿啸哀,渚清沙白鸟飞回,无边落木萧萧下,不尽长江滚滚来"
]
},
"parameters": {
"dimension": 1024,
"output_type": "dense"
}
}'
输入字符串列表Pythonimport os
import dashscope
from dashscope.v2 import DashScopeError
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
DASHSCOPE_MAX_BATCH_SIZE = 10
inputs = ['风急天高猿啸哀', '渚清沙白鸟飞回', '无边落木萧萧下', '不尽长江滚滚来']
result = None
batch_counter = 0
try:
for i in range(0, len(inputs), DASHSCOPE_MAX_BATCH_SIZE):
batch = inputs[i:i + DASHSCOPE_MAX_BATCH_SIZE]
resp = client.embeddings.create(
model="qwen3.7-text-embedding",
input=batch,
dimension=1024 # Specify the vector dimension (only supported by qwen3.7-text-embedding, text-embedding-v3, and text-embedding-v4)
)
if result is None:
result = resp
else:
for emb in resp.embeddings:
emb.index += batch_counter
result.embeddings.append(emb)
result.usage.total_tokens += resp.usage.total_tokens
batch_counter += len(batch)
except DashScopeError as e:
print(f"HTTP status code: {e.status_code}")
print(f"Error code: {e.code}")
print(f"Error message: {e.message}")
print("See: https://www.alibabacloud.com/help/model-studio/developer-reference/error-code")
print(result)
Javaimport java.util.Arrays;
import java.util.List;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.embeddings.TextEmbeddingParam;
import com.alibaba.dashscope.embeddings.TextEmbeddingResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
public final class Main {
static {
// 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
}
private static final int DASHSCOPE_MAX_BATCH_SIZE = 10;
public static void main(String[] args) {
List<String> inputs = Arrays.asList(
"风急天高猿啸哀",
"渚清沙白鸟飞回",
"无边落木萧萧下",
"不尽长江滚滚来"
);
TextEmbeddingResult result = null;
int batchCounter = 0;
for (int i = 0; i < inputs.size(); i += DASHSCOPE_MAX_BATCH_SIZE) {
List<String> batch = inputs.subList(i, Math.min(i + DASHSCOPE_MAX_BATCH_SIZE, inputs.size()));
TextEmbeddingParam param = TextEmbeddingParam.builder()
.model("qwen3.7-text-embedding")
.texts(batch)
.build();
TextEmbedding textEmbedding = new TextEmbedding();
try {
TextEmbeddingResult resp = textEmbedding.call(param);
if (resp != null) {
if (result == null) {
result = resp;
} else {
for (var emb : resp.getOutput().getEmbeddings()) {
emb.setTextIndex(emb.getTextIndex() + batchCounter);
result.getOutput().getEmbeddings().add(emb);
}
result.getUsage().setTotalTokens(result.getUsage().getTotalTokens() + resp.getUsage().getTotalTokens());
}
} else {
System.out.println(resp);
}
} catch (ApiException | NoApiKeyException e) {
e.printStackTrace();
}
batchCounter += batch.size();
}
System.out.println(result);
}
}
curl
如果使用华北2(北京)地域的模型,请使用华北2(北京)地域的 API KEY,并将url替换为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding。以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
curl --location 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3.7-text-embedding",
"input": {
"texts": [
"风急天高猿啸哀",
"渚清沙白鸟飞回",
"无边落木萧萧下",
"不尽长江滚滚来"
]
},
"parameters": {
"dimension": 1024,
"output_type": "dense"
}
}'
输入文件Pythonimport os
import dashscope
# The following configuration is for the Singapore (ap-southeast-1) region. Replace {WorkspaceId} with your actual workspace ID. The region value varies by region.
client = dashscope.Client(
# If you have not set an environment variable, replace the following line with your Model Studio API key: api_key="sk-xxx"
# API keys are region-specific. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
api_key=os.getenv("DASHSCOPE_API_KEY"),
workspace="{WorkspaceId}",
region="ap-southeast-1",
)
# Make sure to replace 'texts_to_embedding.txt' with your own file name or path
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
resp = client.embeddings.create(
model="qwen3.7-text-embedding",
input=f,
dimension=1024 # Specify the vector dimension (only supported by qwen3.7-text-embedding, text-embedding-v3, and text-embedding-v4)
)
print(resp)
Javaimport java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import com.alibaba.dashscope.embeddings.TextEmbedding;
import com.alibaba.dashscope.embeddings.TextEmbeddingParam;
import com.alibaba.dashscope.embeddings.TextEmbeddingResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
public final class Main {
static {
// 以下为新加坡地域URL,调用时请将{WorkspaceId}替换为真实的业务空间ID,各地域的URL不同。
Constants.baseHttpApiUrl="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1";
}
public static void main(String[] args) {
// 请确保替换 'tests_to_embedding.txt' 为您文件的完整路径
try (BufferedReader reader = new BufferedReader(new FileReader("tests_to_embedding.txt"))) {
StringBuilder content = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
TextEmbeddingParam param = TextEmbeddingParam.builder()
.model("qwen3.7-text-embedding")
.text(content.toString())
.build();
TextEmbedding textEmbedding = new TextEmbedding();
TextEmbeddingResult result = textEmbedding.call(param);
if (result != null) {
System.out.println(result);
} else {
System.out.println("Failed to get embedding: " + result);
}
} catch (IOException | ApiException | NoApiKeyException e) {
e.printStackTrace();
}
}
}
curl
如果使用华北2(北京)地域的模型,请使用华北2(北京)地域的 API KEY,并将url替换为:https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding。以下为新加坡地域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/api/v1/services/embeddings/text-embedding/text-embedding' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "qwen3.7-text-embedding",
"input": {
"texts": ['"$FILE_CONTENT"']
},
"parameters": {
"dimension": 1024,
"output_type": "dense"
}
}'
|