|
1 | 1 | // Copyright (c) Alibaba, Inc. and its affiliates. |
2 | 2 |
|
3 | 3 | import java.util.concurrent.Semaphore; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.time.LocalDateTime; |
| 8 | +import java.time.format.DateTimeFormatter; |
| 9 | +import com.alibaba.dashscope.aigc.conversation.ConversationParam.ResultFormat; |
4 | 10 | import com.alibaba.dashscope.aigc.generation.Generation; |
5 | 11 | import com.alibaba.dashscope.aigc.generation.GenerationParam; |
6 | 12 | import com.alibaba.dashscope.aigc.generation.GenerationResult; |
7 | 13 | import com.alibaba.dashscope.aigc.generation.SearchOptions; |
| 14 | +import com.alibaba.dashscope.common.Message; |
| 15 | +import com.alibaba.dashscope.common.Role; |
8 | 16 | import com.alibaba.dashscope.common.ResultCallback; |
9 | 17 | import com.alibaba.dashscope.exception.ApiException; |
10 | 18 | import com.alibaba.dashscope.exception.InputRequiredException; |
11 | 19 | import com.alibaba.dashscope.exception.NoApiKeyException; |
| 20 | +import com.alibaba.dashscope.tools.FunctionDefinition; |
| 21 | +import com.alibaba.dashscope.tools.ToolFunction; |
12 | 22 | import com.alibaba.dashscope.utils.JsonUtils; |
| 23 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | +import com.github.victools.jsonschema.generator.Option; |
| 25 | +import com.github.victools.jsonschema.generator.OptionPreset; |
| 26 | +import com.github.victools.jsonschema.generator.SchemaGenerator; |
| 27 | +import com.github.victools.jsonschema.generator.SchemaGeneratorConfig; |
| 28 | +import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder; |
| 29 | +import com.github.victools.jsonschema.generator.SchemaVersion; |
13 | 30 | import io.reactivex.Flowable; |
14 | 31 |
|
15 | 32 | public class GenerationStreamCall { |
16 | | - public static void streamCall() |
17 | | - throws NoApiKeyException, ApiException, InputRequiredException { |
18 | | - Generation gen = new Generation(); |
19 | | - GenerationParam param = GenerationParam.builder() |
20 | | - .model("qwen3-max") |
21 | | - .prompt("你好") |
22 | | - .topP(0.8) |
23 | | - .incrementalOutput(false) |
24 | | - .build(); |
25 | | - Flowable<GenerationResult> result = gen.streamCall(param); |
26 | | - result.blockingForEach(message -> { |
27 | | - System.out.println("generation_result:"); |
28 | | - System.out.println(message); |
29 | | - System.out.println(JsonUtils.toJson(message) + "\n"); |
30 | | - }); |
31 | | - } |
32 | | - |
33 | | - public static void streamCallWithCallback() |
34 | | - throws NoApiKeyException, ApiException, InputRequiredException,InterruptedException { |
35 | | - Generation gen = new Generation(); |
36 | | - GenerationParam param = GenerationParam.builder().model(Generation.Models.QWEN_PLUS) |
37 | | - .prompt("你好").topP(0.8).build(); |
38 | | - Semaphore semaphore = new Semaphore(0); |
39 | | - gen.streamCall(param, new ResultCallback<GenerationResult>() { |
40 | | - |
41 | | - @Override |
42 | | - public void onEvent(GenerationResult message) { |
43 | | - System.out.println(message); |
44 | | - } |
45 | | - @Override |
46 | | - public void onError(Exception err){ |
47 | | - System.out.println(String.format("Exception: %s", err.getMessage())); |
48 | | - semaphore.release(); |
49 | | - } |
50 | | - |
51 | | - @Override |
52 | | - public void onComplete(){ |
53 | | - System.out.println("Completed"); |
54 | | - semaphore.release(); |
55 | | - } |
56 | | - |
57 | | - }); |
58 | | - semaphore.acquire(); |
59 | | - |
60 | | - } |
61 | | - |
62 | | - public static void streamCallWithSearchOptions() |
63 | | - throws NoApiKeyException, ApiException, InputRequiredException { |
64 | | - Generation gen = new Generation(); |
65 | | - GenerationParam param = GenerationParam.builder() |
66 | | - .model(Generation.Models.QWEN_PLUS) |
67 | | - .prompt("联网搜索明天杭州天气如何?") |
68 | | - .enableSearch(true) |
69 | | - .resultFormat("message") |
70 | | - .searchOptions(SearchOptions.builder() |
71 | | - .enableSource(true) |
72 | | - .enableCitation(true) |
73 | | - .citationFormat("[ref_<number>]") |
74 | | - .searchStrategy("pro_max") |
75 | | - .forcedSearch(true) |
76 | | - .build()) |
77 | | - .build(); |
78 | | - Flowable<GenerationResult> result = gen.streamCall(param); |
79 | | - result.blockingForEach(message -> { |
80 | | - System.out.println(JsonUtils.toJson(message)); |
81 | | - }); |
82 | | - } |
83 | | - |
84 | | - public static void main(String[] args) { |
85 | | - try { |
86 | | - streamCall(); |
87 | | - } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
88 | | - System.out.println(e.getMessage()); |
| 33 | + public static void streamCall() |
| 34 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 35 | + GenerationParam param = |
| 36 | + GenerationParam.builder() |
| 37 | + .model("qwen-turbo") |
| 38 | + .prompt("如何做土豆炖猪脚?") |
| 39 | + .temperature((float) 1.0) |
| 40 | + .repetitionPenalty((float) 1.0) |
| 41 | + .topK(50) |
| 42 | + .build(); |
| 43 | + System.out.println(param.getHttpBody().toString()); |
| 44 | + Generation generation = new Generation(); |
| 45 | + Flowable<GenerationResult> flowable = generation.streamCall(param); |
| 46 | + flowable.blockingForEach(message -> { |
| 47 | + System.out.println(JsonUtils.toJson(message)); |
| 48 | + Long time = System.currentTimeMillis(); |
| 49 | + }); |
89 | 50 | } |
90 | 51 |
|
| 52 | + public static void streamCallWithCallback() |
| 53 | + throws NoApiKeyException, ApiException, InputRequiredException,InterruptedException { |
| 54 | + Generation gen = new Generation(); |
| 55 | + GenerationParam param = GenerationParam.builder() |
| 56 | + .model(Generation.Models.QWEN_PLUS) |
| 57 | + .prompt("你好") |
| 58 | + .topP(0.8) |
| 59 | + .incrementalOutput(false) |
| 60 | + .build(); |
| 61 | + Semaphore semaphore = new Semaphore(0); |
| 62 | + gen.streamCall(param, new ResultCallback<GenerationResult>() { |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onEvent(GenerationResult message) { |
| 66 | + System.out.println(message); |
| 67 | + } |
| 68 | + @Override |
| 69 | + public void onError(Exception err){ |
| 70 | + System.out.println(String.format("Exception: %s", err.getMessage())); |
| 71 | + semaphore.release(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void onComplete(){ |
| 76 | + System.out.println("Completed"); |
| 77 | + semaphore.release(); |
| 78 | + } |
| 79 | + |
| 80 | + }); |
| 81 | + semaphore.acquire(); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + public static void streamCallWithReasoningContent() |
| 86 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 87 | + Generation gen = new Generation(); |
| 88 | + GenerationParam param = GenerationParam.builder() |
| 89 | + .model("qwen-plus") |
| 90 | + .prompt("1.1和0.9哪个大") |
| 91 | + .topP(0.8) |
| 92 | + .incrementalOutput(false) |
| 93 | + .enableThinking(true) |
| 94 | + .resultFormat("message") |
| 95 | + .build(); |
| 96 | + Flowable<GenerationResult> result = gen.streamCall(param); |
| 97 | + result.blockingForEach(message -> { |
| 98 | + System.out.println(JsonUtils.toJson(message)); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + public static void streamCallWithSearchOptions() |
| 104 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 105 | + Generation gen = new Generation(); |
| 106 | + GenerationParam param = GenerationParam.builder() |
| 107 | + .model(Generation.Models.QWEN_PLUS) |
| 108 | + .prompt("联网搜索明天杭州天气如何?") |
| 109 | + .enableSearch(true) |
| 110 | + .resultFormat("message") |
| 111 | + .searchOptions(SearchOptions.builder() |
| 112 | + .enableSource(true) |
| 113 | + .enableCitation(true) |
| 114 | + .citationFormat("[ref_<number>]") |
| 115 | + .searchStrategy("pro_max") |
| 116 | + .forcedSearch(true) |
| 117 | + .build()) |
| 118 | + .build(); |
| 119 | + Flowable<GenerationResult> result = gen.streamCall(param); |
| 120 | + result.blockingForEach(message -> { |
| 121 | + System.out.println(JsonUtils.toJson(message)); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + // Inner classes for tool functions |
| 126 | + public static class GetWeatherTool { |
| 127 | + private String location; |
| 128 | + |
| 129 | + public GetWeatherTool(String location) { |
| 130 | + this.location = location; |
| 131 | + } |
| 132 | + |
| 133 | + public String call() { |
| 134 | + return location + "今天是晴天"; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + public static class GetTimeTool { |
| 139 | + public GetTimeTool() { |
| 140 | + } |
| 141 | + |
| 142 | + public String call() { |
| 143 | + LocalDateTime now = LocalDateTime.now(); |
| 144 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 145 | + String currentTime = "当前时间:" + now.format(formatter) + "。"; |
| 146 | + return currentTime; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public static void streamCallWithToolCalls() |
| 151 | + throws NoApiKeyException, ApiException, InputRequiredException { |
| 152 | + SchemaGeneratorConfigBuilder configBuilder = |
| 153 | + new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2020_12, |
| 154 | + OptionPreset.PLAIN_JSON); |
| 155 | + SchemaGeneratorConfig config = configBuilder.with(Option.EXTRA_OPEN_API_FORMAT_VALUES) |
| 156 | + .without(Option.FLATTENED_ENUMS_FROM_TOSTRING).build(); |
| 157 | + SchemaGenerator generator = new SchemaGenerator(config); |
| 158 | + ObjectNode jsonSchemaWeather = generator.generateSchema(GetWeatherTool.class); |
| 159 | + ObjectNode jsonSchemaTime = generator.generateSchema(GetTimeTool.class); |
| 160 | + |
| 161 | + FunctionDefinition fdWeather = FunctionDefinition.builder() |
| 162 | + .name("get_current_weather") |
| 163 | + .description("获取指定地区的天气") |
| 164 | + .parameters(JsonUtils.parseString(jsonSchemaWeather.toString()) |
| 165 | + .getAsJsonObject()) |
| 166 | + .build(); |
| 167 | + FunctionDefinition fdTime = FunctionDefinition.builder() |
| 168 | + .name("get_current_time") |
| 169 | + .description("获取当前时刻的时间") |
| 170 | + .parameters(JsonUtils.parseString(jsonSchemaTime.toString()) |
| 171 | + .getAsJsonObject()) |
| 172 | + .build(); |
| 173 | + |
| 174 | + Message systemMsg = Message.builder() |
| 175 | + .role(Role.SYSTEM.getValue()) |
| 176 | + .content("You are a helpful assistant. When asked a question, use tools wherever possible.") |
| 177 | + .build(); |
| 178 | + Message userMsg = Message.builder() |
| 179 | + .role(Role.USER.getValue()) |
| 180 | + .content("杭州天气") |
| 181 | + .build(); |
| 182 | + |
| 183 | + List<Message> messages = new ArrayList<>(); |
| 184 | + messages.addAll(Arrays.asList(systemMsg, userMsg)); |
| 185 | + |
| 186 | + GenerationParam param = GenerationParam.builder() |
| 187 | + // 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx") |
| 188 | + .apiKey(System.getenv("DASHSCOPE_API_KEY")) |
| 189 | + // 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models |
| 190 | + .model("qwen-plus") |
| 191 | + .messages(messages) |
| 192 | + .resultFormat(ResultFormat.MESSAGE) |
| 193 | + .incrementalOutput(false) |
| 194 | + .tools(Arrays.asList( |
| 195 | + ToolFunction.builder().function(fdWeather).build(), |
| 196 | + ToolFunction.builder().function(fdTime).build())) |
| 197 | + .build(); |
| 198 | + |
| 199 | + Generation gen = new Generation(); |
| 200 | +// GenerationResult result = gen.call(param); |
| 201 | +// System.out.println(JsonUtils.toJson(result)); |
| 202 | + Flowable<GenerationResult> result = gen.streamCall(param); |
| 203 | + result.blockingForEach(message -> { |
| 204 | + System.out.println(JsonUtils.toJson(message)); |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + public static void main(String[] args) { |
| 209 | + try { |
| 210 | + streamCall(); |
| 211 | + } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
| 212 | + System.out.println(e.getMessage()); |
| 213 | + } |
| 214 | + |
91 | 215 | // try { |
92 | 216 | // streamCallWithCallback(); |
93 | 217 | // } catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) { |
94 | 218 | // System.out.println(e.getMessage()); |
95 | 219 | // } |
96 | | -// |
| 220 | + |
| 221 | +// try { |
| 222 | +// streamCallWithToolCalls(); |
| 223 | +// } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
| 224 | +// System.out.println(e.getMessage()); |
| 225 | +// } |
| 226 | + |
| 227 | +// try { |
| 228 | +// streamCallWithReasoningContent(); |
| 229 | +// } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
| 230 | +// System.out.println(e.getMessage()); |
| 231 | +// } |
| 232 | + |
97 | 233 | // try { |
98 | 234 | // streamCallWithSearchOptions(); |
99 | 235 | // } catch (ApiException | NoApiKeyException | InputRequiredException e) { |
100 | 236 | // System.out.println(e.getMessage()); |
101 | 237 | // } |
102 | 238 |
|
103 | | - System.exit(0); |
104 | | - } |
| 239 | + System.exit(0); |
| 240 | + } |
105 | 241 | } |
0 commit comments