Skip to content

Commit 40f2514

Browse files
committed
feat: support vl model new parameters
1 parent 92f57c6 commit 40f2514

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

samples/MultiModalConversationUsage.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import io.reactivex.Flowable;
2121

2222
public class MultiModalConversationUsage {
23-
private static final String modelName = "qwen-vl-chat-v1";
23+
private static final String modelName = "qwen-vl-max-latest";
2424

2525
public static void simpleMultiModalConversationCall() throws ApiException, NoApiKeyException, UploadFileException {
2626
MultiModalConversation conv = new MultiModalConversation();
@@ -35,9 +35,17 @@ public static void simpleMultiModalConversationCall() throws ApiException, NoApi
3535
.content(Arrays.asList(userImage, userText)).build();
3636
MultiModalConversationParam param = MultiModalConversationParam.builder()
3737
.model(MultiModalConversationUsage.modelName).message(systemMessage)
38+
.vlHighResolutionImages(true)
39+
.vlEnableImageHwOutput(true)
40+
// .incrementalOutput(true)
3841
.message(userMessage).build();
3942
MultiModalConversationResult result = conv.call(param);
40-
System.out.print(result);
43+
System.out.println(result);
44+
System.out.print(JsonUtils.toJson(result));
45+
// Flowable<MultiModalConversationResult> results = conv.streamCall(param);
46+
// results.blockingForEach(result -> {
47+
// System.out.println(JsonUtils.toJson(result));
48+
// });
4149
}
4250

4351
public static void MultiRoundConversationCall() throws ApiException, NoApiKeyException, UploadFileException {
@@ -59,6 +67,7 @@ public static void MultiRoundConversationCall() throws ApiException, NoApiKeyExc
5967
.build();
6068
MultiModalConversationResult result = conv.call(param);
6169
System.out.println(result);
70+
System.out.println(JsonUtils.toJson(result));
6271
MultiModalMessage resultMessage = result.getOutput().getChoices().get(0).getMessage();
6372
MultiModalMessageItemText assistentText = new MultiModalMessageItemText(
6473
(String) resultMessage.getContent().get(0).get("text"));
@@ -70,7 +79,8 @@ public static void MultiRoundConversationCall() throws ApiException, NoApiKeyExc
7079
.content(Arrays.asList(userText)).build());
7180
param.setMessages((List) messages);
7281
result = conv.call(param);
73-
System.out.print(result);
82+
System.out.println(result);
83+
System.out.println(JsonUtils.toJson(result));
7484
}
7585

