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.

111 lines
4.0 KiB

package cn.teammodel.service.impl;
import cn.teammodel.ai.deepseek.DeepSeekClient;
import cn.teammodel.common.PK;
import cn.teammodel.model.dto.ai.deepseek.ChatRequestDto;
import cn.teammodel.model.dto.ai.deepseek.ChatResponseDto;
import cn.teammodel.model.dto.ai.deepseek.MessageDto;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.*;
/**
* 访DeepSeek
*/
@Service
@Slf4j
public class DeepSeekServiceImpl implements DeepSeekService {
@Resource
private DeepSeekSessionService deepSeekService;
@Resource
private DeepSeekRepository deepSeekRepository;
/**
*
* @param message
* @return
*/
@Override
public ChatResponseDto ChatAsk(MessageDto message) {
//创建消息列表
List<MessageDto> msg = new ArrayList<>();
msg.add(message);
//构建请求头
ChatRequestDto requestBody = new ChatRequestDto();
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(MessageDto 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
}