Skip to content

Commit 75cf320

Browse files
committed
Merge branch 'develop'
2 parents e9b3eb5 + e134ccc commit 75cf320

File tree

10 files changed

+1168
-122
lines changed

10 files changed

+1168
-122
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@
7272
<artifactId>junit-jupiter-api</artifactId>
7373
<version>5.10.2</version>
7474
<scope>test</scope>
75-
</dependency>
75+
</dependency>
7676
<dependency>
77-
<groupId>org.junit-pioneer</groupId>
78-
<artifactId>junit-pioneer</artifactId>
79-
<version>1.9.1</version>
80-
<scope>test</scope>
81-
</dependency>
77+
<groupId>org.junit-pioneer</groupId>
78+
<artifactId>junit-pioneer</artifactId>
79+
<version>1.9.1</version>
80+
<scope>test</scope>
81+
</dependency>
8282
<dependency>
8383
<groupId>io.javalin</groupId>
8484
<artifactId>javalin</artifactId>

samples/GenerationStreamCall.java

Lines changed: 212 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,241 @@
11
// Copyright (c) Alibaba, Inc. and its affiliates.
22

33
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;
410
import com.alibaba.dashscope.aigc.generation.Generation;
511
import com.alibaba.dashscope.aigc.generation.GenerationParam;
612
import com.alibaba.dashscope.aigc.generation.GenerationResult;
713
import com.alibaba.dashscope.aigc.generation.SearchOptions;
14+
import com.alibaba.dashscope.common.Message;
15+
import com.alibaba.dashscope.common.Role;
816
import com.alibaba.dashscope.common.ResultCallback;
917
import com.alibaba.dashscope.exception.ApiException;
1018
import com.alibaba.dashscope.exception.InputRequiredException;
1119
import com.alibaba.dashscope.exception.NoApiKeyException;
20+
import com.alibaba.dashscope.tools.FunctionDefinition;
21+
import com.alibaba.dashscope.tools.ToolFunction;
1222
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;
1330
import io.reactivex.Flowable;
1431

1532
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+
});
8950
}
9051

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+
91215
// try {
92216
// streamCallWithCallback();
93217
// } catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
94218
// System.out.println(e.getMessage());
95219
// }
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+
97233
// try {
98234
// streamCallWithSearchOptions();
99235
// } catch (ApiException | NoApiKeyException | InputRequiredException e) {
100236
// System.out.println(e.getMessage());
101237
// }
102238

103-
System.exit(0);
104-
}
239+
System.exit(0);
240+
}
105241
}

0 commit comments

Comments
 (0)