|
| 1 | +// Copyright (c) Alibaba, Inc. and its affiliates. |
| 2 | + |
| 3 | +import com.alibaba.dashscope.aigc.generation.Generation; |
| 4 | +import com.alibaba.dashscope.aigc.generation.GenerationParam; |
| 5 | +import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| 6 | +import com.alibaba.dashscope.aigc.generation.TranslationOptions; |
| 7 | +import com.alibaba.dashscope.common.Message; |
| 8 | +import com.alibaba.dashscope.common.Role; |
| 9 | +import com.alibaba.dashscope.exception.ApiException; |
| 10 | +import com.alibaba.dashscope.exception.InputRequiredException; |
| 11 | +import com.alibaba.dashscope.exception.NoApiKeyException; |
| 12 | +import com.alibaba.dashscope.utils.JsonUtils; |
| 13 | +import io.reactivex.Flowable; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | + |
| 20 | +public class GenerationCallTranslation { |
| 21 | + static final String MODE_NAME = "qwen-mt-turbo"; |
| 22 | + |
| 23 | + public static void baseCall() |
| 24 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 25 | + Generation gen = new Generation(); |
| 26 | + List<Message> msgManager = new ArrayList<>(); |
| 27 | + Message userMsg = Message.builder() |
| 28 | + .role(Role.USER.getValue()) |
| 29 | + .content("很久很久以前,有一只小猫,猫的名字叫小花。") |
| 30 | + .build(); |
| 31 | + msgManager.add(userMsg); |
| 32 | + GenerationParam param = |
| 33 | + GenerationParam.builder() |
| 34 | + .model(MODE_NAME) |
| 35 | + .messages(msgManager) |
| 36 | + .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| 37 | + .translationOptions( |
| 38 | + TranslationOptions.builder() |
| 39 | + .sourceLang("Chinese") |
| 40 | + .targetLang("English") |
| 41 | + .build()) |
| 42 | + .build(); |
| 43 | + GenerationResult result = gen.call(param); |
| 44 | + System.out.println(result); |
| 45 | + } |
| 46 | + |
| 47 | + public static void streamCallTerms() |
| 48 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 49 | + Generation gen = new Generation(); |
| 50 | + List<Message> msgManager = new ArrayList<>(); |
| 51 | + Message userMsg = Message.builder() |
| 52 | + .role(Role.USER.getValue()) |
| 53 | + .content("而这套生物传感器运用了石墨烯这种新型材料,它的目标物是化学元素,敏锐的“嗅觉”让它能更深度、准确地体现身体健康状况。") |
| 54 | + .build(); |
| 55 | + msgManager.add(userMsg); |
| 56 | + |
| 57 | + TranslationOptions translationOptions = TranslationOptions.builder() |
| 58 | + .sourceLang("Chinese") |
| 59 | + .targetLang("English") |
| 60 | + .terms(Arrays.asList( |
| 61 | + TranslationOptions.Term.builder() |
| 62 | + .source("生物传感器") |
| 63 | + .target("shengwu sensor") |
| 64 | + .build(), |
| 65 | + TranslationOptions.Term.builder() |
| 66 | + .source("石墨烯") |
| 67 | + .target("graphene") |
| 68 | + .build(), |
| 69 | + TranslationOptions.Term.builder() |
| 70 | + .source("化学元素") |
| 71 | + .target("huaxue elements") |
| 72 | + .build(), |
| 73 | + TranslationOptions.Term.builder() |
| 74 | + .source("身体健康状况") |
| 75 | + .target("jiankang status of the body") |
| 76 | + .build() |
| 77 | + )) |
| 78 | + .build(); |
| 79 | + |
| 80 | + GenerationParam param = |
| 81 | + GenerationParam.builder() |
| 82 | + .model(MODE_NAME) |
| 83 | + .messages(msgManager) |
| 84 | + .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| 85 | + .incrementalOutput(true) |
| 86 | + .translationOptions(translationOptions) |
| 87 | + .build(); |
| 88 | + Flowable<GenerationResult> result = gen.streamCall(param); |
| 89 | + result.blockingSubscribe( data -> { |
| 90 | + System.out.println(JsonUtils.toJson(data)); |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + public static void streamCallTmList() |
| 95 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 96 | + Generation gen = new Generation(); |
| 97 | + List<Message> msgManager = new ArrayList<>(); |
| 98 | + Message userMsg = Message.builder() |
| 99 | + .role(Role.USER.getValue()) |
| 100 | + .content("通过如下命令可以看出安装thrift的版本信息;") |
| 101 | + .build(); |
| 102 | + msgManager.add(userMsg); |
| 103 | + |
| 104 | + TranslationOptions translationOptions = TranslationOptions.builder() |
| 105 | + .sourceLang("Chinese") |
| 106 | + .targetLang("English") |
| 107 | + .tmList(Arrays.asList( |
| 108 | + TranslationOptions.Tm.builder() |
| 109 | + .source("您可以通过如下方式查看集群的内核版本信息:") |
| 110 | + .target("You can use one of the following methods to query the engine version of a cluster:") |
| 111 | + .build(), |
| 112 | + TranslationOptions.Tm.builder() |
| 113 | + .source("我们云HBase的thrift环境是0.9.0,所以建议客户端的版本也为 0.9.0,可以从这里下载thrift的0.9.0 版本,下载的源码包我们后面会用到,这里需要先安装thrift编译环境,对于源码安装可以参考thrift官网;") |
| 114 | + .target("The version of Thrift used by ApsaraDB for HBase is 0.9.0. Therefore, we recommend that you use Thrift 0.9.0 to create a client. Click here to download Thrift 0.9.0. The downloaded source code package will be used later. You must install the Thrift compiling environment first. For more information, see Thrift official website.") |
| 115 | + .build(), |
| 116 | + TranslationOptions.Tm.builder() |
| 117 | + .source("您可以通过PyPI来安装SDK,安装命令如下:") |
| 118 | + .target("You can run the following command in Python Package Index (PyPI) to install Elastic Container Instance SDK for Python:") |
| 119 | + .build() |
| 120 | + )) |
| 121 | + .build(); |
| 122 | + |
| 123 | + GenerationParam param = |
| 124 | + GenerationParam.builder() |
| 125 | + .model(MODE_NAME) |
| 126 | + .messages(msgManager) |
| 127 | + .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| 128 | + .incrementalOutput(true) |
| 129 | + .translationOptions(translationOptions) |
| 130 | + .build(); |
| 131 | + Flowable<GenerationResult> result = gen.streamCall(param); |
| 132 | + result.blockingSubscribe( data -> { |
| 133 | + System.out.println(JsonUtils.toJson(data)); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + public static void streamCallDomains() |
| 138 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 139 | + Generation gen = new Generation(); |
| 140 | + List<Message> msgManager = new ArrayList<>(); |
| 141 | + Message userMsg = Message.builder() |
| 142 | + .role(Role.USER.getValue()) |
| 143 | + .content("第二个SELECT语句返回一个数字,表示在没有LIMIT子句的情况下,第一个SELECT语句返回了多少行。") |
| 144 | + .build(); |
| 145 | + msgManager.add(userMsg); |
| 146 | + |
| 147 | + TranslationOptions translationOptions = TranslationOptions.builder() |
| 148 | + .sourceLang("Chinese") |
| 149 | + .targetLang("English") |
| 150 | + .domains("The sentence is from Ali Cloud IT domain. It mainly involves computer-related software development and usage methods, including many terms related to computer software and hardware. Pay attention to professional troubleshooting terminologies and sentence patterns when translating. Translate into this IT domain style.") |
| 151 | + .build(); |
| 152 | + |
| 153 | + GenerationParam param = |
| 154 | + GenerationParam.builder() |
| 155 | + .model(MODE_NAME) |
| 156 | + .messages(msgManager) |
| 157 | + .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| 158 | + .incrementalOutput(true) |
| 159 | + .translationOptions(translationOptions) |
| 160 | + .build(); |
| 161 | + Flowable<GenerationResult> result = gen.streamCall(param); |
| 162 | + result.blockingSubscribe( data -> { |
| 163 | + System.out.println(JsonUtils.toJson(data)); |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + public static void main(String[] args){ |
| 168 | + try { |
| 169 | +// baseCall(); |
| 170 | +// streamCallTerms(); |
| 171 | +// streamCallTmList(); |
| 172 | + streamCallDomains(); |
| 173 | + } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
| 174 | + System.out.println(e.getMessage()); |
| 175 | + } |
| 176 | + System.exit(0); |
| 177 | + } |
| 178 | +} |
0 commit comments