Skip to main content
Text Embeddings

DashScope embedding

DashScope embedding API

POST
/api/v1/services/embeddings/text-embedding/text-embedding
import dashscope
from http import HTTPStatus

dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'

resp = dashscope.TextEmbedding.call(
  model=dashscope.TextEmbedding.Models.text_embedding_v4,
  input='Semantic search finds documents by meaning rather than exact keyword matching. Text embeddings map words and sentences into high-dimensional vector spaces. Retrieval-augmented generation combines search results with language models. Document clustering groups similar texts based on their vector representations.',
  dimension=1024,
  output_type="dense&sparse"
)

print(resp) if resp.status_code == HTTPStatus.OK else print(resp)
{
  "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": "sorrow"
          }
        ],
        "embedding": [
          -0.006929283495992422,
          -0.005336422007530928
        ],
        "text_index": 0
      }
    ]
  },
  "usage": {
    "total_tokens": 27
  }
}
Convert text to vectors for semantic search, recommendations, clustering, and classification.

Set the SDK base URL

Python SDK:
import dashscope
dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
Java SDK:
import com.alibaba.dashscope.utils.Constants;
Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";

Supported models

ModelDimensionsMax tokensBatch sizeLanguagesPrice (per 1M tokens)Free quota
text-embedding-v42048, 1536, 1024 (default), 768, 512, 256, 128, 648,19210100+$0.071M tokens (90 days)
text-embedding-v31024 (default), 768, 5128,1921050+$0.071M tokens (90 days)

Input formats

  • Single string: up to 8,192 tokens
  • Array: up to 10 strings, each up to 8,192 tokens
  • Text file: up to 10 lines, each line up to 8,192 tokens

DashScope-specific features

  • text_type: set to query or document for asymmetric tasks like retrieval.
  • output_type: return sparse vectors (dense&sparse) for hybrid search (v3/v4 only).
  • instruct: add a task description to improve accuracy by ~1-5% (v4 only; English recommended).
See Rate limits.

Authorizations

string
header
required

DashScope API key. Obtain from the Qwen Cloud console.

Body

application/json
string
required

The model to call. Supported values: text-embedding-v4, text-embedding-v3.

text-embedding-v4
object
required

The input object containing texts to embed.

object

Optional parameters for the embedding request.

Response

200-application/json
integer
default"200"

The status code. A value of 200 indicates a successful request.

string
default"1ba94ac8-e058-99bc-9cc1-7fdb37940a46"

The unique request ID. Use this ID to trace and troubleshoot requests.

string
default""

If the request fails, this indicates the error code. Empty on success.

string
default""

If the request fails, this indicates the detailed error message. Empty on success.

object

The output data for the task.

object
default"{\"total_tokens\":27}"

Token usage statistics.

DashScope embedding | Qwen Cloud