add ai chat deepseek

This commit is contained in:
ran.shang
2025-07-07 10:51:42 +08:00
parent c6a46b62cc
commit cd3aaea259
10 changed files with 238 additions and 3 deletions

View File

@@ -0,0 +1,29 @@
package com.newyetai.sdk.deepseek.config;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = DeepSeekProperties.PREFIX)
@Data
@Component
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class DeepSeekProperties implements Serializable{
/** **/
private static final long serialVersionUID = 6362606996602121889L;
public static final String PREFIX = "deepseek";
/** 控制台获取http服务接口认证信息中的APIPassword **/
private String httpAPIPassword;
}

View File

@@ -0,0 +1,65 @@
package com.newyetai.sdk.deepseek.service;
import com.newyetai.common.AlitaResult;
import com.newyetai.common.util.JsonUtils;
import com.newyetai.sdk.common.constant.SdkConstants;
import com.newyetai.sdk.common.dto.DefaultSdkInvokeLogDTO;
import com.newyetai.sdk.common.util.HttpUtils;
import com.newyetai.sdk.deepseek.config.DeepSeekProperties;
import com.newyetai.sdk.xf.model.ChatReq;
import com.newyetai.sdk.xf.model.ChatRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class DeepSeekProvider {
private static DeepSeekProperties deepSeekProperties;
private DeepSeekProvider(DeepSeekProperties deepSeekProperties) {
DeepSeekProvider.deepSeekProperties = deepSeekProperties;
}
public static AlitaResult<String> httpChat(ChatReq req) {
return httpChat(req, deepSeekProperties.getHttpAPIPassword());
}
public static AlitaResult<String> httpChat(ChatReq req, String httpAPIPassword) {
ResponseEntity<ChatRes> response = null;
try {
try {
HttpHeaders header = new HttpHeaders();
header.add("Authorization", "Bearer " + httpAPIPassword);
header.add("Content-Type", "application/json");
// 发送请求
HttpEntity requestEntity = new HttpEntity(req, header);
response =
HttpUtils.restTemplate.exchange("https://api.deepseek.com/chat/completions",
HttpMethod.POST, requestEntity, ChatRes.class);
} catch (Exception e) {
log.error("http error:{}", e.getMessage());
return AlitaResult.error(SdkConstants.SDK_HTTP_INVOKE_ERROR_CODE, e.getMessage());
}
try {
String content = response.getBody().getChoices().get(0).getMessage().getContent();
return AlitaResult.success(content);
} catch (Exception e) {
return AlitaResult.error(SdkConstants.SDK_RESPONSE_CONVERT_ERROR_CODE, "返回结果获取content失败");
}
} finally {
log.info("invoke:{}",
JsonUtils
.toJsonString(
DefaultSdkInvokeLogDTO.builder().requestData(req).responseData(response)
.build()));
}
}
}