7686
public static void textInTextStreamOut() throws ApiException, NoApiKeyException, UploadFileException, IOException {
@@ -273,14 +283,14 @@ public static void videoInTextAudioStreamOut() throws ApiException, NoApiKeyExce
273283

274284
public static void main(String[] args) {
275285
try {
276-
// simpleMultiModalConversationCall();
286+
simpleMultiModalConversationCall();
277287
// MultiRoundConversationCall();
278288
// textInAudioStreamOut();
279-
textInTextStreamOut();
289+
// textInTextStreamOut();
280290
// audioInTextAudioStreamOut();
281291
// imageInTextAudioStreamOut();
282292
// videoInTextAudioStreamOut();
283-
} catch (ApiException | NoApiKeyException | UploadFileException | IOException e) {
293+
} catch (ApiException | NoApiKeyException | UploadFileException /*| IOException*/ e) {
284294
System.out.println(e.getMessage());
285295
}
286296
System.exit(0);

src/main/java/com/alibaba/dashscope/aigc/multimodalconversation/MultiModalConversationParam.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public class MultiModalConversationParam extends HalfDuplexServiceParam {
122122
/** enable parallel tool calls */
123123
protected Boolean parallelToolCalls;
124124

125+
/** enable vl_high_resolution_images */
126+
protected Boolean vlHighResolutionImages;
127+
128+
/** enable vl_enable_image_hw_output */
129+
protected Boolean vlEnableImageHwOutput;
130+
125131
@Override
126132
public JsonObject getHttpBody() {
127133
JsonObject requestObject = new JsonObject();
@@ -219,6 +225,14 @@ public Map<String, Object> getParameters() {
219225
params.put("parallel_tool_calls", parallelToolCalls);
220226
}
221227

228+
if (vlHighResolutionImages != null) {
229+
params.put("vl_high_resolution_images", vlHighResolutionImages);
230+
}
231+
232+
if (vlEnableImageHwOutput != null) {
233+
params.put("vl_enable_image_hw_output", vlEnableImageHwOutput);
234+
}
235+
222236
params.putAll(parameters);
223237
return params;
224238
}

src/main/java/com/alibaba/dashscope/common/MultiModalMessageAdapter.java

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,16 @@ private void writeMapObject(JsonWriter out, Map<String, Object> mapObject) throw
2424
out.beginObject();
2525
for (Map.Entry<String, Object> entry : mapObject.entrySet()) {
2626
out.name(entry.getKey());
27-
if (entry.getValue() instanceof String) {
28-
out.value((String) entry.getValue());
29-
} else if (entry.getValue() instanceof Integer) {
30-
out.value((Integer) (entry.getValue()));
31-
} else if (entry.getValue() instanceof Double) {
32-
out.value((Double) (entry.getValue()));
33-
} else if (entry.getValue() instanceof Boolean) {
34-
out.value((Boolean) (entry.getValue()));
35-
} else if (entry.getValue() instanceof Character) {
36-
out.value((Character) (entry.getValue()));
37-
} else if (entry.getValue() instanceof List) {
38-
out.beginArray();
39-
for (Object v : (List) entry.getValue()) {
40-
writePrimitiveType(out, v);
41-
}
42-
out.endArray();
43-
} else {
44-
writeMapObject(out, (Map<String, Object>) entry.getValue());
45-
}
27+
writeValue(out, entry.getValue());
4628
}
4729
out.endObject();
4830
}
4931
}
5032

51-
private void writePrimitiveType(JsonWriter out, Object value) throws IOException {
52-
if (value instanceof String) {
33+
private void writeValue(JsonWriter out, Object value) throws IOException {
34+
if (value == null) {
35+
out.nullValue();
36+
} else if (value instanceof String) {
5337
out.value((String) value);
5438
} else if (value instanceof Integer) {
5539
out.value((Integer) value);
@@ -59,6 +43,18 @@ private void writePrimitiveType(JsonWriter out, Object value) throws IOException
5943
out.value((Boolean) value);
6044
} else if (value instanceof Character) {
6145
out.value((Character) value);
46+
} else if (value instanceof List) {
47+
out.beginArray();
48+
List<?> list = (List<?>) value;
49+
for (Object item : list) {
50+
writeValue(out, item);
51+
}
52+
out.endArray();
53+
} else if (value instanceof Map) {
54+
writeMapObject(out, (Map<String, Object>) value);
55+
} else {
56+
// Fallback for other types
57+
out.value(value.toString());
6258
}
6359
}
6460
private void writeToolCallBase(JsonWriter writer, ToolCallBase toolCallBase) throws IOException {
@@ -128,6 +124,16 @@ public void write(JsonWriter out, MultiModalMessage value) throws IOException {
128124
out.endArray();
129125
}
130126

127+
if (value.getToolCallId() != null) {
128+
out.name(ApiKeywords.TOOL_CALL_ID);
129+
out.value(value.getToolCallId());
130+
}
131+
132+
if (value.getName() != null) {
133+
out.name(ApiKeywords.NAME);
134+
out.value(value.getName());
135+
}
136+
131137
out.endObject();
132138
}
133139

@@ -163,6 +169,18 @@ public MultiModalMessage read(JsonReader in) throws IOException {
163169
objectMap.remove(ApiKeywords.TOOL_CALLS);
164170
}
165171

172+
if (objectMap.containsKey(ApiKeywords.TOOL_CALL_ID)) {
173+
String toolCallId = (String) objectMap.get(ApiKeywords.TOOL_CALL_ID);
174+
msg.setToolCallId(toolCallId);
175+
objectMap.remove(ApiKeywords.TOOL_CALL_ID);
176+
}
177+
178+
if (objectMap.containsKey(ApiKeywords.NAME)) {
179+
String name = (String) objectMap.get(ApiKeywords.NAME);
180+
msg.setName(name);
181+
objectMap.remove(ApiKeywords.NAME);
182+
}
183+
166184
return msg;
167185
}
168186
}

0 commit comments

Comments
 (0)