All Products
Search
Document Center

Alibaba Cloud Model Studio:Synchronous API reference

Last Updated:Aug 12, 2025

Text Embedding is a multilingual, unified text embedding model developed by Tongyi Lab based on large language models (LLMs). It supports multiple mainstream languages and provides efficient embedding services for text data. This model is suitable for natural language processing tasks, such as retrieval-augmented generation (RAG), text classification, and sentiment analysis.

Model overview

Singapore

Model

Vector dimension

Maximum rows

Maximum tokens per row (Note)

Unit price (Million input tokens)

Supported languages

Free quota(Note)

text-embedding-v3

1,024 (default), 768, 512

10

8,192

$0.07

More than 50 mainstream languages, such as Chinese, English, Spanish, French, Portuguese, Indonesian, Japanese, Korean, German, and Russian

1 million tokens

Validity period: 180 days after Model Studio is activated

China (Beijing)

Model

Vector dimension

Maximum rows

Maximum tokens per row

Unit price (Million input tokens)

Supported languages

text-embedding-v4

Qwen3-Embedding series

2,048, 1,536, 1,024 (default), 768, 512, 256, 128, 64

10

8,192

$0.072

More than 100 mainstream languages, such as Chinese, English, Spanish, French, Portuguese, Indonesian, Japanese, Korean, German, and Russian, and multiple programming languages

For information about model rate limits, see Rate limits.

Prerequisites

You must have obtained an API key and set the API key as an environment variable. If you want to call through SDK, you must also install the DashScope SDK.

OpenAI compatible

base_url for SDK:

  • Singapore: https://dashscope-intl.aliyuncs.com/compatible-mode/v1

  • China (Beijing): https://dashscope.aliyuncs.com/compatible-mode/v1

Endpoint for HTTP:

  • Singapore: POST https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings

  • China (Beijing): POST https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings

Request body

String

Python

import os
from openai import OpenAI

client = OpenAI(
    # If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region. To obtain the API key, visit https://bailian.console.alibabacloud.com/?tab=model#/api-key.
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # If you have not configured the environment variable, replace the placeholder with your API key.
    # If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1.
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"  
)

completion = client.embeddings.create(
    # If you use a model in the China (Beijing) region, you must replace the model with text-embedding-v4.
    model="text-embedding-v3",
    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())

Java

import 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) {
        // If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region. To obtain the API key, visit https://bailian.console.alibabacloud.com/?tab=model#/api-key.
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        if (apiKey == null) {
            System.out.println("DASHSCOPE_API_KEY not found in environment variables");
            return;
        }
        // If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings.
        String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
        HttpClient client = HttpClient.newHttpClient();

        Map<String, Object> requestBody = new HashMap<>();
        // If you use a model in the China (Beijing) region, the model is text-embedding-v4.
        requestBody.put("model", "text-embedding-v3");
        requestBody.put("input", "The wind is strong and the sky is high, the apes wail sadly. The islet is clear, the sand is white, and birds fly back. Endless falling leaves descend rustling, the mighty Yangtze River flows on endlessly.");
        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

If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region, set the model to text-embedding-v4, and replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings.
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "text-embedding-v3",
    "input": "The wind is strong and the sky is high, the apes wail sadly. The islet is clear, the sand is white, and birds fly back. Endless falling leaves descend rustling, the mighty Yangtze River flows on endlessly.",  
    "dimension": "1024",  
    "encoding_format": "float"
}'

String list

Python

import os
from openai import OpenAI

client = OpenAI(
    # If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region. To obtain the API key, visit https://bailian.console.alibabacloud.com/?tab=model#/api-key.
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # If you have not configured the environment variable, replace the placeholder with your API key.
    # If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1.
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"  
)

completion = client.embeddings.create(
    model="text-embedding-v3",
    input=['The wind is strong and the sky is high, the apes wail sadly', 'The islet is clear and the sand is white, birds fly back', 'Endless falling leaves descend rustling', 'The mighty Yangtze River flows on endlessly'],
    dimensions=1024,
    encoding_format="float"
)

print(completion.model_dump_json())

Java

