mirror of https://github.com/jeecgboot/jeecg-boot
修复问题:线上ai助手后端内容一次性返回,消息不显示
parent
86cfc18fe8
commit
81b4cbbe35
|
@ -17,6 +17,7 @@ import java.util.Objects;
|
|||
import java.util.function.Consumer;
|
||||
|
||||
//update-begin---author:chenrui ---date:20240126 for:【QQYUN-7932】AI助手------------
|
||||
|
||||
/**
|
||||
* OpenAI的SSE监听
|
||||
* @author chenrui
|
||||
|
@ -102,6 +103,11 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
|
|||
try {
|
||||
//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();
|
||||
|
@ -124,18 +130,17 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
|
|||
// 响应消息体不记录思考过程
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -104,6 +104,7 @@ public class ChatServiceImpl implements ChatService {
|
|||
//超时回调
|
||||
sseEmitter.onTimeout(() -> {
|
||||
log.info("[{}]连接超时...................", uid);
|
||||
LocalCache.CACHE.remove(uid);
|
||||
});
|
||||
//异常回调
|
||||
sseEmitter.onError(
|
||||
|
@ -115,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);
|
||||
}
|
||||
|
@ -179,6 +180,7 @@ public class ChatServiceImpl implements ChatService {
|
|||
finalMsgHistory.add(tempMessage);
|
||||
redisTemplate.opsForHash().put(cacheKey, CACHE_KEY_MSG_CONTEXT, JSONUtil.toJsonStr(finalMsgHistory));
|
||||
});
|
||||
log.info("话题:{},开始发送消息~~~", topicId);
|
||||
ChatCompletion completion = ChatCompletion
|
||||
.builder()
|
||||
.messages(msgHistory)
|
||||
|
|
|
@ -153,6 +153,7 @@
|
|||
import presetQuestion from './presetQuestion.vue';
|
||||
import { DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue';
|
||||
import { message, Modal, Tabs } from 'ant-design-vue';
|
||||
import { isObject, isString } from '/@/utils/is';
|
||||
import '../style/github-markdown.less';
|
||||
import '../style/highlight.less';
|
||||
import '../style/style.less';
|
||||
|
@ -250,7 +251,8 @@
|
|||
// 当从事件源接收到数据时触发
|
||||
evtSource.onmessage = function (e) {
|
||||
const data = e.data;
|
||||
// console.log(e);
|
||||
let delay = 0;
|
||||
setTimeout(() => {
|
||||
if (data === '[DONE]') {
|
||||
updateChatSome(uuid, props.chatData.length - 1, { loading: false });
|
||||
scrollToBottom();
|
||||
|
@ -273,17 +275,22 @@
|
|||
});
|
||||
scrollToBottom();
|
||||
} else {
|
||||
updateChatSome(uuid.value, props.chatData.length - 1, { loading: false });
|
||||
scrollToBottom();
|
||||
handleStop();
|
||||
// updateChatSome(uuid.value, props.chatData.length - 1, { loading: false });
|
||||
// scrollToBottom();
|
||||
// handleStop();
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log('ai 聊天:::', error);
|
||||
if (isObject(error) && isString(error.message) && error.message.endsWith('is not valid JSON')) {
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
updateChatSome(uuid.value, props.chatData.length - 1, { loading: false });
|
||||
scrollToBottom();
|
||||
handleStop();
|
||||
evtSource.close(); // 关闭连接
|
||||
}
|
||||
}
|
||||
}, delay);
|
||||
};
|
||||
// 与事件源的连接无法打开时触发
|
||||
evtSource.onerror = function (e) {
|
||||
|
@ -347,6 +354,7 @@
|
|||
};
|
||||
// 停止响应
|
||||
const handleStop = () => {
|
||||
console.log('ai 聊天:::---停止响应');
|
||||
if (loading.value) {
|
||||
loading.value = false;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue