Commit c8dc39c8 authored by 陈立彬's avatar 陈立彬

ai问答接入deepseek

parent 4ac33135
......@@ -196,6 +196,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-lke</artifactId>
<version>3.1.1221</version>
</dependency>
</dependencies>
<build>
......
......@@ -14,12 +14,14 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.codec.ServerSentEvent;
......@@ -46,6 +48,18 @@ public class AppChatCompletionService {
@Value("${dify.chat_api_key}")
private String apiKey;
@Value("${deepseek.api_base}")
private String dsApiBase;
@Value("${deepseek.app_key}")
private String dsAppKey;
@Value("${deepseek.secret_id}")
private String dsSecretId;
@Value("${deepseek.app_id}")
private String dsAppId;
private final RestTemplate restTemplate = new RestTemplate();
private final ChatCompletionService chatCompletionService;
......@@ -484,6 +498,116 @@ public class AppChatCompletionService {
).map(v -> ServerSentEvent.builder(v).build());
}
/**
* 用户提问
* @param request
* @return
*/
public Flux<ServerSentEvent<UserAskResultMobileDto>> userAsk4Stream2(UserPrincipal userPrincipal, UserQaMobileRequestDto request) {
// 获取会话详情
UserQaRequestModel requestModel = new UserQaRequestModel();
requestModel.setChatCompletionId(request.getChatCompletionId());
requestModel.setUserId(userPrincipal.getUserId());
UserChatCompletionResponseModel ccModel = chatCompletionService.userQaDetail(requestModel);
String sessionId = "";
if(Objects.nonNull(ccModel) && StrUtil.isNotEmpty(ccModel.getSessionId())) {
sessionId = ccModel.getSessionId();
}
if(StringUtils.isEmpty(sessionId)) {
sessionId = UUID.randomUUID().toString();
}
// 更新会话信息
UserChatCompletionSaveModel saveModel = new UserChatCompletionSaveModel();
saveModel.setUserId(userPrincipal.getUserId());
saveModel.setUserName(userPrincipal.getUserName());
saveModel.setId(request.getChatCompletionId());
saveModel.setLastQuestion(request.getContent());
saveModel.setShopId(userPrincipal.getShopId());
saveModel.setShopName(userPrincipal.getShopName());
// 首次提问
if(Objects.isNull(request.getChatCompletionId()) || (Objects.nonNull(ccModel) && StrUtil.isEmpty(ccModel.getFirstQuestion()))) {
saveModel.setCreateTime(new Date());
saveModel.setFirstQuestion(request.getContent());
}
Integer chatCompletionId = chatCompletionService.saveUserQaSession(saveModel);
// 保存问答详情
Integer userQaRecordId = chatCompletionService.saveUserQaRecord(chatCompletionId, 0, null, request.getContent(), request.getAssistantId());
// 获取随机问题
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);
}
List<HotQaResponseModel> hotQaList = chatCompletionService.hotQaList(new HotQaRequestModel());
if(CollectionUtil.isNotEmpty(hotQaList)) {
Collections.shuffle(hotQaList);
if(hotQaList.size() > 2) {
hotQaList = hotQaList.subList(0, 2);
}
hotQaList.forEach(v -> {
HotQaMobileDto dto = new HotQaMobileDto();
dto.setQuestion(v.getQuestion());
hots.add(dto);
});
}
JSONObject reqBody = new JSONObject();
reqBody.put("content", request.getContent());
reqBody.put("bot_app_key", dsAppKey);
reqBody.put("visitor_biz_id", userPrincipal.getUserId());
reqBody.put("session_id", sessionId);
final StringBuffer buffer = new StringBuffer();
final String[] replyContent = {""};
final String[] difySessionId = {""};
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);
JSONObject payload = json.getJSONObject("payload");
if(Objects.nonNull(payload)) {
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(ObjectUtil.isEmpty(difySessionId[0]) && ObjectUtil.isNotEmpty(payload.getString("session_id"))) {
difySessionId[0] = payload.getString("session_id");
}
}
UserAskResultMobileDto result = new UserAskResultMobileDto();
result.setReplyContent(replyContent[0]);
result.setChatCompletionId(chatCompletionId);
result.setMessageId(userQaRecordId);
result.setHots(hots);
return result;
}).doOnComplete(
() -> {
// 更新DIFY会话ID
if(StrUtil.isEmpty(finalSessionId)) {
UserChatCompletionSaveModel updateModel = new UserChatCompletionSaveModel();
updateModel.setId(chatCompletionId);
updateModel.setSessionId(difySessionId[0]);
chatCompletionService.saveUserQaSession(updateModel);
}
// 保存AI问答详情
chatCompletionService.saveUserQaRecord(chatCompletionId, 1, userQaRecordId, replyContent[0], request.getAssistantId());
}
).map(v -> ServerSentEvent.builder(v).build());
}
/**
* 新会话
* @return
......
......@@ -60,7 +60,7 @@ public class ChatCompletionMobileController {
public Flux<ServerSentEvent<UserAskResultMobileDto>> askStream(@Parameter(hidden = true) UserPrincipal userPrincipal,
@RequestBody UserQaMobileRequestDto request) {
request.setContent(commonService.sentenceWordCorrect(request.getContent()));
return chatCompletionService.userAsk4Stream(userPrincipal, request);
return chatCompletionService.userAsk4Stream2(userPrincipal, request);
}
@Operation(summary = "切换助手")
......
......@@ -23,6 +23,11 @@ dify:
api_key: app-mJFu7K2wl3qEYsILkQBgRAKO
chat_api_base: https://ai-api.tech.breezeai.cn/v1
chat_api_key: app-ZpEcSEDYl3A8NoQJcR1dgYYg
deepseek:
app_id: 1304653544
secret_id: AKIDHMA2jAKD6dwzpPikWR5TbT5RWLtAAuNZ
app_key: imNDldBw
api_base: https://wss.lke.cloud.tencent.com/v1/qbot/chat/sse
---
spring:
config:
......
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