import 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) {
        /** Obtain the API key from the environment variable. If the environment variable is not configured, replace the placeholder with your API key.*/
        // If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region. To obtain the API key, visit https://bailian.console.alibabacloud.com/?tab=model#/api-key.
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        if (apiKey == null) {
            System.out.println("DASHSCOPE_API_KEY not found in environment variables");
            return;
        }
        // If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings.
        String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
        HttpClient client = HttpClient.newHttpClient();
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", "text-embedding-v3");
        List<String> inputList = Arrays.asList("The wind is strong and the sky is high, the apes wail sadly", "The islet is clear and the sand is white, birds fly back", "Endless falling leaves descend rustling", "The mighty Yangtze River flows on endlessly");
        requestBody.put("input", inputList);
        requestBody.put("encoding_format", "float");

        try {
            /** Convert the request body to a JSON string.*/
            String requestBodyString = JsonUtils.toJson(requestBody);

            /**Build an HTTP request.*/
            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(baseUrl))
                    .header("Content-Type", "application/json")
                    .header("Authorization", "Bearer " + apiKey)
                    .POST(HttpRequest.BodyPublishers.ofString(requestBodyString))
                    .build();

            /**Send the request and receive the response.*/
            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) {
            /** Catch and print the exception.*/
            System.err.println("Error: " + e.getMessage());
        }
    }
}

curl

If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region, set the model to text-embedding-v4, and replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings.
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "text-embedding-v3",
    "input": [
        "The wind is strong and the sky is high, the apes wail sadly",
        "The islet is clear and the sand is white, birds fly back", 
        "Endless falling leaves descend rustling", 
        "The mighty Yangtze River flows on endlessly"
        ],
    "dimension": 1024,
    "encoding_format": "float"
}'

File

Python

import os
from openai import OpenAI

client = OpenAI(
    # If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region. To obtain the API key, visit https://bailian.console.alibabacloud.com/?tab=model#/api-key.
    api_key=os.getenv("DASHSCOPE_API_KEY"),  # If you have not configured the environment variable, replace the placeholder with your API key.
    # If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1.
    base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1"  
)
# Make sure that you replace 'texts_to_embedding.txt' with your file name or path.
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
    completion = client.embeddings.create(
        model="text-embedding-v3",
        input=f,
        encoding_format="float"
    )
print(completion.model_dump_json())

Java

import 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) {
        /** Obtain the API key from the environment variable. If the environment variable is not configured, replace the placeholder with your API key.*/
        // If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region. To obtain the API key, visit https://bailian.console.alibabacloud.com/?tab=model#/api-key.
        String apiKey = System.getenv("DASHSCOPE_API_KEY");
        if (apiKey == null) {
            System.out.println("DASHSCOPE_API_KEY not found in environment variables");
            return;
        }
        // If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings.
        String baseUrl = "https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings";
        HttpClient client = HttpClient.newHttpClient();

        /** Read the input file.*/
        StringBuilder inputText = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader("<path to the content root of the file>"))) {
            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", "text-embedding-v3");
        requestBody.put("input", inputText.toString().trim());
        requestBody.put("dimensions", 1024);
        requestBody.put("encoding_format", "float");

        try {
            String requestBodyString = JsonUtils.toJson(requestBody);

            /**Build an HTTP request.*/
            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

If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region, set the model to text-embedding-v4, and replace the URL with https://dashscope.aliyuncs.com/compatible-mode/v1/embeddings.
Make sure that you replace 'texts_to_embedding.txt' with your file name or path.
FILE_CONTENT=$(cat texts_to_embedding.txt | jq -Rs .)
curl --location 'https://dashscope-intl.aliyuncs.com/compatible-mode/v1/embeddings' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "text-embedding-v3",
    "input": ['"$FILE_CONTENT"'],
    "dimension": 1024,
    "encoding_format": "float"
}'

model string Required

The model to call. You can select text-embedding-v3, or text-embedding-v4.

input array<string> or string or file Required

The input text. The value can be a string, a list of strings, or a file. Each line in the file represents a piece of content to be embedded.

Text limits:

  • Number of texts:

    • If the input is a string, the string can contain up to 8,192 tokens.

    • If the input is a list of strings, the list can contain up to 10 strings, and each string can contain up to 8,192 tokens.

    • If the input is a file, the file can contain up to 10 lines, and each line can contain up to 8,192 tokens.

dimension integer Optional

The dimension of the output vector. This parameter applies only to text-embedding-v3 and text-embedding-v4. You can set this parameter to one of the following values: 2048 (for text-embedding-v4 only), 1536 (for text-embedding-v4 only), 1024, 768, 512, 256 (for text-embedding-v4 only), 128 (for text-embedding-v4 only), or 64 (for text-embedding-v4 only). The default value is 1024.

encoding_format string Optional

The format of the returned embedding. Currently, only the float format is supported.

Response object

Successful response

{
  "data": [
    {
      "embedding": [
        -0.0695386752486229, 0.030681096017360687, ...
      ],
      "index": 0,
      "object": "embedding"
    },
    ...
    {
      "embedding": [
        -0.06348952651023865, 0.060446035116910934, ...
      ],
      "index": 5,
      "object": "embedding"
    }
  ],
  "model": "text-embedding-v3",
  "object": "list",
  "usage": {
    "prompt_tokens": 184,
    "total_tokens": 184
  },
  "id": "73591b79-d194-9bca-8bb5-xxxxxxxxxxxx"
}

Error response

{
    "error": {
        "message": "Incorrect API key provided. ",
        "type": "invalid_request_error",
        "param": null,
        "code": "invalid_api_key"
    }
}

data array

The output of the task.

Properties

embedding list

The value of the returned object. The value is an array of float data that contains the embedding vector.

index integer

The index of the input text in the input array that corresponds to the result in this structure.

object string

The type of the returned object. The default value is embedding.

model string

The name of the model that was called.

object string

The type of the returned data. The default value is list.

usage object

Properties

prompt_tokens integer

The number of tokens in the user input text.

total_tokens integer

The total number of tokens in the input for this request. Billing is based on the number of tokens parsed from the input string by the model's tokenizer.

id string

The unique ID of the request. You can use this ID to trace request details and troubleshoot issues.

DashScope

base_url for SDK:

  • Singapore: https://dashscope-intl.aliyuncs.com/api/v1

  • China (Beijing): https://dashscope.aliyuncs.com/api/v1

Endpoint for HTTP:

  • Singapore: POST https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding

  • China (Beijing): POST https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding

Request body

String

Python

import dashscope
from http import HTTPStatus

# If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/api/v1.
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

# If you use a model in the China (Beijing) region, the model is text-embedding-v4.
resp = dashscope.TextEmbedding.call(
    model=dashscope.TextEmbedding.Models.text_embedding_v3,
    input='The wind is strong and the sky is high, the apes wail sadly. The islet is clear, the sand is white, and birds fly back. Endless falling leaves descend rustling, the mighty Yangtze River flows on endlessly.',
    dimension=1024,
    output_type="dense&sparse"
)

print(resp) if resp.status_code == HTTPStatus.OK else print(resp)

Java

import 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 {
        // If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/api/v1.
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void basicCall() throws ApiException, NoApiKeyException{
        TextEmbeddingParam param = TextEmbeddingParam
        .builder()
        // If you use a model in the China (Beijing) region, the model is text-embedding-v4.
        .model(TextEmbedding.Models.TEXT_EMBEDDING_V3)
        .texts(Arrays.asList("The wind is strong and the sky is high, the apes wail sadly", "The islet is clear and the sand is white, birds fly back", "Endless falling leaves descend rustling", "The mighty Yangtze River flows on endlessly")).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("The wind is strong and the sky is high, the apes wail sadly", "The islet is clear and the sand is white, birds fly back", "Endless falling leaves descend rustling", "The mighty Yangtze River flows on endlessly")).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

If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region, set the model to text-embedding-v4, and replace the URL with https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding.
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "text-embedding-v3",
    "input": {
        "texts": [
        "The wind is strong and the sky is high, the apes wail sadly. The islet is clear, the sand is white, and birds fly back. Endless falling leaves descend rustling, the mighty Yangtze River flows on endlessly."
        ]
    },
    "parameters": {
    	"dimension": 1024,
    	"output_type": "dense"
    }
}'

String list

Python

import dashscope
from http import HTTPStatus

# If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/api/v1.
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
DASHSCOPE_MAX_BATCH_SIZE = 25

inputs = ['The wind is strong and the sky is high, the apes wail sadly', 'The islet is clear and the sand is white, birds fly back', 'Endless falling leaves descend rustling', 'The mighty Yangtze River flows on endlessly']

result = None
batch_counter = 0
for i in range(0, len(inputs), DASHSCOPE_MAX_BATCH_SIZE):
    batch = inputs[i:i + DASHSCOPE_MAX_BATCH_SIZE]
    resp = dashscope.TextEmbedding.call(
        # If you use a model in the China (Beijing) region, the model is text-embedding-v4.
        model=dashscope.TextEmbedding.Models.text_embedding_v3,
        input=batch,
        dimension=1024
    )
    if resp.status_code == HTTPStatus.OK:
        if result is None:
            result = resp
        else:
            for emb in resp.output['embeddings']:
                emb['text_index'] += batch_counter
                result.output['embeddings'].append(emb)
            result.usage['total_tokens'] += resp.usage['total_tokens']
    else:
        print(resp)
    batch_counter += len(batch)

print(result)

Java

import 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 {
        // If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/api/v1.
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    private static final int DASHSCOPE_MAX_BATCH_SIZE = 25;

    public static void main(String[] args) {
        List<String> inputs = Arrays.asList(
                "The wind is strong and the sky is high, the apes wail sadly",
                "The islet is clear and the sand is white, birds fly back",
                "Endless falling leaves descend rustling",
                "The mighty Yangtze River flows on endlessly"
        );

        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()
                    // If you use a model in the China (Beijing) region, the model is text-embedding-v4.
                    .model(TextEmbedding.Models.TEXT_EMBEDDING_V3)
                    .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

If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region, set the model to text-embedding-v4, and replace the URL with https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding.
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "text-embedding-v3",
    "input": {
        "texts": [
          "The wind is strong and the sky is high, the apes wail sadly",
          "The islet is clear and the sand is white, birds fly back", 
          "Endless falling leaves descend rustling", 
          "The mighty Yangtze River flows on endlessly"
        ]
    },
    "parameters": {
    	  "dimension": 1024,
    	  "output_type": "dense"
    }
}'

File

Python

import dashscope
from http import HTTPStatus
from dashscope import TextEmbedding

# If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/api/v1.
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
# Make sure that you replace 'texts_to_embedding.txt' with your file name or path.
with open('texts_to_embedding.txt', 'r', encoding='utf-8') as f:
    resp = TextEmbedding.call(
        # If you use a model in the China (Beijing) region, the model is text-embedding-v4.
        model=TextEmbedding.Models.text_embedding_v3,
        input=f,
        dimension=1024
    )

    if resp.status_code == HTTPStatus.OK:
        print(resp)
    else:
        print(resp)

Java

import 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 {
        // If you use a model in the China (Beijing) region, you must replace the URL with https://dashscope.aliyuncs.com/api/v1.
        Constants.baseHttpApiUrl="https://dashscope-intl.aliyuncs.com/api/v1";
    }
    public static void main(String[] args) {
        // Make sure that you replace 'tests_to_embedding.txt' with the full path of your file.
        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()
                    // If you use a model in the China (Beijing) region, the model is text-embedding-v4.
                    .model(TextEmbedding.Models.TEXT_EMBEDDING_V3)
                    .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

If you use a model in the China (Beijing) region, you must use the API key of the China (Beijing) region, set the model to text-embedding-v4, and replace the URL with https://dashscope.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding.
Make sure that you replace 'texts_to_embedding.txt' with your file name or path.
FILE_CONTENT=$(cat texts_to_embedding.txt | jq -Rs .)
curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
    "model": "text-embedding-v3",
    "input": {
        "texts": ['"$FILE_CONTENT"']
    },
    "parameters": {
        "dimension": 1024,
        "output_type": "dense"
    }
}'

model string Required

The model to call. You can select text-embedding-v3, or text-embedding-v4.

input string or array<string> Required

The input text. The value can be a string, a list of strings, or a file. Each line in the file represents a piece of content to be embedded.

Text limits:

  • Number of texts:

    • If the input is a string, the string can contain up to 8,192 tokens.

    • If the input is a list of strings, the list can contain up to 10 strings, and each string can contain up to 8,192 tokens.

    • If the input is a file, the file can contain up to 10 lines, and each line can contain up to 8,192 tokens.

text_type string Optional

After text is converted into vectors, it can be used for downstream tasks such as retrieval, clustering, and classification. For asymmetric tasks, such as retrieval, we recommend that you distinguish between query text (query) and document text (document) to achieve better results. For symmetric tasks such as clustering and classification, you do not need to specify this parameter. The default value is document.

dimension integer Optional

The dimension of the output vector. This parameter applies only to text-embedding-v3 and text-embedding-v4. You can set this parameter to one of the following values: 2048 (for text-embedding-v4 only), 1536 (for text-embedding-v4 only), 1024, 768, 512, 256 (for text-embedding-v4 only), 128 (for text-embedding-v4 only), or 64 (for text-embedding-v4 only). The default value is 1024.

output_type string Optional

The discrete vector representation of the output. This parameter applies only to text_embedding_v3 and text_embedding_v4. Valid values are dense, sparse, and dense&sparse. The default value is dense, which means that only continuous vectors are output.

instruct string Optional

Custom task instructions. This parameter takes effect only when you use the text-embedding-v4 model and set text_type to query. We recommend writing the instructions in English. This can improve model performance by approximately 1% to 5%.

Response object

Successful response

{   "status_code": 200, 
    "request_id": "1ba94ac8-e058-99bc-9cc1-7fdb37940a46", 
    "code": "", 
    "message": "",
    "output":{
        "embeddings": [
          {  
             "sparse_embedding":[
               {"index":7149,"value":0.829,"token":"wind"},
               .....
               {"index":111290,"value":0.9004,"token":"sadly"}],
             "embedding": [-0.006929283495992422,-0.005336422007530928, ...],
             "text_index": 0
          }, 
          {
             "sparse_embedding":[
               {"index":246351,"value":1.0483,"token":"islet"},
               .....
               {"index":2490,"value":0.8579,"token":"back"}],
             "embedding": [-0.006929283495992422,-0.005336422007530928, ...],
             "text_index": 1
          },
          {
             "sparse_embedding":[
               {"index":3759,"value":0.7065,"token":"endless"},
               .....
               {"index":1130,"value":0.815,"token":"descend"}],
             "embedding": [-0.006929283495992422,-0.005336422007530928, ...],
             "text_index": 2
          },
          {
             "sparse_embedding":[
               {"index":562,"value":0.6752,"token":"not"},
               .....
               {"index":1589,"value":0.7097,"token":"on"}],
             "embedding": [-0.001945948973298072,-0.005336422007530928, ...],
             "text_index": 3
          }
        ]
    },
    "usage":{
        "total_tokens":27
    },
    "request_id":"xxxxxxxx"
}

Error response

{
    "code":"InvalidApiKey",
    "message":"Invalid API-key provided.",
    "request_id":"xxxxxxxx"
}

status_code string

The status code, which indicates the execution result of the request. For example, a value of 200 indicates that the request was successful.

request_id string

The unique ID of the request. You can use this ID to trace request details and troubleshoot issues.

code string

An error code that is returned if the request fails. If the request is successful, this parameter is empty.

message string

Detailed information that is returned if the request fails. If the request is successful, this parameter is empty.

output object

The output of the task.

Properties

embeddings array

The algorithm output for this request. This parameter is an array of structures. Each structure in the array contains the algorithm output that corresponds to an input text.

Properties

sparse_embedding array

The discrete vector representation of the algorithm output for the corresponding string. Sparse embedding applies only to text_embedding_v3 and text_embedding_v4.

Properties

index integer

The position index of the word or character in the vocabulary.

value float

The weight or importance score of the token. A higher value indicates that the token is more important or relevant in the current text context.

token string

The actual text unit or word in the vocabulary.

embedding array

The continuous vector representation (dense embedding) of the algorithm output for the corresponding string.

text_index integer

The index of the input text in the input array that corresponds to the result in this structure.

usage object

Properties

total_tokens integer

The total number of tokens in the input for this request. Billing is based on the number of tokens parsed from the input string by the model's tokenizer.

request_id string

The unique ID of the request. You can use this ID to trace request details and troubleshoot issues.

Error codes

If a model call failed and an error message is returned, see Error messages for troubleshooting.