快速開始
使用通用文本向量模型可以免去您在本地部署嵌入模型與向量數(shù)據(jù)庫的步驟,且按token進行計費,幫助您降低項目初期的投入成本。
場景示例
推薦:根據(jù)輸入數(shù)據(jù)推薦相關(guān)條目。例如,根據(jù)用戶購買歷史和瀏覽記錄推薦相關(guān)商品。
聚類:按相關(guān)性對輸入數(shù)據(jù)進行分組。例如,將海量新聞按主題歸類為科技、體育、娛樂等。
搜索:將搜索結(jié)果按照與輸入數(shù)據(jù)的相關(guān)性進行排序。例如,文本向量模型可以根據(jù)用戶搜索詞語返回相關(guān)網(wǎng)頁,多模態(tài)向量模型可以實現(xiàn)以文搜圖。
異常檢測:識別出相關(guān)性較小的異常值。例如,從大量交易記錄中識別出異常交易模式。
支持的模型
模型 | 向量維度 | 單行最大處理token長度 | 單次請求文本最大行數(shù) | 支持語種 |
text-embedding-v1 | 1536 | 2048 | 25 | 中文、英語、西班牙語、法語、葡萄牙語、印尼語 |
text-embedding-async-v1 | 100000 | |||
text-embedding-v2 | 25 | 在v1基礎(chǔ)上:增加日語、韓語、德語、俄羅斯語 | ||
text-embedding-async-v2 | 100000 | |||
text-embedding-v3 | 1024(默認)/768/512 | 8192 | 6 | 在v2基礎(chǔ)上:增加到50+語種(含意大利語、波蘭語、越南語、泰語等) |
模型升級概述
text-embedding-v2
語種擴充:新增對日語、韓語、德語、俄羅斯語的文本向量化能力。
效果提升:優(yōu)化了預訓練模型和SFT策略,提升了整體效果,公開數(shù)據(jù)評測結(jié)果顯示了顯著改進。
text-embedding-v3
語種擴充:支持意大利語、波蘭語、越南語、泰語等,語種數(shù)量增加至50+。
輸入長度擴展:支持最大輸入長度從2048 tokens擴展至8192 tokens。
連續(xù)向量維度自定義:允許用戶選擇512、768或1024維度,默認最大維度降低至1024,以節(jié)省下游任務(wù)的使用成本。
不再區(qū)分Query/Document類型:簡化輸入,text_type參數(shù)不再需要指定文本類型。
Sparse向量支持:支持輸出連續(xù)向量和離散向量,用戶可在接口中指定。
效果提升:進一步優(yōu)化預訓練模型和SFT策略,提升整體效果,公開數(shù)據(jù)評測結(jié)果顯示改善。
v1、v2、v3模型的效果數(shù)據(jù)
模型 | 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-v3 | 63.39 | 55.41 | 68.92 | 73.23 |
text-embedding-v3模型不同維度效果對比
模型 | 模型維度 | MTEB | MTEB(Retrieval task) | CMTEB | CMTEB (Retrieval task) |
text-embedding-v3 | 1024 | 63.39 | 55.41 | 68.92 | 73.23 |
text-embedding-v3 | 768 | 62.43 | 54.74 | 67.90 | 72.29 |
text-embedding-v3 | 512 | 62.11 | 54.30 | 66.81 | 71.88 |
歸一化處理:text-embedding-v2/v3對輸出向量結(jié)果默認歸一化處理。
向量檢索需保持離線與在線使用的向量化模型一致,使用text-embedding-v1構(gòu)建離線索引數(shù)據(jù)的場景請勿使用text-embedding-v2作為query請求的向量化模型,反之亦然。
快速調(diào)用
調(diào)用前準備
您需要已獲取API Key并配置API Key到環(huán)境變量。如果通過SDK調(diào)用,還需要安裝DashScope SDK。
代碼示例
同步調(diào)用示例
同步調(diào)用支持輸入單條文本,對單條文本進行處理并返回結(jié)果。
import dashscope
from http import HTTPStatus
resp = dashscope.TextEmbedding.call(
model=dashscope.TextEmbedding.Models.text_embedding_v3,
input='風急天高猿嘯哀,渚清沙白鳥飛回,無邊落木蕭蕭下,不盡長江滾滾來',
dimension=1024
)
print(resp) if resp.status_code == HTTPStatus.OK else print(resp)
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 callWithCallback() throws ApiException, NoApiKeyException, InterruptedException{
TextEmbeddingParam param = TextEmbeddingParam
.builder()
.model(TextEmbedding.Models.TEXT_EMBEDDING_V1)
.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);
}
}
同步調(diào)用輸出
{ "status_code": 200,
"request_id": "1ba94ac8-e058-99bc-9cc1-7fdb37940a46",
"code": "",
"message": "",
"output":{
"embeddings": [
{
"sparse_embedding":[
{"index":7149,"value":0.829,"token":"風"},
.....
{"index":111290,"value":0.9004,"token":"哀"}],
"embedding": [-0.006929283495992422,-0.005336422007530928, ...],
"text_index": 0
},
{
"sparse_embedding":[
{"index":246351,"value":1.0483,"token":"渚"},
.....
{"index":2490,"value":0.8579,"token":"回"}],
"embedding": [-0.006929283495992422,-0.005336422007530928, ...],
"text_index": 1
},
{
"sparse_embedding":[
{"index":3759,"value":0.7065,"token":"無"},
.....
{"index":1130,"value":0.815,"token":"下"}],
"embedding": [-0.006929283495992422,-0.005336422007530928, ...],
"text_index": 2
},
{
"sparse_embedding":[
{"index":562,"value":0.6752,"token":"不"},
.....
{"index":1589,"value":0.7097,"token":"來"}],
"embedding": [-0.001945948973298072,-0.005336422007530928, ...],
"text_index": 3
}
]
},
"usage":{
"total_tokens":27
},
"request_id":"xxxxxxxx"
}
批處理接口調(diào)用
如果您有大批量數(shù)據(jù)需要處理,可以使用批處理接口。
from dashscope import BatchTextEmbedding
result = BatchTextEmbedding.call(BatchTextEmbedding.Models.text_embedding_async_v1,
url="https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241016/nigwvr/text_embedding_file.txt",
text_type="document")
print(result)
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://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241016/nigwvr/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": "766b7431-22c9-9a64-9ed3-32b5049c2757",
"code": null,
"message": "",
"output": {
"task_id": "35a1b2d1-e7b6-4749-90c1-4169e2953508",
"task_status": "SUCCEEDED",
"url": "xxx",
"submit_time": "2024-06-12 11:16:22.096",
"scheduled_time": "2024-06-12 11:16:22.114",
"end_time": "2024-06-12 11:16:24.235"
},
"usage": {
"total_tokens": 28
}
}
了解更多
通用文本向量同步調(diào)用代碼示例如上,有關(guān)通用文本向量模型API的詳細調(diào)用文檔可前往同步接口API詳情頁面進行了解。批量數(shù)據(jù)異步接口代碼示例詳見批處理接口API詳情。