Skip to content

Commit

Permalink
- **2.1.0:** 不兼容API修复
Browse files Browse the repository at this point in the history
  - 修复模块中错误的exports,该修复会将模块中不应该暴露的内部api重新收回。本次修复不向下兼容。
  - 修复ChatRequest中函数调用因为Message序列化问题丢失PluginCall、Plugin、ToolCall、Tool等信息的BUG
  • Loading branch information
oldmanpushcart committed Jul 27, 2024
1 parent 3321458 commit 8f7b001
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 34 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
> 请注意:在使用 DashScope4j 时,你需要遵守灵积API的使用条款和条件。
## 重要更新
- **2.1.0:** 不兼容API修复
- 修复模块中错误的exports,该修复会将模块中不应该暴露的内部api重新收回。本次修复不向下兼容。
- 修复ChatRequest中函数调用因为Message序列化问题丢失PluginCall、Plugin、ToolCall、Tool等信息的BUG
- **2.0.0:** 大版本重构。核心API进行不兼容调整和实现重构(请注意),删除1.x.x版本被标记为已废弃的方法
- 重构拦截器接口和实现重构,并添加了流控、重试等拦截器实现
- 调整部分类、API的位置和命名;删除已废弃的方法
Expand Down Expand Up @@ -69,7 +72,7 @@ final var request = ChatRequest.newBuilder()
<dependency>
<groupId>io.github.oldmanpushcart</groupId>
<artifactId>dashscope4j</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>io.github.oldmanpushcart</groupId>
<artifactId>dashscope4j</artifactId>
<packaging>jar</packaging>
<version>2.0.0</version>
<version>2.1.0</version>
<name>dashscope4j ${project.version}</name>

<description>Java library for DashScope</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.oldmanpushcart.dashscope4j.chat.ChatRequest;
import io.github.oldmanpushcart.dashscope4j.chat.message.Content;
import io.github.oldmanpushcart.dashscope4j.chat.message.Message;
import io.github.oldmanpushcart.internal.dashscope4j.chat.message.MessageImpl;
import io.github.oldmanpushcart.internal.dashscope4j.util.CommonUtils;

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

// 新的 QwenLong 系统消息
final var newQwenLongSystemMessage = new QwenLongSystemMessage(new ArrayList<>());
final var newQwenLongSystemMessage = new QwenLongSystemMessageImpl(new ArrayList<>());

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

final var schema = resource.getScheme();

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

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

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

/**
* 特殊系统消息
*
* @param contents 内容集合
*/
private record QwenLongSystemMessage(List<Content<?>> contents) implements Message {
private static class QwenLongSystemMessageImpl extends MessageImpl {

@Override
public Role role() {
return Role.SYSTEM;
public QwenLongSystemMessageImpl(List<Content<?>> contents) {
super(Role.SYSTEM, contents);
}

@Override
Expand All @@ -176,13 +174,6 @@ public String text() {
.collect(Collectors.joining(","));
}

/**
* @return 是否为空
*/
boolean isEmpty() {
return contents.isEmpty();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.oldmanpushcart.internal.dashscope4j.chat;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.oldmanpushcart.dashscope4j.Option;
import io.github.oldmanpushcart.dashscope4j.chat.ChatModel;
import io.github.oldmanpushcart.dashscope4j.chat.ChatRequest;
Expand Down Expand Up @@ -68,14 +69,14 @@ protected Object input() {

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

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

Expand All @@ -85,14 +86,28 @@ protected Object input() {
return new HashMap<>() {{
put("messages", new ArrayList<>() {{
for (final var message : messages) {
add(new HashMap<>() {{
put("role", message.role());
if(modeRef.get() == ChatModel.Mode.TEXT) {
put("content", message.text());

/*
* 根据模式的不同,构造不同的消息列表格式
* 之所以要如此繁琐的原因,是要保持Message无状态实现Json时根据ChatModel的不同而做出不同的序列化结果
* 所以在此对message的序列化做精细化控制
*
* FIX BUG: 2.0.0 版本中此处有严重问题,会丢失PluginCall、Plugin、ToolCall、Tool等信息
*/
final var messageNode = JacksonUtils.toNode(message);
if (messageNode instanceof ObjectNode node) {
if (modeRef.get() == ChatModel.Mode.TEXT) {
node.put("content", message.text());
} else {
put("content", message.contents());
node.putPOJO("content", message.contents());
}
}});
}

/*
* 将JsonNode代替原来的Message参与接下来ChatRequest的序列化操作
*/
add(messageNode);

}
}});
}};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.oldmanpushcart.internal.dashscope4j.chat.message;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.oldmanpushcart.dashscope4j.chat.message.Content;
import io.github.oldmanpushcart.dashscope4j.chat.message.Message;

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

@JsonProperty("role")
@Override
public Message.Role role() {
return role;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class JacksonUtils {

/**
* 压缩Json字符串
* @param json json
*
* @param json json
* @return json
*/
public static String compact(String json) {
Expand All @@ -35,7 +36,7 @@ public static String compact(String json) {
/**
* {@code json -> node}
*
* @param json json
* @param json json
* @return node
*/
public static JsonNode toNode(String json) {
Expand All @@ -46,6 +47,16 @@ public static JsonNode toNode(String json) {
}
}

/**
* {@code object -> node}
*
* @param object object
* @return node
*/
public static JsonNode toNode(Object object) {
return mapper.valueToTree(object);
}

/**
* {@code json -> T}
*
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.module.jsonSchema;


exports io.github.oldmanpushcart.dashscope4j;

exports io.github.oldmanpushcart.dashscope4j.base;
exports io.github.oldmanpushcart.dashscope4j.base.algo;
exports io.github.oldmanpushcart.dashscope4j.base.api;
Expand All @@ -18,8 +16,8 @@
exports io.github.oldmanpushcart.dashscope4j.base.store;
exports io.github.oldmanpushcart.dashscope4j.base.cache;
exports io.github.oldmanpushcart.dashscope4j.base.interceptor;
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.process;
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.ratelimit;
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.process;
exports io.github.oldmanpushcart.dashscope4j.base.interceptor.spec.retry;

exports io.github.oldmanpushcart.dashscope4j.chat;
Expand All @@ -36,6 +34,5 @@
exports io.github.oldmanpushcart.dashscope4j.embedding.mm;

exports io.github.oldmanpushcart.dashscope4j.util;
exports io.github.oldmanpushcart.internal.dashscope4j.util;

}

0 comments on commit 8f7b001

Please sign in to comment.