文档

快速开始

更新时间:
一键部署

前言

通用文本向量,是通义实验室基于LLM底座的多语言文本统一向量模型,面向全球多个主流语种,提供高水准的向量服务,帮助开发者将文本数据快速转换为高质量的向量数据。

模型中文名

模型英文名

向量维度

单次请求文本最大行数

单行最大输入字符长度

支持语种

通用文本向量

text-embedding-v1

1536

25

2048

中文、英语、西班牙语、法语、葡萄牙语、印尼语。

text-embedding-async-v1

1536

100000

2048

中文、英语、西班牙语、法语、葡萄牙语、印尼语。

text-embedding-v2

1536

25

2048

中文、英语、西班牙语、法语、葡萄牙语、印尼语、日语、韩语、德语、俄罗斯语。

text-embedding-async-v2

1536

100000

2048

中文、英语、西班牙语、法语、葡萄牙语、印尼语、日语、韩语、德语、俄罗斯语。

text-embedding-v2是text-embedding-v1模型的升级版本, 更新内容主要包括:

  • 语种扩充: text-embedding-v2模型对比text-embedding-v1模型扩展日语、韩语、德语、俄罗斯语文本向量化能力

  • 效果提升: 预训练模型底座和SFT策略优化提升embedding模型整体效果,公开数据评测结果

MTEB

MTEB(Retrieval task)

CMTEB

CMTEB (Retrieval task)

text-embedding-v1

58.30

45.47

59.84

56.59

text-embedding-v2

60.13

49.49

62.17

62.78

  • 归一化处理: text-embedding-v2对输出向量结果默认归一化处理

使用注意

重要

向量检索需保持离在线使用的向量化模型一致,使用text-embedding-v1构建离线索引数据的场景请勿使用text-embedding-v2作为query请求的向量化模型,反之亦然。

快速调用

调用前准备

  1. 已开通服务并获得API-KEY:获取API-KEY

  2. 已安装最新版SDK:安装SDK

代码示例

API-KEY设置

export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY

同步调用示例

同步调用支持输入单条文本,对单条文本进行处理返回结果。

import dashscope
from http import HTTPStatus


def embed_with_str():
    resp = dashscope.TextEmbedding.call(
        model=dashscope.TextEmbedding.Models.text_embedding_v1,
        input='衣服的质量杠杠的,很漂亮,不枉我等了这么久啊,喜欢,以后还来这里买')
    if resp.status_code == HTTPStatus.OK:
        print(resp)
    else:
        print(resp)


if __name__ == '__main__':
    embed_with_str()
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;

public final class Main {
    public static void basicCall() throws ApiException, NoApiKeyException{
        TextEmbeddingParam param = TextEmbeddingParam
        .builder()
        .model(TextEmbedding.Models.TEXT_EMBEDDING_V1)
        .texts(Arrays.asList("风急天高猿啸哀", "渚清沙白鸟飞回", "无边落木萧萧下", "不尽长江滚滚来")).build();
        TextEmbedding textEmbedding = new TextEmbedding();
        TextEmbeddingResult result = textEmbedding.call(param);
        System.out.println(result);
    }

  public static void main(String[] args){
      try {
        basicCall();
    } catch (ApiException | NoApiKeyException e) {
        System.out.println(e.getMessage());
    }
    System.exit(0);
  }
}

同步调用输出

{
    "status_code": 200,
    "request_id": "617b3670-6f9e-9f47-ad57-997ed8aeba6a",
    "code": "",
    "message": "",
    "output": {
        "embeddings": [
            {
                "embedding": [
                    0.09393704682588577,
                    2.4155092239379883,
                    -1.8923076391220093,
                    .,
                    .,
                    .

                ],
                "text_index": 0
            }
        ]
    },
    "usage": {
        "total_tokens": 23
    }
}

批处理接口调用

如果您有大批量数据需要处理,可以使用批处理接口。

from dashscope import BatchTextEmbedding


def call():
    result = BatchTextEmbedding.call(BatchTextEmbedding.Models.text_embedding_async_v1,
                                     url="https://dashscope.oss-cn-beijing.aliyuncs.com/samples/text/text-embedding-test.txt",
                                     text_type="document")
    print(result)
    
if __name__ == '__main__':
    call()
import com.alibaba.dashscope.embeddings.BatchTextEmbedding;
import com.alibaba.dashscope.embeddings.BatchTextEmbeddingParam;
import com.alibaba.dashscope.embeddings.BatchTextEmbeddingResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.task.AsyncTaskListParam;
import com.alibaba.dashscope.task.AsyncTaskListResult;
import com.alibaba.dashscope.utils.JsonUtils;


public class Main {
    public static void basicCall() throws ApiException, NoApiKeyException {
        BatchTextEmbeddingParam param = BatchTextEmbeddingParam.builder()
                .model(BatchTextEmbedding.Models.TEXT_EMBEDDING_ASYNC_V1)
                .url("https://modelscope.oss-cn-beijing.aliyuncs.com/resource/text_embedding_file.txt")
                .build();
        BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
        BatchTextEmbeddingResult result = textEmbedding.call(param);
        System.out.println(result);
    }

    public static void main(String[] args) {
        try {
            basicCall();
        } catch (ApiException | NoApiKeyException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

python示例输出

{
    "status_code": 200,
    "request_id": "e941b97b-3b82-9de5-9ef0-512815b9c0ca",
    "code": null,
    "message": "",
    "output": {
        "task_id": "0b8ccc25-dec8-4d5f-bb84-cfe2ea580f9d",
        "task_status": "SUCCEEDED",
        "url": "The embedding result file url.",
        "submit_time": "2023-09-07 10:22:52.459",
        "scheduled_time": "2023-09-07 10:22:52.481",
        "end_time": "2023-09-07 10:22:53.419"
    },
    "usage": {
        "total_tokens": 384
    }
}

了解更多

  • 本页导读 (1)
文档反馈