MCPcopy Create free account
hub / github.com/dashscope/dashscope-sdk-java / BatchTextEmbeddingUsage

Class BatchTextEmbeddingUsage

samples/BatchTextEmbeddingUsage.java:10–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8import com.alibaba.dashscope.utils.JsonUtils;
9
10public class BatchTextEmbeddingUsage {
11 public static void basicCall() throws ApiException, NoApiKeyException {
12 BatchTextEmbeddingParam param = BatchTextEmbeddingParam.builder()
13 .model(BatchTextEmbedding.Models.TEXT_EMBEDDING_ASYNC_V1)
14 .url("https://modelscope.oss-cn-beijing.aliyuncs.com/resource/text_embedding_file.txt")
15 .build();
16 BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
17 BatchTextEmbeddingResult result = textEmbedding.call(param);
18 System.out.println(result);
19 }
20
21 // 创建批处理任务,
22 public static BatchTextEmbeddingResult createTask() throws ApiException, NoApiKeyException {
23 BatchTextEmbeddingParam param = BatchTextEmbeddingParam.builder()
24 .model(BatchTextEmbedding.Models.TEXT_EMBEDDING_ASYNC_V1)
25 .url("https://modelscope.oss-cn-beijing.aliyuncs.com/resource/text_embedding_file.txt")
26 .build();
27 BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
28 return textEmbedding.asyncCall(param);
29
30 }
31
32 // 获取任务状态
33 public static void fetchTaskStatus(BatchTextEmbeddingResult result)
34 throws ApiException, NoApiKeyException {
35 BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
36 result = textEmbedding.fetch(result, null);
37 System.out.println(result);
38 }
39
40 // 等待任务结束,wait内部封装了轮询逻辑,会一直等待任务结束
41 public static void waitTask(BatchTextEmbeddingResult result)
42 throws ApiException, NoApiKeyException {
43 BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
44 result = textEmbedding.wait(result, null);
45 System.out.println(result);
46 }
47
48 // 取消任务,只能取消处于pending状态的任务
49 public static void cancelTask(BatchTextEmbeddingResult result)
50 throws ApiException, NoApiKeyException {
51 BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
52 result = textEmbedding.cancel(result, null);
53 System.out.println(result);
54 }
55
56 // 查询已经提交的任务
57 public static void list() throws ApiException, NoApiKeyException {
58 AsyncTaskListParam param = AsyncTaskListParam.builder().pageNo(1).pageSize(20).build();
59 BatchTextEmbedding textEmbedding = new BatchTextEmbedding();
60 AsyncTaskListResult result = textEmbedding.list(param);
61 System.out.println(JsonUtils.toJson(result));
62 }
63
64 public static void main(String[] args) {
65 try {
66 BatchTextEmbeddingResult task = createTask();
67 fetchTaskStatus(task);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected