Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
264 lines (242 loc) · 17.9 KB

File metadata and controls

264 lines (242 loc) · 17.9 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import com.alibaba.dashscope.aigc.codegeneration.CodeGeneration;
import com.alibaba.dashscope.aigc.codegeneration.CodeGenerationParam;
import com.alibaba.dashscope.aigc.codegeneration.CodeGenerationResult;
import com.alibaba.dashscope.aigc.codegeneration.models.AttachmentRoleMessageParam;
import com.alibaba.dashscope.aigc.codegeneration.models.MessageParamBase;
import com.alibaba.dashscope.aigc.codegeneration.models.UserRoleMessageParam;
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.protocol.Protocol;
import com.alibaba.dashscope.utils.JsonUtils;
import io.reactivex.Flowable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
public class CodeGenerationQuickStart {
public static final String MODEL = CodeGeneration.Models.TONGYI_LINGMA_V1;
public static void testCustomSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("根据下面的功能描述生成一个python函数。代码的功能是计算给定路径下所有文件的总大小。"));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CUSTOM)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testCustomSample:" + JsonUtils.toJson(result));
}
public static void testNl2codeSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("计算给定路径下所有文件的总大小"));
String meta = "{\"language\": \"java\"}";
messageParams.add(new AttachmentRoleMessageParam(JsonUtils.parse(meta)));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.NL2CODE)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testNl2codeSample:" + JsonUtils.toJson(result));
}
public static void testCode2commentSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("1. 生成中文注释\\n2. 仅生成代码部分,不需要额外解释函数功能\\n"));
String meta = "{\"code\": \"\\t\\t@Override\\n\\t\\tpublic CancelExportTaskResponse cancelExportTask(\\n\\t\\t\\t\\tCancelExportTask cancelExportTask) {\\n\\t\\t\\tAmazonEC2SkeletonInterface ec2Service = ServiceProvider.getInstance().getServiceImpl(AmazonEC2SkeletonInterface.class);\\n\\t\\t\\treturn ec2Service.cancelExportTask(cancelExportTask);\\n\\t\\t}\", \"language\": \"java\"}";
messageParams.add(new AttachmentRoleMessageParam(JsonUtils.parse(meta)));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CODE2COMMENT)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testCode2commentSample:" + JsonUtils.toJson(result));
}
public static void testCode2explainSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("要求不低于200字"));
String meta = "{\"code\": \"@Override\\n public int getHeaderCacheSize()\\n {\\n return 0;\\n }\\n\\n\", \"language\": \"java\"}";
messageParams.add(new AttachmentRoleMessageParam(JsonUtils.parse(meta)));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CODE2EXPLAIN)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testCode2explainSample:" + JsonUtils.toJson(result));
}
public static void testCommit2msgSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
String meta = "{\"diff_list\": [{\"diff\": \"--- src/com/siondream/core/PlatformResolver.java\\n+++ src/com/siondream/core/PlatformResolver.java\\n@@ -1,11 +1,8 @@\\npackage com.siondream.core;\\n-\\n-import com.badlogic.gdx.files.FileHandle;\\n\\npublic interface PlatformResolver {\\npublic void openURL(String url);\\npublic void rateApp();\\npublic void sendFeedback();\\n-\\tpublic FileHandle[] listFolder(String path);\\n}\\n\", \"old_file_path\": \"src/com/siondream/core/PlatformResolver.java\", \"new_file_path\": \"src/com/siondream/core/PlatformResolver.java\"}]}";
messageParams.add(new AttachmentRoleMessageParam(JsonUtils.parse(meta)));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.COMMIT2MSG)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testCommit2msgSample:" + JsonUtils.toJson(result));
}
public static void testUnittestSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
String meta = "{\"code\": \"public static <T> TimestampMap<T> parseTimestampMap(Class<T> typeClass, String input, DateTimeZone timeZone) throws IllegalArgumentException {\\n if (typeClass == null) {\\n throw new IllegalArgumentException(\\\"typeClass required\\\");\\n }\\n\\n if (input == null) {\\n return null;\\n }\\n\\n TimestampMap result;\\n\\n typeClass = AttributeUtils.getStandardizedType(typeClass);\\n if (typeClass.equals(String.class)) {\\n result = new TimestampStringMap();\\n } else if (typeClass.equals(Byte.class)) {\\n result = new TimestampByteMap();\\n } else if (typeClass.equals(Short.class)) {\\n result = new TimestampShortMap();\\n } else if (typeClass.equals(Integer.class)) {\\n result = new TimestampIntegerMap();\\n } else if (typeClass.equals(Long.class)) {\\n result = new TimestampLongMap();\\n } else if (typeClass.equals(Float.class)) {\\n result = new TimestampFloatMap();\\n } else if (typeClass.equals(Double.class)) {\\n result = new TimestampDoubleMap();\\n } else if (typeClass.equals(Boolean.class)) {\\n result = new TimestampBooleanMap();\\n } else if (typeClass.equals(Character.class)) {\\n result = new TimestampCharMap();\\n } else {\\n throw new IllegalArgumentException(\\\"Unsupported type \\\" + typeClass.getClass().getCanonicalName());\\n }\\n\\n if (input.equalsIgnoreCase(EMPTY_VALUE)) {\\n return result;\\n }\\n\\n StringReader reader = new StringReader(input + ' ');// Add 1 space so\\n // reader.skip\\n // function always\\n // works when\\n // necessary (end of\\n // string not\\n // reached).\\n\\n try {\\n int r;\\n char c;\\n while ((r = reader.read()) != -1) {\\n c = (char) r;\\n switch (c) {\\n case LEFT_BOUND_SQUARE_BRACKET:\\n case LEFT_BOUND_BRACKET:\\n parseTimestampAndValue(typeClass, reader, result, timeZone);\\n break;\\n default:\\n // Ignore other chars outside of bounds\\n }\\n }\\n } catch (IOException ex) {\\n throw new RuntimeException(\\\"Unexpected expection while parsing timestamps\\\", ex);\\n }\\n\\n return result;\\n }\", \"language\": \"java\"}";
messageParams.add(new AttachmentRoleMessageParam(JsonUtils.parse(meta)));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.UNIT_TEST)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testUnittestSample:" + JsonUtils.toJson(result));
}
public static void testCodeqaSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("I'm writing a small web server in Python, using BaseHTTPServer and a custom subclass of BaseHTTPServer.BaseHTTPRequestHandler. Is it possible to make this listen on more than one port?\\nWhat I'm doing now:\\nclass MyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):\\n def doGET\\n [...]\\n\\nclass ThreadingHTTPServer(ThreadingMixIn, HTTPServer): \\n pass\\n\\nserver = ThreadingHTTPServer(('localhost', 80), MyRequestHandler)\\nserver.serve_forever()"));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CODE_QA)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testCodeqaSample:" + JsonUtils.toJson(result));
}
public static void testNl2sqlSample() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("小明的总分数是多少"));
String meta = "{\"synonym_infos\": {\"学生姓名\": \"姓名|名字|名称\", \"学生分数\": \"分数|得分\"}, \"recall_infos\": [{\"content\": \"student_score.id='小明'\", \"score\": \"0.83\"}], \"schema_infos\": [{\"table_id\": \"student_score\", \"table_desc\": \"学生分数表\", \"columns\": [{\"col_name\": \"id\", \"col_caption\": \"学生id\", \"col_desc\": \"例值为:1,2,3\", \"col_type\": \"string\"}, {\"col_name\": \"name\", \"col_caption\": \"学生姓名\", \"col_desc\": \"例值为:张三,李四,小明\", \"col_type\": \"string\"}, {\"col_name\": \"score\", \"col_caption\": \"学生分数\", \"col_desc\": \"例值为:98,100,66\", \"col_type\": \"string\"}]}]}";
messageParams.add(new AttachmentRoleMessageParam(JsonUtils.parse(meta)));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.NL2SQL)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
CodeGenerationResult result = generation.call(param);
System.out.println("testNl2sqlSample:" + JsonUtils.toJson(result));
}
public static void testCustomSampleWithCallBack() throws ApiException, NoApiKeyException, InterruptedException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("根据下面的功能描述生成一个python函数。代码的功能是计算给定路径下所有文件的总大小。"));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CUSTOM)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
Semaphore semaphore = new Semaphore(0);
List<CodeGenerationResult> results = new ArrayList<>();
generation.call(
param,
new ResultCallback<CodeGenerationResult>() {
@Override
public void onEvent(CodeGenerationResult msg) {
results.add(msg);
}
@Override
public void onComplete() {
semaphore.release();
}
@Override
public void onError(Exception e) {
semaphore.release();
}
});
semaphore.acquire();
System.out.println("testCustomSampleWithCallBack:" + JsonUtils.toJson(results));
}
public static void testCustomSampleWithStream() throws ApiException, NoApiKeyException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("根据下面的功能描述生成一个python函数。代码的功能是计算给定路径下所有文件的总大小。"));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CUSTOM)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
Flowable<CodeGenerationResult> flowable = generation.streamCall(param);
flowable.blockingForEach(result -> {
System.out.println("testCustomSampleWithStream forEach:" + JsonUtils.toJson(result));
});
}
public static void testCustomSampleWithStreamCallBack() throws ApiException, NoApiKeyException, InterruptedException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("根据下面的功能描述生成一个python函数。代码的功能是计算给定路径下所有文件的总大小。"));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CUSTOM)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration();
Semaphore semaphore = new Semaphore(0);
List<CodeGenerationResult> results = new ArrayList<>();
generation.streamCall(
param,
new ResultCallback<CodeGenerationResult>() {
@Override
public void onEvent(CodeGenerationResult msg) {
results.add(msg);
}
@Override
public void onComplete() {
semaphore.release();
}
@Override
public void onError(Exception e) {
semaphore.release();
}
});
semaphore.acquire();
System.out.println("testCustomSampleWithStreamCallBack:" + JsonUtils.toJson(results));
}
public static void testCustomSampleWithWebSocket() throws ApiException, NoApiKeyException, InterruptedException, InputRequiredException {
List<MessageParamBase> messageParams = new ArrayList<>();
messageParams.add(new UserRoleMessageParam("根据下面的功能描述生成一个python函数。代码的功能是计算给定路径下所有文件的总大小。"));
CodeGenerationParam param =
CodeGenerationParam.builder()
.model(MODEL)
.scene(CodeGeneration.Scenes.CUSTOM)
.message(messageParams)
.build();
CodeGeneration generation = new CodeGeneration(Protocol.WEBSOCKET.getValue());
CodeGenerationResult result = generation.call(param);
System.out.println(result);
System.exit(0);
}
public static void main(String[] args) {
try {
testCustomSample();
testNl2codeSample();
testCode2commentSample();
testCode2explainSample();
testCommit2msgSample();
testUnittestSample();
testCodeqaSample();
testNl2sqlSample();
testCustomSampleWithCallBack();
testCustomSampleWithStream();
testCustomSampleWithStreamCallBack();
testCustomSampleWithWebSocket();
} catch (NoApiKeyException | InputRequiredException | InterruptedException e) {
e.printStackTrace();
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.