Skip to content

Commit 2aff504

Browse files
Merge pull request #16 from oldmanpushcart/develop-20240727
不兼容API修复
2 parents 3321458 + 8f7b001 commit 2aff504

File tree

7 files changed

+53
-34
lines changed

7 files changed

+53
-34
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
> 请注意:在使用 DashScope4j 时,你需要遵守灵积API的使用条款和条件。
99
1010
## 重要更新
11+
- **2.1.0:** 不兼容API修复
12+
- 修复模块中错误的exports,该修复会将模块中不应该暴露的内部api重新收回。本次修复不向下兼容。
13+
- 修复ChatRequest中函数调用因为Message序列化问题丢失PluginCall、Plugin、ToolCall、Tool等信息的BUG
1114
- **2.0.0:** 大版本重构。核心API进行不兼容调整和实现重构(请注意),删除1.x.x版本被标记为已废弃的方法
1215
- 重构拦截器接口和实现重构,并添加了流控、重试等拦截器实现
1316
- 调整部分类、API的位置和命名;删除已废弃的方法
@@ -69,7 +72,7 @@ final var request = ChatRequest.newBuilder()
6972
<dependency>
7073
<groupId>io.github.oldmanpushcart</groupId>
7174
<artifactId>dashscope4j</artifactId>
72-
<version>2.0.0</version>
75+
<version>2.1.0</version>
7376
</dependency>
7477
```
7578

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>io.github.oldmanpushcart</groupId>
55
<artifactId>dashscope4j</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.0.0</version>
7+
<version>2.1.0</version>
88
<name>dashscope4j ${project.version}</name>
99

1010
<description>Java library for DashScope</description>

src/main/java/io/github/oldmanpushcart/internal/dashscope4j/base/interceptor/spec/process/ProcessingMessageListForQwenLong.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.github.oldmanpushcart.dashscope4j.chat.ChatRequest;
88
import io.github.oldmanpushcart.dashscope4j.chat.message.Content;
99
import io.github.oldmanpushcart.dashscope4j.chat.message.Message;
10+
import io.github.oldmanpushcart.internal.dashscope4j.chat.message.MessageImpl;
1011
import io.github.oldmanpushcart.internal.dashscope4j.util.CommonUtils;
1112

1213
import java.net.URI;
@@ -42,7 +43,7 @@ public CompletableFuture<? extends List<Message>> process(InvocationContext cont
4243
}
4344

4445
// 新的 QwenLong 系统消息
45-
final var newQwenLongSystemMessage = new QwenLongSystemMessage(new ArrayList<>());
46+
final var newQwenLongSystemMessage = new QwenLongSystemMessageImpl(new ArrayList<>());
4647

4748
// 新的对话消息列表
4849
final var newMessages = new ArrayList<Message>();
@@ -54,7 +55,7 @@ public CompletableFuture<? extends List<Message>> process(InvocationContext cont
5455
* 如果消息列表中已经有了 QwenLong 系统消息,则合并到新的系统消息中。
5556
* 本条消息将不会加入到新的对话消息列表中,后边会再重建加回
5657
*/
57-
if (message instanceof QwenLongSystemMessage original) {
58+
if (message instanceof QwenLongSystemMessageImpl original) {
5859
newQwenLongSystemMessage.contents().addAll(original.contents());
5960
return;
6061
}
@@ -113,7 +114,7 @@ private static boolean isQwenLongSupportContent(Content<?> content) {
113114
if (content.type() != Content.Type.FILE || !(content.data() instanceof URI resource)) {
114115
return false;
115116
}
116-
117+
117118
final var schema = resource.getScheme();
118119

119120
/*
@@ -132,10 +133,10 @@ private static boolean isQwenLongSupportContent(Content<?> content) {
132133
* @param qwenLongSystemMessage QwenLong 模型的系统消息
133134
* @return 重建后的消息列表
134135
*/
135-
private static List<Message> rebuildMessageList(List<Message> messages, QwenLongSystemMessage qwenLongSystemMessage) {
136+
private static List<Message> rebuildMessageList(List<Message> messages, Message qwenLongSystemMessage) {
136137

137138
// 将 QwenLong 系统消息作为最后一个SystemMessage注入到聊天列表中
138-
if (!qwenLongSystemMessage.isEmpty()) {
139+
if (!qwenLongSystemMessage.contents().isEmpty()) {
139140

140141
// 找到最后一个系统消息
141142
int found = -1;
@@ -157,14 +158,11 @@ private static List<Message> rebuildMessageList(List<Message> messages, QwenLong
157158

158159
/**
159160
* 特殊系统消息
160-
*
161-
* @param contents 内容集合
162161
*/
163-
private record QwenLongSystemMessage(List<Content<?>> contents) implements Message {
162+
private static class QwenLongSystemMessageImpl extends MessageImpl {
164163

165-
@Override
166-
public Role role() {
167-
return Role.SYSTEM;
164+
public QwenLongSystemMessageImpl(List<Content<?>> contents) {
165+
super(Role.SYSTEM, contents);
168166
}
169167

170168
@Override
@@ -176,13 +174,6 @@ public String text() {
176174
.collect(Collectors.joining(","));
177175
}
178176

179-
/**
180-
* @return 是否为空
181-
*/
182-
boolean isEmpty() {
183-
return contents.isEmpty();
184-
}
185-
186177
}
187178

188179
}

src/main/java/io/github/oldmanpushcart/internal/dashscope4j/chat/ChatRequestImpl.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.oldmanpushcart.internal.dashscope4j.chat;
22

3+
import com.fasterxml.jackson.databind.node.ObjectNode;
34
import io.github.oldmanpushcart.dashscope4j.Option;
45
import io.github.oldmanpushcart.dashscope4j.chat.ChatModel;
56
import io.github.oldmanpushcart.dashscope4j.chat.ChatRequest;
@@ -68,14 +69,14 @@ protected Object input() {
6869

6970
// 聊天消息列表中是否包含File类型的内容
7071
final var hasFileContent = messages.stream()
71-
.flatMap(message-> message.contents().stream())
72+
.flatMap(message -> message.contents().stream())
7273
.anyMatch(content -> content.type() == Content.Type.FILE);
7374

7475
/*
7576
* PDFExtract插件比较特殊,
7677
* 他在有File类型的内容时,消息列表格式为为多模态格式,否则则为文本格式
7778
*/
78-
if(hasPdfExtractPlugin && hasFileContent) {
79+
if (hasPdfExtractPlugin && hasFileContent) {
7980
modeRef.set(ChatModel.Mode.MULTIMODAL);
8081
}
8182

@@ -85,14 +86,28 @@ protected Object input() {
8586
return new HashMap<>() {{
8687
put("messages", new ArrayList<>() {{
8788
for (final var message : messages) {
88-
add(new HashMap<>() {{
89-
put("role", message.role());
90-
if(modeRef.get() == ChatModel.Mode.TEXT) {
91-
put("content", message.text());
89+
90+
/*
91+
* 根据模式的不同,构造不同的消息列表格式
92+
* 之所以要如此繁琐的原因,是要保持Message无状态实现Json时根据ChatModel的不同而做出不同的序列化结果
93+
* 所以在此对message的序列化做精细化控制
94+
*
95+
* FIX BUG: 2.0.0 版本中此处有严重问题,会丢失PluginCall、Plugin、ToolCall、Tool等信息
96+
*/
97+
final var messageNode = JacksonUtils.toNode(message);
98+
if (messageNode instanceof ObjectNode node) {
99+
if (modeRef.get() == ChatModel.Mode.TEXT) {
100+
node.put("content", message.text());
92101
} else {
93-
put("content", message.contents());
102+
node.putPOJO("content", message.contents());
94103
}
95-
}});
104+
}
105+
106+
/*
107+
* 将JsonNode代替原来的Message参与接下来ChatRequest的序列化操作
108+
*/
109+
add(messageNode);
110+
96111
}
97112
}});
98113
}};

src/main/java/io/github/oldmanpushcart/internal/dashscope4j/chat/message/MessageImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.oldmanpushcart.internal.dashscope4j.chat.message;
22

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import io.github.oldmanpushcart.dashscope4j.chat.message.Content;
45
import io.github.oldmanpushcart.dashscope4j.chat.message.Message;
56

@@ -20,6 +21,7 @@ public MessageImpl(Role role, List<Content<?>> contents) {
2021
this.contents.addAll(contents);
2122
}
2223

24+
@JsonProperty("role")
2325
@Override
2426
public Message.Role role() {
2527
return role;

src/main/java/io/github/oldmanpushcart/internal/dashscope4j/util/JacksonUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public class JacksonUtils {
2525

2626
/**
2727
* 压缩Json字符串
28-
* @param json json
28+
*
29+
* @param json json
2930
* @return json
3031
*/
3132
public static String compact(String json) {
@@ -35,7 +36,7 @@ public static String compact(String json) {
3536
/**
3637
* {@code json -> node}
3738
*
38-
* @param json json
39+
* @param json json
3940
* @return node
4041
*/
4142
public static JsonNode toNode(String json) {
@@ -46,6 +47,16 @@ public static JsonNode toNode(String json) {
4647
}
4748
}
4849

50+
/**
51+
* {@code object -> node}
52+
*
53+
* @param object object
54+
* @return node
55+
*/
56+
public static JsonNode toNode(Object object) {
57+
return mapper.valueToTree(object);
58+
}
59+
4960
/**
5061
* {@code json -> T}
5162
*

src/main/java/module-info.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
requires com.fasterxml.jackson.core;
88
requires com.fasterxml.jackson.module.jsonSchema;
99

10-
1110
exports io.github.oldmanpushcart.dashscope4j;
12-
1311
exports io.github.oldmanpushcart.dashscope4j.base;
1412
exports io.github.oldmanpushcart.dashscope4j.base.algo;
1513
exports io.github.oldmanpushcart.dashscope4j.base.api;
@@ -18,8 +16,8 @@
1816
exports io.github.oldmanpushcart.dashscope4j.base.store;
1917
exports io.github.oldmanpushcart.dashscope4j.base.cache;
2018
exports io.github.oldmanpushcart.dashscope4j.base.interceptor;
21-
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.process;
2219
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.ratelimit;
20+
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.process;
2321
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.retry;
2422

2523
exports io.github.oldmanpushcart.dashscope4j.chat;
@@ -36,6 +34,5 @@
3634
exports io.github.oldmanpushcart.dashscope4j.embedding.mm;
3735

3836
exports io.github.oldmanpushcart.dashscope4j.util;
39-
exports io.github.oldmanpushcart.internal.dashscope4j.util;
4037

4138
}

0 commit comments

Comments
 (0)