86 lines
4.0 KiB
Java
86 lines
4.0 KiB
Java
package com.moyi.controlplane.provider;
|
|||
|
|
|
||
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import com.moyi.controlplane.common.ApiException;
|
||
|
|
import java.net.URI;
|
||
|
|
import java.net.http.HttpClient;
|
||
|
|
import java.net.http.HttpRequest;
|
||
|
|
import java.net.http.HttpResponse;
|
||
|
|
import java.time.Duration;
|
||
|
|
import java.util.LinkedHashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import org.springframework.stereotype.Service;
|
||
|
|
|
||
|
|
@Service
|
||
|
|
public class OpenAiCompatibleProvider {
|
||
|
|
private final ObjectMapper objectMapper;
|
||
|
|
private final HttpClient httpClient;
|
||
|
|
private final Map<String, ProviderConfig> providers;
|
||
|
|
|
||
|
|
public OpenAiCompatibleProvider(
|
||
|
|
ObjectMapper objectMapper,
|
||
|
|
org.springframework.core.env.Environment environment
|
||
|
|
) {
|
||
|
|
this.objectMapper = objectMapper;
|
||
|
|
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||
|
|
this.providers = Map.of(
|
||
|
|
"provider-deepseek", new ProviderConfig(environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com")),
|
||
|
|
"provider-kimi", new ProviderConfig(environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1")),
|
||
|
|
"provider-zhipu", new ProviderConfig(environment.getProperty("moyi.providers.zhipu.api-key", ""), environment.getProperty("moyi.providers.zhipu.base-url", "https://open.bigmodel.cn/api/paas/v4"))
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public boolean isConfigured(String providerId) {
|
||
|
|
ProviderConfig provider = providers.get(providerId);
|
||
|
|
return provider != null && !provider.apiKey().isBlank();
|
||
|
|
}
|
||
|
|
|
||
|
|
public Map<String, Object> chat(String providerId, String upstreamModel, List<?> messages) {
|
||
|
|
ProviderConfig provider = providers.get(providerId);
|
||
|
|
if (provider == null || provider.apiKey().isBlank()) {
|
||
|
|
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||
|
|
}
|
||
|
|
Map<String, Object> body = new LinkedHashMap<>();
|
||
|
|
body.put("model", upstreamModel);
|
||
|
|
body.put("messages", messages == null ? List.of() : messages);
|
||
|
|
body.put("stream", false);
|
||
|
|
String json = writeJson(body);
|
||
|
|
HttpRequest request = HttpRequest.newBuilder()
|
||
|
|
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
||
|
|
.timeout(Duration.ofSeconds(90))
|
||
|
|
.header("Authorization", "Bearer " + provider.apiKey())
|
||
|
|
.header("Content-Type", "application/json")
|
||
|
|
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||
|
|
.build();
|
||
|
|
try {
|
||
|
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||
|
|
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||
|
|
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||
|
|
}
|
||
|
|
return objectMapper.readValue(response.body(), new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||
|
|
} catch (ApiException exception) {
|
||
|
|
throw exception;
|
||
|
|
} catch (InterruptedException exception) {
|
||
|
|
Thread.currentThread().interrupt();
|
||
|
|
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
|
||
|
|
} catch (Exception exception) {
|
||
|
|
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private String normalizeUrl(String value) {
|
||
|
|
return value == null ? "" : value.replaceAll("/+$", "");
|
||
|
|
}
|
||
|
|
|
||
|
|
private String writeJson(Object value) {
|
||
|
|
try {
|
||
|
|
return objectMapper.writeValueAsString(value);
|
||
|
|
} catch (JsonProcessingException exception) {
|
||
|
|
throw new ApiException("INVALID_REQUEST", "请求内容无法编码", 400);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private record ProviderConfig(String apiKey, String baseUrl) {}
|
||
|
|
}
|