版本合并,升级springboot3分支到3.7.3

This commit is contained in:
JEECG
2025-02-20 17:56:16 +08:00
230 changed files with 23438 additions and 12308 deletions

View File

@ -14,8 +14,10 @@ import org.jetbrains.annotations.NotNull;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.util.Objects;
import java.util.function.Consumer;
//update-begin---author:chenrui ---date:20240126 for【QQYUN-7932】AI助手------------
/**
* OpenAI的SSE监听
* @author chenrui
@ -29,22 +31,47 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
private SseEmitter sseEmitter;
private String topicId;
/**
* 回复消息内容
*/
private String messageContent = "";
/**
* 完成回复回调
*/
private Consumer<String> doneCallback;
/**
* 是否正在思考
*/
private boolean isThinking = false;
public OpenAISSEEventSourceListener(SseEmitter sseEmitter) {
this.sseEmitter = sseEmitter;
}
public OpenAISSEEventSourceListener(String topicId,SseEmitter sseEmitter){
public OpenAISSEEventSourceListener(String topicId, SseEmitter sseEmitter){
this.topicId = topicId;
this.sseEmitter = sseEmitter;
}
/**
* 设置消息完成响应时的回调
* for [QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持tink标签
* @param doneCallback
* @author chenrui
* @date 2025/2/7 18:14
*/
public void onDone(Consumer<String> doneCallback){
this.doneCallback = doneCallback;
}
/**
* {@inheritDoc}
*/
@Override
public void onOpen(@NotNull EventSource eventSource, @NotNull Response response) {
log.info("OpenAI建立sse连接...");
log.info("ai-chat建立sse连接...");
}
/**
@ -53,10 +80,12 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
@SneakyThrows
@Override
public void onEvent(@NotNull EventSource eventSource, String id, String type, @NotNull String data) {
log.debug("OpenAI返回数据:{}", data);
log.debug("ai-chat返回数据:{}", data);
tokens += 1;
if (data.equals("[DONE]")) {
log.info("OpenAI返回数据结束了");
log.info("ai-chat返回数据结束了");
this.doneCallback.accept(messageContent);
messageContent = "";
sseEmitter.send(SseEmitter.event()
.id("[TOKENS]")
.data("<br/><br/>tokens" + tokens())
@ -72,12 +101,46 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
ObjectMapper mapper = new ObjectMapper();
ChatCompletionResponse completionResponse = mapper.readValue(data, ChatCompletionResponse.class); // 读取Json
try {
sseEmitter.send(SseEmitter.event()
.id(this.topicId)
.data(completionResponse.getChoices().get(0).getDelta())
.reconnectTime(3000));
//update-begin---author:chenrui ---date:20250207 for[QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持think标签------------
// 兼容think标签
//update-begin---author:chenrui ---date:20250210 for判断空,防止反悔的内容为空报错.------------
if(null != completionResponse.getChoices()
&& !completionResponse.getChoices().isEmpty()
&& null != completionResponse.getChoices().get(0)) {
//update-end---author:chenrui ---date:20250210 for判断空,防止反悔的内容为空报错.------------
Message delta = completionResponse.getChoices().get(0).getDelta();
if (null != delta) {
String content = delta.getContent();
if ("<think>".equals(content)) {
isThinking = true;
content = "> ";
delta.setContent(content);
}
if ("</think>".equals(content)) {
isThinking = false;
content = "\n\n";
delta.setContent(content);
}
if (isThinking) {
if (null != content && content.contains("\n")) {
content = "\n> ";
delta.setContent(content);
}
} else {
// 响应消息体不记录思考过程
messageContent += null == content ? "" : content;
}
log.info("ai-chat返回数据,发送给前端:" + content);
sseEmitter.send(SseEmitter.event()
.id(this.topicId)
.data(delta)
.reconnectTime(3000));
}
}
//update-end---author:chenrui ---date:20250207 for[QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持think标签------------
} catch (Exception e) {
log.error(e.getMessage(),e);
log.error("ai-chat返回数据,发生异常"+e.getMessage(),e);
sseEmitter.completeWithError(e);
eventSource.cancel();
}
}
@ -86,7 +149,7 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
@Override
public void onClosed(@NotNull EventSource eventSource) {
log.info("流式输出返回值总共{}tokens", tokens() - 2);
log.info("OpenAI关闭sse连接...");
log.info("ai-chat关闭sse连接...");
}
@ -96,10 +159,10 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
String errMsg = "";
ResponseBody body = null == response ? null:response.body();
if (Objects.nonNull(body)) {
log.error("OpenAI sse连接异常data{},异常:{}", body.string(), t.getMessage());
log.error("ai-chat sse连接异常data{},异常:{}", body.string(), t.getMessage());
errMsg = body.string();
} else {
log.error("OpenAI sse连接异常data{},异常:{}", response, t.getMessage());
log.error("ai-chat sse连接异常data{},异常:{}", response, t.getMessage());
errMsg = t.getMessage();
}
eventSource.cancel();

View File

@ -9,6 +9,7 @@ import com.unfbx.chatgpt.entity.chat.Message;
import com.unfbx.chatgpt.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.chatgpt.prop.AiChatProperties;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
@ -58,6 +59,11 @@ public class ChatServiceImpl implements ChatService {
private OpenAiStreamClient openAiStreamClient = null;
/**
* ai聊天配置
* for [QQYUN-10943]【AI 重要】jeecg-boot-starter-chatgpt 支持deepseek等国产模型
*/
private AiChatProperties aiChatProperties;
//update-begin---author:chenrui ---date:20240131 for[QQYUN-8212]fix 没有配置启动报错------------
/**
@ -66,17 +72,17 @@ public class ChatServiceImpl implements ChatService {
* @author chenrui
* @date 2024/2/3 23:08
*/
private OpenAiStreamClient ensureClient(){
private void ensureClient(){
if (null == this.openAiStreamClient){
//update-begin---author:chenrui ---date:20240625 for[TV360X-1570]给于更友好的提示提示未配置ai------------
try {
this.openAiStreamClient = SpringContextUtils.getBean(OpenAiStreamClient.class);
this.aiChatProperties = SpringContextUtils.getBean(AiChatProperties.class);
} catch (Exception ignored) {
sendErrorMsg("如果您想使用AI助手请先设置相应配置!");
}
//update-end---author:chenrui ---date:20240625 for[TV360X-1570]给于更友好的提示提示未配置ai------------
}
return this.openAiStreamClient;
}
//update-end---author:chenrui ---date:20240131 for[QQYUN-8212]fix 没有配置启动报错------------
@ -98,6 +104,7 @@ public class ChatServiceImpl implements ChatService {
//超时回调
sseEmitter.onTimeout(() -> {
log.info("[{}]连接超时...................", uid);
LocalCache.CACHE.remove(uid);
});
//异常回调
sseEmitter.onError(
@ -109,7 +116,7 @@ public class ChatServiceImpl implements ChatService {
.name("发生异常!")
.data(Message.builder().content("发生异常请重试!").build())
.reconnectTime(3000));
LocalCache.CACHE.put(uid, sseEmitter);
LocalCache.CACHE.remove(uid);
} catch (IOException e) {
log.error(e.getMessage(),e);
}
@ -138,6 +145,7 @@ public class ChatServiceImpl implements ChatService {
@Override
public void sendMessage(String topicId, String message) {
ensureClient();
String uid = getUserId();
if (StrUtil.isBlank(message)) {
log.info("参数异常message为null");
@ -164,18 +172,22 @@ public class ChatServiceImpl implements ChatService {
throw new JeecgBootException("聊天消息推送失败uid:[{}],没有创建连接,请重试。~");
}
//update-begin---author:chenrui ---date:20240625 for[TV360X-1570]给于更友好的提示提示未配置ai------------
OpenAiStreamClient client = ensureClient();
if (null != client) {
if (null != openAiStreamClient) {
OpenAISSEEventSourceListener openAIEventSourceListener = new OpenAISSEEventSourceListener(topicId, sseEmitter);
List<Message> finalMsgHistory = msgHistory;
openAIEventSourceListener.onDone(respMessage -> {
Message tempMessage = Message.builder().content(respMessage).role(Message.Role.ASSISTANT).build();
finalMsgHistory.add(tempMessage);
redisTemplate.opsForHash().put(cacheKey, CACHE_KEY_MSG_CONTEXT, JSONUtil.toJsonStr(finalMsgHistory));
});
log.info("话题:{},开始发送消息~~~", topicId);
ChatCompletion completion = ChatCompletion
.builder()
.messages(msgHistory)
.model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
.model(aiChatProperties.getModel())
.build();
client.streamChatCompletion(completion, openAIEventSourceListener);
redisTemplate.opsForHash().put(cacheKey, CACHE_KEY_MSG_CONTEXT, JSONUtil.toJsonStr(msgHistory));
openAiStreamClient.streamChatCompletion(completion, openAIEventSourceListener);
//update-end---author:chenrui ---date:20240223 for[QQYUN-8225]聊天记录保存------------
Result.ok(completion.tokens());
}
//update-end---author:chenrui ---date:20240625 for[TV360X-1570]给于更友好的提示提示未配置ai------------
}