全部产品

文本向量生成

文本向量生成, 适用于中文。

Java代码示例

  1. DefaultProfile defaultProfile = DefaultProfile.getProfile("cn-hangzhou","your-access-id-key","your-access-id-secret");
  2. IAcsClient client = new DefaultAcsClient(defaultProfile);
  3. Map<String, Object> map = new HashMap<>();
  4. map.put("message", "我是中国人'');
  5. RunPreTrainServiceRequest request = new RunPreTrainServiceRequest();
  6. request.setServiceName("NLP-Text-Embedding");
  7. request.setPredictContent(JSON.toJSONString(map));
  8. RunPreTrainServiceResponse response = client.getAcsResponse(request);
  9. System.out.println(response.getPredictResult());

Python代码示例

  1. # 安装依赖
  2. pip install aliyun-python-sdk-core
  3. pip install aliyun-python-sdk-nlp-automl
  1. # -*- coding: utf8 -*-
  2. import json
  3. from aliyunsdkcore.client import AcsClient
  4. from aliyunsdkcore.acs_exception.exceptions import ClientException
  5. from aliyunsdkcore.acs_exception.exceptions import ServerException
  6. from aliyunsdknlp_automl.request.v20191111 import RunPreTrainServiceRequest
  7. # Initialize AcsClient instance
  8. client = AcsClient(
  9. "<your-access-key-id>",
  10. "<your-access-key-secret>",
  11. "cn-hangzhou"
  12. );
  13. content ={"message": "我是中国人"}
  14. # Initialize a request and set parameters
  15. request = RunPreTrainServiceRequest.RunPreTrainServiceRequest()
  16. request.set_ServiceName('NLP-Text-Embedding')
  17. request.set_PredictContent(json.dumps(content))
  18. # Print response
  19. response = client.do_action_with_exception(request)
  20. resp_obj = json.loads(response)
  21. predict_result = json.loads(resp_obj['PredictResult'])
  22. print(predict_result['embedding'])

PredictContent内容示例

  1. {
  2. "message": "我是中国人",
  3. }

PredictResult内容示例

  1. {
  2. {"embedding":[0.032712288200855255,0.06901254504919052,-0.03633863478899002,-0.06322920322418213,-0.011379375122487545,..]
  3. }

入参说明

参数 说明
message 待预测文本

出参说明

参数 说明
embedding 文本对应的向量表示