Repository navigation
Expand file tree
/
Copy pathGenerationStreamCall.java
More file actions
99 lines (87 loc) · 3.64 KB
/
Copy pathGenerationStreamCall.java
File metadata and controls
99 lines (87 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) Alibaba, Inc. and its affiliates.
import java.util.concurrent.Semaphore;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.SearchOptions;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
public class GenerationStreamCall {
public static void streamCall()
throws NoApiKeyException, ApiException, InputRequiredException {
Generation gen = new Generation();
GenerationParam param = GenerationParam.builder().model(Generation.Models.QWEN_PLUS)
.prompt("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品").topP(0.8).build();
Flowable<GenerationResult> result = gen.streamCall(param);
result.blockingForEach(message -> {
System.out.println(JsonUtils.toJson(message));
});
}
public static void streamCallWithCallback()
throws NoApiKeyException, ApiException, InputRequiredException,InterruptedException {
Generation gen = new Generation();
GenerationParam param = GenerationParam.builder().model(Generation.Models.QWEN_PLUS)
.prompt("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品").topP(0.8).build();
Semaphore semaphore = new Semaphore(0);
gen.streamCall(param, new ResultCallback<GenerationResult>() {
@Override
public void onEvent(GenerationResult message) {
System.out.println(message);
}
@Override
public void onError(Exception err){
System.out.println(String.format("Exception: %s", err.getMessage()));
semaphore.release();
}
@Override
public void onComplete(){
System.out.println("Completed");
semaphore.release();
}
});
semaphore.acquire();
}
public static void streamCallWithSearchOptions()
throws NoApiKeyException, ApiException, InputRequiredException {
Generation gen = new Generation();
GenerationParam param = GenerationParam.builder()
.model(Generation.Models.QWEN_PLUS)
.prompt("联网搜索明天杭州天气如何?")
.enableSearch(true)
.resultFormat("message")
.searchOptions(SearchOptions.builder()
.enableSource(true)
.enableCitation(true)
.citationFormat("[ref_<number>]")
.searchStrategy("pro_max")
.forcedSearch(true)
.build())
.build();
Flowable<GenerationResult> result = gen.streamCall(param);
result.blockingForEach(message -> {
System.out.println(JsonUtils.toJson(message));
});
}
public static void main(String[] args) {
try {
streamCall();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
try {
streamCallWithCallback();
} catch (ApiException | NoApiKeyException | InputRequiredException | InterruptedException e) {
System.out.println(e.getMessage());
}
try {
streamCallWithSearchOptions();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.println(e.getMessage());
}
System.exit(0);
}
}