|
|
@@ -6,10 +6,12 @@ import com.njuzr.eaibackend.dto.deepseek.DeepSeekMessage;
|
|
|
import com.njuzr.eaibackend.dto.deepseek.DeepSeekRequest;
|
|
|
import com.njuzr.eaibackend.dto.deepseek.DeepSeekResponse;
|
|
|
import com.njuzr.eaibackend.dto.deepseek.DeepSeekStreamResponse;
|
|
|
+import com.njuzr.eaibackend.exception.MyException;
|
|
|
import com.njuzr.eaibackend.service.DeepSeekService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -56,7 +58,9 @@ public class DeepSeekServiceImpl implements DeepSeekService {
|
|
|
RestTemplate restTemplate = createRestTemplate();
|
|
|
org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- headers.setBearerAuth(deepSeekConfig.getApiKey());
|
|
|
+ String apiKey = deepSeekConfig.getApiKey();
|
|
|
+ if (apiKey != null) apiKey = apiKey.trim();
|
|
|
+ headers.setBearerAuth(apiKey);
|
|
|
|
|
|
org.springframework.http.HttpEntity<DeepSeekRequest> entity =
|
|
|
new org.springframework.http.HttpEntity<>(request, headers);
|
|
|
@@ -167,6 +171,12 @@ public class DeepSeekServiceImpl implements DeepSeekService {
|
|
|
new Thread(() -> {
|
|
|
HttpURLConnection connection = null;
|
|
|
try {
|
|
|
+ String apiKey = deepSeekConfig.getApiKey();
|
|
|
+ if (apiKey != null) apiKey = apiKey.trim();
|
|
|
+ if (apiKey == null || apiKey.isBlank()) {
|
|
|
+ throw MyException.create(HttpStatus.UNAUTHORIZED, "DeepSeek API key 未配置(deepseek.api-key / DEEPSEEK_API_KEY)");
|
|
|
+ }
|
|
|
+
|
|
|
DeepSeekRequest request = DeepSeekRequest.builder()
|
|
|
.model(deepSeekConfig.getModel())
|
|
|
.messages(messages)
|
|
|
@@ -180,7 +190,7 @@ public class DeepSeekServiceImpl implements DeepSeekService {
|
|
|
connection = (HttpURLConnection) new URL(url).openConnection();
|
|
|
connection.setRequestMethod("POST");
|
|
|
connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
|
|
- connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + deepSeekConfig.getApiKey());
|
|
|
+ connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);
|
|
|
connection.setDoOutput(true);
|
|
|
connection.setDoInput(true);
|
|
|
connection.setConnectTimeout(deepSeekConfig.getTimeout());
|