|
| 1 | +// Copyright (c) Alibaba, Inc. and its affiliates. |
| 2 | +package com.alibaba.dashscope; |
| 3 | + |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import com.alibaba.dashscope.common.MultiModalMessage; |
| 8 | +import com.alibaba.dashscope.utils.JsonUtils; |
| 9 | +import com.google.gson.JsonArray; |
| 10 | +import com.google.gson.JsonObject; |
| 11 | +import java.math.BigDecimal; |
| 12 | +import java.math.BigInteger; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +public class TestMultiModalMessageAdapter { |
| 20 | + |
| 21 | + private static class CustomContent { |
| 22 | + public String url = "http://example.com/a.png"; |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testBigDecimalAndBigIntegerViaToJsonTree() { |
| 27 | + Map<String, Object> videoContent = new HashMap<>(); |
| 28 | + videoContent.put("video", Arrays.asList("base64img1", "base64img2")); |
| 29 | + videoContent.put("fps", new BigDecimal("0.1")); |
| 30 | + videoContent.put("nframes", new BigInteger("12345678901234567890")); |
| 31 | + |
| 32 | + MultiModalMessage msg = |
| 33 | + MultiModalMessage.builder() |
| 34 | + .role("user") |
| 35 | + .content( |
| 36 | + Arrays.asList( |
| 37 | + Collections.singletonMap("text", (Object) "describe this video"), videoContent)) |
| 38 | + .build(); |
| 39 | + |
| 40 | + // MultiModalConversationParam.getInput() serializes through Gson.toJsonTree, |
| 41 | + // whose JsonTreeWriter does not support jsonValue() |
| 42 | + JsonArray array = JsonUtils.toJsonArray(Arrays.asList(msg)); |
| 43 | + JsonObject video = |
| 44 | + array.get(0).getAsJsonObject().getAsJsonArray("content").get(1).getAsJsonObject(); |
| 45 | + assertTrue(video.get("fps").isJsonPrimitive()); |
| 46 | + assertTrue(video.get("fps").getAsJsonPrimitive().isNumber()); |
| 47 | + assertEquals(new BigDecimal("0.1"), video.get("fps").getAsBigDecimal()); |
| 48 | + assertEquals(new BigInteger("12345678901234567890"), video.get("nframes").getAsBigInteger()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testUnsupportedTypeFallbackViaToJsonTree() { |
| 53 | + Map<String, Object> content = new HashMap<>(); |
| 54 | + content.put("custom", new CustomContent()); |
| 55 | + |
| 56 | + MultiModalMessage msg = |
| 57 | + MultiModalMessage.builder().role("user").content(Arrays.asList(content)).build(); |
| 58 | + |
| 59 | + JsonArray array = JsonUtils.toJsonArray(Arrays.asList(msg)); |
| 60 | + JsonObject custom = |
| 61 | + array |
| 62 | + .get(0) |
| 63 | + .getAsJsonObject() |
| 64 | + .getAsJsonArray("content") |
| 65 | + .get(0) |
| 66 | + .getAsJsonObject() |
| 67 | + .getAsJsonObject("custom"); |
| 68 | + assertEquals("http://example.com/a.png", custom.get("url").getAsString()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testStreamingWriterPathStillWorks() { |
| 73 | + Map<String, Object> content = new HashMap<>(); |
| 74 | + content.put("fps", new BigDecimal("0.1")); |
| 75 | + content.put("custom", new CustomContent()); |
| 76 | + |
| 77 | + MultiModalMessage msg = |
| 78 | + MultiModalMessage.builder().role("user").content(Arrays.asList(content)).build(); |
| 79 | + |
| 80 | + String json = JsonUtils.toJson(msg); |
| 81 | + JsonObject parsed = JsonUtils.parse(json); |
| 82 | + JsonObject first = parsed.getAsJsonArray("content").get(0).getAsJsonObject(); |
| 83 | + assertEquals(new BigDecimal("0.1"), first.get("fps").getAsBigDecimal()); |
| 84 | + assertEquals( |
| 85 | + "http://example.com/a.png", first.getAsJsonObject("custom").get("url").getAsString()); |
| 86 | + } |
| 87 | +} |
0 commit comments