-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathCodeGeneration.java
More file actions
187 lines (170 loc) · 6.83 KB
/
Copy pathCodeGeneration.java
File metadata and controls
187 lines (170 loc) · 6.83 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Alibaba, Inc. and its affiliates.
package com.alibaba.dashscope.aigc.codegeneration;
import com.alibaba.dashscope.api.SynchronizeHalfDuplexApi;
import com.alibaba.dashscope.common.DashScopeResult;
import com.alibaba.dashscope.common.Function;
import com.alibaba.dashscope.common.OutputMode;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.common.Task;
import com.alibaba.dashscope.common.TaskGroup;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.ApiServiceOption;
import com.alibaba.dashscope.protocol.ConnectionOptions;
import com.alibaba.dashscope.protocol.HttpMethod;
import com.alibaba.dashscope.protocol.Protocol;
import com.alibaba.dashscope.protocol.StreamingMode;
import io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public final class CodeGeneration {
private final SynchronizeHalfDuplexApi<CodeGenerationParam> syncApi;
private final ApiServiceOption serviceOption;
public static class Models {
public static final String TONGYI_LINGMA_V1 = "tongyi-lingma-v1";
}
public static class Scenes {
public static final String CUSTOM = "custom";
public static final String NL2CODE = "nl2code";
public static final String CODE2COMMENT = "code2comment";
public static final String CODE2EXPLAIN = "code2explain";
public static final String COMMIT2MSG = "commit2msg";
public static final String UNIT_TEST = "unittest";
public static final String CODE_QA = "codeqa";
public static final String NL2SQL = "nl2sql";
}
private ApiServiceOption defaultServiceOption() {
return ApiServiceOption.builder()
.protocol(Protocol.HTTP)
.httpMethod(HttpMethod.POST)
.streamingMode(StreamingMode.OUT)
.outputMode(OutputMode.ACCUMULATE)
.taskGroup(TaskGroup.AIGC.getValue())
.task(Task.CODE_GENERATION.getValue())
.function(Function.GENERATION.getValue())
.build();
}
public CodeGeneration() {
serviceOption = defaultServiceOption();
syncApi = new SynchronizeHalfDuplexApi<>(serviceOption);
}
public CodeGeneration(String protocol) {
serviceOption = defaultServiceOption();
serviceOption.setProtocol(Protocol.of(protocol));
syncApi = new SynchronizeHalfDuplexApi<>(serviceOption);
}
public CodeGeneration(String protocol, String baseUrl) {
serviceOption = defaultServiceOption();
serviceOption.setProtocol(Protocol.of(protocol));
if (Protocol.HTTP.getValue().equals(protocol)) {
serviceOption.setBaseHttpUrl(baseUrl);
} else {
serviceOption.setBaseWebSocketUrl(baseUrl);
}
syncApi = new SynchronizeHalfDuplexApi<>(serviceOption);
}
public CodeGeneration(String protocol, String baseUrl, ConnectionOptions connectionOptions) {
serviceOption = defaultServiceOption();
serviceOption.setProtocol(Protocol.of(protocol));
if (Protocol.HTTP.getValue().equals(protocol)) {
serviceOption.setBaseHttpUrl(baseUrl);
} else {
serviceOption.setBaseWebSocketUrl(baseUrl);
}
syncApi = new SynchronizeHalfDuplexApi<>(connectionOptions, serviceOption);
}
/**
* Call the server to get the whole result.
*
* @param param The input param of class `CodeGenerationParam`.
* @return The output structure of `CodeGenerationResult`.
* @throws NoApiKeyException Can not find api key
* @throws InputRequiredException Missing inputs.
*/
public CodeGenerationResult call(CodeGenerationParam param)
throws ApiException, NoApiKeyException, InputRequiredException {
param.validate();
serviceOption.setIsSSE(false);
serviceOption.setStreamingMode(StreamingMode.NONE);
return CodeGenerationResult.fromDashScopeResult(syncApi.call(param));
}
/**
* Call the server to get the result in the callback function.
*
* @param param The input param of class `CodeGenerationParam`.
* @param callback The callback to receive response, the template class is `CodeGenerationResult`.
* @throws NoApiKeyException Can not find api key
* @throws ApiException The request failed, possibly due to a network or data error.
* @throws InputRequiredException Missing inputs.
*/
public void call(CodeGenerationParam param, ResultCallback<CodeGenerationResult> callback)
throws ApiException, NoApiKeyException, InputRequiredException {
param.validate();
serviceOption.setIsSSE(false);
serviceOption.setStreamingMode(StreamingMode.NONE);
syncApi.call(
param,
new ResultCallback<DashScopeResult>() {
@Override
public void onEvent(DashScopeResult message) {
callback.onEvent(CodeGenerationResult.fromDashScopeResult(message));
}
@Override
public void onComplete() {
callback.onComplete();
}
@Override
public void onError(Exception e) {
callback.onError(e);
}
});
}
/**
* Call the server to get the result by stream.
*
* @param param The input param of class `CodeGenerationParam`.
* @return A `Flowable` of the output structure.
* @throws NoApiKeyException Can not find api key
* @throws ApiException The request failed, possibly due to a network or data error.
* @throws InputRequiredException Missing inputs.
*/
public Flowable<CodeGenerationResult> streamCall(CodeGenerationParam param)
throws ApiException, NoApiKeyException, InputRequiredException {
param.validate();
serviceOption.setIsSSE(true);
serviceOption.setStreamingMode(StreamingMode.OUT);
return syncApi.streamCall(param).map(item -> CodeGenerationResult.fromDashScopeResult(item));
}
/**
* Call the server to get the result by stream.
*
* @param param The input param of class `CodeGenerationParam`.
* @param callback The result callback.
* @throws NoApiKeyException Can not find api key
* @throws ApiException The request failed, possibly due to a network or data error.
* @throws InputRequiredException The input field is missing.
*/
public void streamCall(CodeGenerationParam param, ResultCallback<CodeGenerationResult> callback)
throws ApiException, NoApiKeyException, InputRequiredException {
param.validate();
serviceOption.setIsSSE(true);
serviceOption.setStreamingMode(StreamingMode.OUT);
syncApi.streamCall(
param,
new ResultCallback<DashScopeResult>() {
@Override
public void onEvent(DashScopeResult msg) {
callback.onEvent(CodeGenerationResult.fromDashScopeResult(msg));
}
@Override
public void onComplete() {
callback.onComplete();
}
@Override
public void onError(Exception e) {
callback.onError(e);
}
});
}
}