|
| 1 | +# dashscope-sdk-java |
| 2 | + |
| 3 | +这是 DashScope 模型的 Java SDK。 |
| 4 | + |
| 5 | +## 使用方法 |
| 6 | + |
| 7 | +要在您的 Java 系统中使用该 SDK,请在 pom.xml 中添加以下 Maven 依赖: |
| 8 | + |
| 9 | +```xml |
| 10 | +<dependency> |
| 11 | + <groupId>com.alibaba</groupId> |
| 12 | + <artifactId>dashscope-sdk-java</artifactId> |
| 13 | + <version>{dashscope-sdk-java-version}</version> |
| 14 | +</dependency> |
| 15 | +``` |
| 16 | + |
| 17 | +## 快速开始 |
| 18 | + |
| 19 | +### 文本生成 |
| 20 | + |
| 21 | +您可以通过以下方式创建文本生成客户端: |
| 22 | + |
| 23 | +```java |
| 24 | +Generation generation = new Generation(); |
| 25 | +``` |
| 26 | + |
| 27 | +文本生成接口支持流式和非流式查询。这些查询都接受 `GenerationParam` 作为输入,并返回 `GenerationResult` 作为输出。 |
| 28 | + |
| 29 | +以下是各方法的使用示例,以 `qwen-turbo` 模型为例。 |
| 30 | + |
| 31 | +#### 支持流式和非流式模式,通过回调接收输出 |
| 32 | + |
| 33 | +```java |
| 34 | +import com.alibaba.dashscope.aigc.generation.Generation; |
| 35 | +import com.alibaba.dashscope.aigc.generation.GenerationParam; |
| 36 | +import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| 37 | +import com.alibaba.dashscope.common.Message; |
| 38 | +import com.alibaba.dashscope.common.ResultCallback; |
| 39 | +import com.alibaba.dashscope.common.Role; |
| 40 | +import com.alibaba.dashscope.exception.ApiException; |
| 41 | +import com.alibaba.dashscope.utils.JsonUtils; |
| 42 | +import java.util.Arrays; |
| 43 | + |
| 44 | +public class Main { |
| 45 | + |
| 46 | + public static void main(String[] args) { |
| 47 | + Generation generation = new Generation(); |
| 48 | + GenerationParam param = GenerationParam.builder() |
| 49 | + .apiKey(System.getenv("DASHSCOPE_API_KEY")) |
| 50 | + .model(Generation.Models.QWEN_TURBO) |
| 51 | + .messages(Arrays.asList( |
| 52 | + Message.builder() |
| 53 | + .role(Role.USER.getValue()) |
| 54 | + .content("Hello, how are you?").build() |
| 55 | + )).build(); |
| 56 | + |
| 57 | + class ReactCallback extends ResultCallback<GenerationResult> { |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onEvent(GenerationResult message) { |
| 61 | + System.out.println(JsonUtils.toJson(message)); |
| 62 | + } |
| 63 | + |
| 64 | + public void onComplete() { |
| 65 | + // TODO 所有消息已接收 |
| 66 | + } |
| 67 | + |
| 68 | + public void onError(Exception e) { |
| 69 | + ApiException apiException = (ApiException) e; |
| 70 | + // TODO 处理异常 |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + generation.call(param, new ReactCallback()); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +异常实例是一个 `ApiException` 实例。该异常可能包含两部分: |
| 80 | + |
| 81 | +- 一个 `Status` 实例。该实例包含 status_code(HTTP 错误码)、code(服务器错误码)、message(服务器错误信息)、请求 ID 和使用信息。 |
| 82 | +- 如果发生异常,`ApiException` 实例可能只包含一个 `Exception` 堆栈跟踪,您可以像平常一样处理它。 |
| 83 | + |
| 84 | +#### 仅流式模式,通过响应式 IO 接收 |
| 85 | + |
| 86 | +```java |
| 87 | +import com.alibaba.dashscope.aigc.generation.Generation; |
| 88 | +import com.alibaba.dashscope.aigc.generation.GenerationParam; |
| 89 | +import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| 90 | +import com.alibaba.dashscope.common.Message; |
| 91 | +import com.alibaba.dashscope.common.Role; |
| 92 | +import com.alibaba.dashscope.exception.ApiException; |
| 93 | +import com.alibaba.dashscope.exception.InputRequiredException; |
| 94 | +import com.alibaba.dashscope.exception.NoApiKeyException; |
| 95 | +import com.alibaba.dashscope.utils.JsonUtils; |
| 96 | +import io.reactivex.Flowable; |
| 97 | +import java.util.Arrays; |
| 98 | + |
| 99 | +public class Main { |
| 100 | + |
| 101 | + public static void main(String[] args) { |
| 102 | + Generation generation = new Generation(); |
| 103 | + |
| 104 | + Message systemMsg = Message.builder() |
| 105 | + .role(Role.SYSTEM.getValue()) |
| 106 | + .content("You are a helpful assistant.") |
| 107 | + .build(); |
| 108 | + Message userMsg = Message.builder() |
| 109 | + .role(Role.USER.getValue()) |
| 110 | + .content("Hello!") |
| 111 | + .build(); |
| 112 | + GenerationParam param = GenerationParam.builder() |
| 113 | + .apiKey(System.getenv("DASHSCOPE_API_KEY")) |
| 114 | + .model(Generation.Models.QWEN_TURBO) |
| 115 | + .messages(Arrays.asList(systemMsg, userMsg)) |
| 116 | + .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| 117 | + .build(); |
| 118 | + |
| 119 | + try { |
| 120 | + Flowable<GenerationResult> result = generation.streamCall(param); |
| 121 | + result.blockingForEach(msg -> System.out.println(JsonUtils.toJson(msg))); |
| 122 | + } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
| 123 | + System.err.println("An error occurred: " + e.getMessage()); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +`streamCall` 方法接受 `GenerationParam`,并返回一个 `Flowable`,您可以通过 `blockingForEach` 获取流式结果,并通过 try-catch 块捕获异常。 |
| 130 | + |
| 131 | +#### 仅非流式模式 |
| 132 | + |
| 133 | +```java |
| 134 | +import com.alibaba.dashscope.aigc.generation.Generation; |
| 135 | +import com.alibaba.dashscope.aigc.generation.GenerationParam; |
| 136 | +import com.alibaba.dashscope.aigc.generation.GenerationResult; |
| 137 | +import com.alibaba.dashscope.common.Message; |
| 138 | +import com.alibaba.dashscope.common.Role; |
| 139 | +import com.alibaba.dashscope.exception.ApiException; |
| 140 | +import com.alibaba.dashscope.exception.InputRequiredException; |
| 141 | +import com.alibaba.dashscope.exception.NoApiKeyException; |
| 142 | +import com.alibaba.dashscope.utils.JsonUtils; |
| 143 | +import java.util.Arrays; |
| 144 | + |
| 145 | +public class Main { |
| 146 | + |
| 147 | + public static void main(String[] args) { |
| 148 | + Generation generation = new Generation(); |
| 149 | + |
| 150 | + Message systemMsg = Message.builder() |
| 151 | + .role(Role.SYSTEM.getValue()) |
| 152 | + .content("You are a helpful assistant.") |
| 153 | + .build(); |
| 154 | + Message userMsg = Message.builder() |
| 155 | + .role(Role.USER.getValue()) |
| 156 | + .content("Hello!") |
| 157 | + .build(); |
| 158 | + GenerationParam param = GenerationParam.builder() |
| 159 | + .apiKey(System.getenv("DASHSCOPE_API_KEY")) |
| 160 | + .model(Generation.Models.QWEN_TURBO) |
| 161 | + .messages(Arrays.asList(systemMsg, userMsg)) |
| 162 | + .resultFormat(GenerationParam.ResultFormat.MESSAGE) |
| 163 | + .build(); |
| 164 | + |
| 165 | + try { |
| 166 | + GenerationResult result = generation.call(param); |
| 167 | + System.out.println(JsonUtils.toJson(result)); |
| 168 | + } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
| 169 | + System.err.println("An error occurred: " + e.getMessage()); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +`call` 方法接受 `GenerationParam`,并返回 `GenerationResult`,您也可以通过 try-catch 块捕获异常。 |
0 commit comments