11// Copyright (c) Alibaba, Inc. and its affiliates.
22
33import 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 ;
410import com .alibaba .dashscope .aigc .generation .Generation ;
511import com .alibaba .dashscope .aigc .generation .GenerationParam ;
612import com .alibaba .dashscope .aigc .generation .GenerationResult ;
713import com .alibaba .dashscope .aigc .generation .SearchOptions ;
14+ import com .alibaba .dashscope .common .Message ;
15+ import com .alibaba .dashscope .common .Role ;
816import com .alibaba .dashscope .common .ResultCallback ;
917import com .alibaba .dashscope .exception .ApiException ;
1018import com .alibaba .dashscope .exception .InputRequiredException ;
1119import com .alibaba .dashscope .exception .NoApiKeyException ;
20+ import com .alibaba .dashscope .tools .FunctionDefinition ;
21+ import com .alibaba .dashscope .tools .ToolFunction ;
1222import 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 ;
1330import io .reactivex .Flowable ;
1431
1532public class GenerationStreamCall {
1633 public static void streamCall ()
1734 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 -> {
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 -> {
2747 System .out .println (JsonUtils .toJson (message ));
48+ Long time = System .currentTimeMillis ();
2849 });
2950 }
3051
3152 public static void streamCallWithCallback ()
3253 throws NoApiKeyException , ApiException , InputRequiredException ,InterruptedException {
3354 Generation gen = new Generation ();
34- GenerationParam param = GenerationParam .builder ().model (Generation .Models .QWEN_PLUS )
35- .prompt ("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品" ).topP (0.8 ).build ();
55+ GenerationParam param = GenerationParam .builder ()
56+ .model (Generation .Models .QWEN_PLUS )
57+ .prompt ("你好" )
58+ .topP (0.8 )
59+ .incrementalOutput (false )
60+ .build ();
3661 Semaphore semaphore = new Semaphore (0 );
3762 gen .streamCall (param , new ResultCallback <GenerationResult >() {
3863
@@ -57,6 +82,24 @@ public void onComplete(){
5782
5883 }
5984
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+
60103 public static void streamCallWithSearchOptions ()
61104 throws NoApiKeyException , ApiException , InputRequiredException {
62105 Generation gen = new Generation ();
@@ -79,25 +122,120 @@ public static void streamCallWithSearchOptions()
79122 });
80123 }
81124
82- public static void main (String [] args ) {
83- try {
84- streamCall ();
85- } catch (ApiException | NoApiKeyException | InputRequiredException e ) {
86- System .out .println (e .getMessage ());
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 ;
87131 }
88132
89- try {
90- streamCallWithCallback ();
91- } catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e ) {
92- System .out .println (e .getMessage ());
133+ public String call () {
134+ return location + "今天是晴天" ;
93135 }
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 ));
94185
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 ) {
95209 try {
96- streamCallWithSearchOptions ();
210+ streamCall ();
97211 } catch (ApiException | NoApiKeyException | InputRequiredException e ) {
98212 System .out .println (e .getMessage ());
99213 }
100214
215+ // try {
216+ // streamCallWithCallback();
217+ // } catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
218+ // System.out.println(e.getMessage());
219+ // }
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+
233+ // try {
234+ // streamCallWithSearchOptions();
235+ // } catch (ApiException | NoApiKeyException | InputRequiredException e) {
236+ // System.out.println(e.getMessage());
237+ // }
238+
101239 System .exit (0 );
102240 }
103241}
0 commit comments