前言
通用文本向量,是达摩院基于LLM底座的多语言文本统一向量模型,面向全球多个主流语种,提供高水准的向量服务,帮助开发者将文本数据快速转换为高质量的向量数据。
模型中文名 | 模型英文名 | 向量维度 | 单次请求文本最大行数 | 单行最大输入字符长度 | 支持语种 |
通用文本向量 | text-embedding-v1 | 1536 | 25 | 2048 | 中文、英语、西班牙语、法语、葡萄牙语、印尼语。 |
text-embedding-async-v1 | 1536 | 100000 | 2048 | 中文、英语、西班牙语、法语、葡萄牙语、印尼语。 |
快速调用
调用前准备
已开通服务并获得API-KEY:开通DashScope并创建API-KEY
已安装最新版SDK:安装DashScope SDK。
代码示例
API-KEY设置
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
同步调用示例
同步调用支持输入单条文本,对单条文本进行处理返回结果。
import dashscope
from http import HTTPStatus
from dashscope import TextEmbedding
def embed_with_str():
resp = TextEmbedding.call(
model=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 BatchTextEmbeddingUsage {
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
}
}
了解更多
通用文本向量同步调用代码示例如上,有关通用文本向量模型API的详细调用文档可前往同步接口API详情页面进行了解。批量数据异步接口代码示例详见批处理接口API详情
文档内容是否对您有帮助?