Commit 977ef3d8 authored by 陈立彬's avatar 陈立彬

ai问答接入deepseek

parent c8dc39c8
......@@ -538,20 +538,20 @@ public class AppChatCompletionService {
// 获取随机问题
final List<HotQaMobileDto> hots = Lists.newArrayList();
PageResult<HotQaMobileDto> pageResult = this.hotQaList(new QaAssistantRequestDto());
if(Objects.nonNull(pageResult) && CollectionUtil.isNotEmpty(pageResult.getItems())) {
List<HotQaMobileDto> items = pageResult.getItems();
if(items.size() > 2){
Collections.shuffle(items);
items = items.subList(0, 2);
}
hots.addAll(items);
}
// PageResult<HotQaMobileDto> pageResult = this.hotQaList(new QaAssistantRequestDto());
// if(Objects.nonNull(pageResult) && CollectionUtil.isNotEmpty(pageResult.getItems())) {
// List<HotQaMobileDto> items = pageResult.getItems();
// if(items.size() > 2){
// Collections.shuffle(items);
// items = items.subList(0, 2);
// }
// hots.addAll(items);
// }
List<HotQaResponseModel> hotQaList = chatCompletionService.hotQaList(new HotQaRequestModel());
if(CollectionUtil.isNotEmpty(hotQaList)) {
Collections.shuffle(hotQaList);
if(hotQaList.size() > 2) {
hotQaList = hotQaList.subList(0, 2);
if(hotQaList.size() > 5) {
hotQaList = hotQaList.subList(0, 5);
}
hotQaList.forEach(v -> {
HotQaMobileDto dto = new HotQaMobileDto();
......@@ -566,35 +566,64 @@ public class AppChatCompletionService {
reqBody.put("visitor_biz_id", userPrincipal.getUserId());
reqBody.put("session_id", sessionId);
final StringBuffer buffer = new StringBuffer();
// JSONObject customVariables = new JSONObject();
// customVariables.put("Position", userPrincipal.getPosition());
// customVariables.put("Department", userPrincipal.getShopName());
// customVariables.put("UserId", userPrincipal.getUserId());
// reqBody.put("custom_variables", customVariables.toJSONString());
final String[] thoughtContent = {""};
final String[] replyContent = {""};
final String[] difySessionId = {""};
final Boolean[] replyFlag = {false};
String finalSessionId = sessionId;
return webClient.post().uri(dsApiBase).accept(MediaType.TEXT_EVENT_STREAM).bodyValue(reqBody.toJSONString()).exchangeToFlux(r -> r.bodyToFlux(String.class))
.mapNotNull(v -> {
JSONObject json = JSONObject.parseObject(v);
String type = json.getString("type");
if(Objects.equals(type, "error")) {
thoughtContent[0] = json.getJSONObject("error").getString("message");
}
JSONObject payload = json.getJSONObject("payload");
if(Objects.nonNull(payload)) {
if(Objects.equals(type, "thought")) {
JSONArray procedures = payload.getJSONArray("procedures");
if(CollectionUtil.isNotEmpty(procedures) && procedures.size() > 1) {
String answer = procedures.getJSONObject(1).getJSONObject("debugging").getString("display_content");
if(StringUtils.isNotEmpty(answer)) {
replyContent[0] = answer;
if(CollectionUtil.isNotEmpty(procedures)) {
String content = procedures.getJSONObject(0).getJSONObject("debugging").getString("content");
if(StrUtil.isNotEmpty(content)) {
thoughtContent[0] = "## 大模型正在思考中 ##\n";
thoughtContent[0] += content;
}
}
if(ObjectUtil.isEmpty(difySessionId[0]) && ObjectUtil.isNotEmpty(payload.getString("session_id"))) {
difySessionId[0] = payload.getString("session_id");
}
if(Objects.equals(type, "reply") && Objects.equals(payload.getBoolean("is_from_self"), false)) {
if(Objects.nonNull(payload)) {
String content = payload.getString("content");
if(StrUtil.isNotEmpty(content)) {
replyContent[0] = "\n\n## 以下是大模型回复 ##\n";
replyContent[0] += content;
replyFlag[0] = true;
}
if(ObjectUtil.isEmpty(difySessionId[0]) && ObjectUtil.isNotEmpty(payload.getString("session_id"))) {
difySessionId[0] = payload.getString("session_id");
}
}
}
String finalContent = thoughtContent[0];
if(replyFlag[0]) {
finalContent += replyContent[0];
}
UserAskResultMobileDto result = new UserAskResultMobileDto();
result.setReplyContent(replyContent[0]);
result.setReplyContent(finalContent);
result.setChatCompletionId(chatCompletionId);
result.setMessageId(userQaRecordId);
result.setHots(hots);
return result;
}).doOnComplete(
() -> {
String finalContent = thoughtContent[0] + replyContent[0];
// 更新DIFY会话ID
if(StrUtil.isEmpty(finalSessionId)) {
UserChatCompletionSaveModel updateModel = new UserChatCompletionSaveModel();
......@@ -603,7 +632,7 @@ public class AppChatCompletionService {
chatCompletionService.saveUserQaSession(updateModel);
}
// 保存AI问答详情
chatCompletionService.saveUserQaRecord(chatCompletionId, 1, userQaRecordId, replyContent[0], request.getAssistantId());
chatCompletionService.saveUserQaRecord(chatCompletionId, 1, userQaRecordId, finalContent, request.getAssistantId());
}
).map(v -> ServerSentEvent.builder(v).build());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment