mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-09 02:46:50 +08:00
ai对话错误信息处理
This commit is contained in:
@@ -106,8 +106,18 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
@Override
|
||||
@Transactional
|
||||
public SseEmitter chat(CaseAiChatDto caseAiChatDto, CurrentUser currentUser) {
|
||||
// 创建SSE响应器
|
||||
SseEmitter sseEmitter = new SseEmitter();
|
||||
// 1. 获取conversationId
|
||||
String conversationId = getOrCreateConversationId(caseAiChatDto, currentUser);
|
||||
String conversationId;
|
||||
try {
|
||||
conversationId = getOrCreateConversationId(caseAiChatDto, currentUser);
|
||||
} catch (Exception e) {
|
||||
log.error("获取会话ID失败", e);
|
||||
errMessage(sseEmitter, "服务繁忙,请稍后再试。");
|
||||
sseEmitter.complete();
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
// 2. 查询历史
|
||||
List<CaseAiMessageVo> historyMessages = elasticSearchIndexService.queryData(conversationId);
|
||||
@@ -141,7 +151,15 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
log.info("案例问答接口请求参数: [{}]", chatParamStr);
|
||||
|
||||
// 4. 设置请求头
|
||||
String accessToken = aiAccessTokenService.getAccessToken();
|
||||
String accessToken;
|
||||
try {
|
||||
accessToken = aiAccessTokenService.getAccessToken();
|
||||
} catch (Exception e) {
|
||||
log.error("获取access_token失败", e);
|
||||
errMessage(sseEmitter, "服务繁忙,请稍后再试。");
|
||||
sseEmitter.complete();
|
||||
return sseEmitter;
|
||||
}
|
||||
String apiCode = caseAiProperties.getChatApiCode();
|
||||
Request.Builder builder = new Request.Builder();
|
||||
builder.url(caseAiProperties.getBaseUrl() + "/apigateway/chat/knowledge/v1/chat/completions");
|
||||
@@ -151,8 +169,6 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
builder.post(bodyRequestBody);
|
||||
Request request = builder.build();
|
||||
|
||||
// 5. 创建SSE响应器
|
||||
SseEmitter sseEmitter = new SseEmitter();
|
||||
|
||||
// 6. 用于收集对话数据的容器
|
||||
AiChatConversationData conversationData = new AiChatConversationData();
|
||||
@@ -188,8 +204,7 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
|
||||
if (status != null) {
|
||||
CaseAiChatStatusEnum statusEnum = CaseAiChatStatusEnum.getByCode(status);
|
||||
switch (statusEnum) {
|
||||
case REFERS: // 返回引用文件
|
||||
if (statusEnum == CaseAiChatStatusEnum.REFERS) { // 返回引用文件
|
||||
// 处理文件引用并构建返给前端的数据
|
||||
JSONObject modifiedData = handleFileReferAndBuildResponse(responseData, conversationData);
|
||||
if (modifiedData != null) {
|
||||
@@ -197,21 +212,20 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
sseEmitter.send(modifiedData.toJSONString());
|
||||
return; // 早期返回,不发送原始数据
|
||||
}
|
||||
break;
|
||||
case CHAT: // 流式对话中
|
||||
} else if (statusEnum == CaseAiChatStatusEnum.CHAT) { // 流式对话中
|
||||
String content = responseData.getString("content");
|
||||
if (content != null) {
|
||||
conversationData.appendAnswer(content);
|
||||
}
|
||||
break;
|
||||
case SUGGESTIONS: // 返回建议
|
||||
} else if (statusEnum == CaseAiChatStatusEnum.SUGGESTIONS) { // 返回建议
|
||||
handleSuggestions(responseData, conversationData);
|
||||
break;
|
||||
case CHAT_COMPLETED:
|
||||
case API_COMPLETED: // 接口交互完成
|
||||
default:
|
||||
} else if (statusEnum == CaseAiChatStatusEnum.CHAT_COMPLETED || statusEnum == CaseAiChatStatusEnum.API_COMPLETED) { // 接口交互完成
|
||||
// 不做特殊处理
|
||||
break;
|
||||
} else {
|
||||
// 异常问题,取message内容
|
||||
String message = jsonData.getString("message");
|
||||
errMessage(sseEmitter, message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
sseEmitter.send(responseData.toJSONString());
|
||||
@@ -237,7 +251,7 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
// 如果是 content-type 错误,尝试作为普通 HTTP 请求处理
|
||||
if (e instanceof IllegalStateException && e.getMessage() != null && e.getMessage().contains("Invalid content-type")) {
|
||||
log.warn("服务器返回的 Content-Type 不是 text/event-stream,尝试作为普通 HTTP 请求处理");
|
||||
CaseAiChatServiceImpl.this.handleAsRegularHttpRequest(request, sseEmitter, conversationData);
|
||||
handleAsRegularHttpRequest(request, sseEmitter, conversationData);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -694,4 +708,16 @@ public class CaseAiChatServiceImpl implements ICaseAiChatService {
|
||||
sseEmitter.completeWithError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void errMessage(SseEmitter sseEmitter, String message) {
|
||||
JSONObject jsonData = new JSONObject();
|
||||
jsonData.put("status", 1);
|
||||
jsonData.put("content", message);
|
||||
try {
|
||||
sseEmitter.send(jsonData.toJSONString());
|
||||
} catch (IOException e) {
|
||||
log.error("发送错误信息异常", e);
|
||||
sseEmitter.completeWithError(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user