You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package cn.teammodel.service.impl;
import cn.teammodel.ai.deepseek.DeepSeekClient;
import cn.teammodel.common.PK;
import cn.teammodel.model.dto.ai.deepseek.ChatRequestOKHttpDto;
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
import cn.teammodel.model.dto.ai.deepseek.ChatReqDto;
import cn.teammodel.model.entity.ai.DeepSeekSession;
import cn.teammodel.model.entity.ai.DeepSeekSession.DeepSeekMessage;
import cn.teammodel.repository.DeepSeekRepository;
import cn.teammodel.security.utils.SecurityUtil;
import cn.teammodel.service.DeepSeekService;
import cn.teammodel.service.DeepSeekSessionService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.*;
/**
* 描述访问DeepSeek方法
*/
@Service
public class DeepSeekServiceImpl implements DeepSeekService {
@Resource
private DeepSeekSessionService deepSeekService;
@Resource
private DeepSeekRepository deepSeekRepository;
/**
* 提问
* @param message
* @return
*/
@Override
public ChatResponseDto ChatAsk(ChatReqDto message) {
//创建消息列表
List<ChatReqDto> msg = new ArrayList<>();
msg.add(message);
//构建请求头
ChatRequestOKHttpDto requestBody = new ChatRequestOKHttpDto();
requestBody.setModel(DeepSeekClient.API_Model);
requestBody.setMessages(msg);
requestBody.setTemperature(0);
requestBody.setMax_tokens(1024);
//开始时间
long startTime = System.currentTimeMillis();
//发起请求
ChatResponseDto response = DeepSeekClient.SendRequests(requestBody);
//Map<String, Object> response = DeepSeekClient.SendRequests(requestBody);
//Map<String, Object> response = SendRequest(requestBody);
//结束时间
long endTime = System.currentTimeMillis();
//思考耗时 秒
response.setWasteTime((endTime-startTime)/1000);
if (response.getCode() == 200){
DeepSeekMessage savaMessage = new DeepSeekMessage();
savaMessage.setId(response.getId());
savaMessage.setUserText(message.getContent());
savaMessage.setAiText(response.getChoices().get(0).getMessage().getContent());
savaMessage.setRole(response.getChoices().get(0).getMessage().getRole());
savaMessage.setCreateTime(response.getCreated());
savaMessage.setFinish_reason(response.getChoices().get(0).getFinish_reason());
DeepSeekSession session = new DeepSeekSession();
if (message.getSessionId() != null){
session = deepSeekRepository.findSessionByIdAndCode(message.getSessionId(), PK.CHAT_SESSION);
UpdateSession(message, session, savaMessage, response);
}else {
UpdateSession(message, session, savaMessage, response);
}
}
return response;
}
//region 辅助方法
/**
* 新增/更新会话
* @param message
* @param session
* @param savaMessage
* @param response
*/
private void UpdateSession(ChatReqDto message, DeepSeekSession session, DeepSeekMessage savaMessage, ChatResponseDto response) {
if (session.getId() == null){
List<DeepSeekMessage> history = Collections.singletonList(savaMessage);
String userId = SecurityUtil.getLoginUser().getId();
session.setId(UUID.randomUUID().toString());
session.setCode(PK.DEEPSEEK_SESSION);
session.setTitle("新对话");
session.setUserId(userId);
session.setModel(response.getModel());
session.setCreateTime(Instant.now().toEpochMilli());
session.setUpdateTime(Instant.now().toEpochMilli());
session.setHistory(history);
session = deepSeekRepository.save(session);
}else {
session.getHistory().add(savaMessage);
deepSeekService.updateSession(session, message.getSessionId());
}
}
//endregion
}