修复问题:线上ai助手后端内容一次性返回,消息不显示

dev
JEECG 4 months ago
parent 86cfc18fe8
commit 81b4cbbe35

@ -17,6 +17,7 @@ import java.util.Objects;
import java.util.function.Consumer; import java.util.function.Consumer;
//update-begin---author:chenrui ---date:20240126 for【QQYUN-7932】AI助手------------ //update-begin---author:chenrui ---date:20240126 for【QQYUN-7932】AI助手------------
/** /**
* OpenAISSE * OpenAISSE
* @author chenrui * @author chenrui
@ -102,6 +103,11 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
try { try {
//update-begin---author:chenrui ---date:20250207 for[QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持think标签------------ //update-begin---author:chenrui ---date:20250207 for[QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持think标签------------
// 兼容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(); Message delta = completionResponse.getChoices().get(0).getDelta();
if (null != delta) { if (null != delta) {
String content = delta.getContent(); String content = delta.getContent();
@ -124,18 +130,17 @@ public class OpenAISSEEventSourceListener extends EventSourceListener {
// 响应消息体不记录思考过程 // 响应消息体不记录思考过程
messageContent += null == content ? "" : content; messageContent += null == content ? "" : content;
} }
log.info("ai-chat返回数据,发送给前端:" + content); log.info("ai-chat返回数据,发送给前端:" + content);
sseEmitter.send(SseEmitter.event() sseEmitter.send(SseEmitter.event()
.id(this.topicId) .id(this.topicId)
.data(delta) .data(delta)
.reconnectTime(3000)); .reconnectTime(3000));
} }
}
//update-end---author:chenrui ---date:20250207 for[QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持think标签------------ //update-end---author:chenrui ---date:20250207 for[QQYUN-11102/QQYUN-11109]兼容deepseek模型,支持think标签------------
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage(),e); log.error("ai-chat返回数据,发生异常"+e.getMessage(),e);
sseEmitter.completeWithError(e);
eventSource.cancel(); eventSource.cancel();
} }
} }

@ -104,6 +104,7 @@ public class ChatServiceImpl implements ChatService {
//超时回调 //超时回调
sseEmitter.onTimeout(() -> { sseEmitter.onTimeout(() -> {
log.info("[{}]连接超时...................", uid); log.info("[{}]连接超时...................", uid);
LocalCache.CACHE.remove(uid);
}); });
//异常回调 //异常回调
sseEmitter.onError( sseEmitter.onError(
@ -115,7 +116,7 @@ public class ChatServiceImpl implements ChatService {
.name("发生异常!") .name("发生异常!")
.data(Message.builder().content("发生异常请重试!").build()) .data(Message.builder().content("发生异常请重试!").build())
.reconnectTime(3000)); .reconnectTime(3000));
LocalCache.CACHE.put(uid, sseEmitter); LocalCache.CACHE.remove(uid);
} catch (IOException e) { } catch (IOException e) {
log.error(e.getMessage(),e); log.error(e.getMessage(),e);
} }
@ -179,6 +180,7 @@ public class ChatServiceImpl implements ChatService {
finalMsgHistory.add(tempMessage); finalMsgHistory.add(tempMessage);
redisTemplate.opsForHash().put(cacheKey, CACHE_KEY_MSG_CONTEXT, JSONUtil.toJsonStr(finalMsgHistory)); redisTemplate.opsForHash().put(cacheKey, CACHE_KEY_MSG_CONTEXT, JSONUtil.toJsonStr(finalMsgHistory));
}); });
log.info("话题:{},开始发送消息~~~", topicId);
ChatCompletion completion = ChatCompletion ChatCompletion completion = ChatCompletion
.builder() .builder()
.messages(msgHistory) .messages(msgHistory)

@ -153,6 +153,7 @@
import presetQuestion from './presetQuestion.vue'; import presetQuestion from './presetQuestion.vue';
import { DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue'; import { DeleteOutlined, ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { message, Modal, Tabs } from 'ant-design-vue'; import { message, Modal, Tabs } from 'ant-design-vue';
import { isObject, isString } from '/@/utils/is';
import '../style/github-markdown.less'; import '../style/github-markdown.less';
import '../style/highlight.less'; import '../style/highlight.less';
import '../style/style.less'; import '../style/style.less';
@ -250,7 +251,8 @@
// //
evtSource.onmessage = function (e) { evtSource.onmessage = function (e) {
const data = e.data; const data = e.data;
// console.log(e); let delay = 0;
setTimeout(() => {
if (data === '[DONE]') { if (data === '[DONE]') {
updateChatSome(uuid, props.chatData.length - 1, { loading: false }); updateChatSome(uuid, props.chatData.length - 1, { loading: false });
scrollToBottom(); scrollToBottom();
@ -273,17 +275,22 @@
}); });
scrollToBottom(); scrollToBottom();
} else { } else {
updateChatSome(uuid.value, props.chatData.length - 1, { loading: false }); // updateChatSome(uuid.value, props.chatData.length - 1, { loading: false });
scrollToBottom(); // scrollToBottom();
handleStop(); // 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 }); updateChatSome(uuid.value, props.chatData.length - 1, { loading: false });
scrollToBottom(); scrollToBottom();
handleStop(); handleStop();
evtSource.close(); // evtSource.close(); //
} }
} }
}, delay);
}; };
// //
evtSource.onerror = function (e) { evtSource.onerror = function (e) {
@ -347,6 +354,7 @@
}; };
// //
const handleStop = () => { const handleStop = () => {
console.log('ai 聊天:::---停止响应');
if (loading.value) { if (loading.value) {
loading.value = false; loading.value = false;
} }

Loading…
Cancel
Save