2026-06-01 19:09:40 +08:00

29 lines
1.4 KiB
Java

package com.company.agentfoundation.api;
import com.company.agentfoundation.config.FoundationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.util.retry.Retry;
import java.time.Duration;
@Component
public class DeepseekClient {
private final FoundationProperties properties;
private final WebClient webClient;
public DeepseekClient(FoundationProperties properties, WebClient.Builder builder) {
this.properties = properties;
this.webClient = builder.baseUrl(properties.getDeepseek().getBaseUrl()).defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();
}
public DeepseekChatResponse chat(DeepseekChatRequest request) {
if (properties.getDeepseek().getApiKey() == null || properties.getDeepseek().getApiKey().isBlank()) throw new IllegalStateException("DEEPSEEK_API_KEY is missing");
return webClient.post()
.uri(properties.getDeepseek().getChatPath())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + properties.getDeepseek().getApiKey())
.bodyValue(request)
.retrieve()
.bodyToMono(DeepseekChatResponse.class)
.retryWhen(Retry.backoff(properties.getDeepseek().getMaxRetries(), Duration.ofMillis(300)))
.block(Duration.ofMillis(properties.getDeepseek().getReadTimeoutMs()));
}
}