2026-09-07 17:04:23 +08:00
|
|
|
package com.moyi.controlplane.provider;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.moyi.controlplane.common.ApiException;
|
2026-09-07 17:23:18 +08:00
|
|
|
import com.moyi.controlplane.security.SecretCipher;
|
2026-09-07 17:04:23 +08:00
|
|
|
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;
|
2026-09-07 17:23:18 +08:00
|
|
|
import org.springframework.dao.EmptyResultDataAccessException;
|
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
2026-09-07 17:04:23 +08:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class OpenAiCompatibleProvider {
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
private final HttpClient httpClient;
|
2026-09-07 17:23:18 +08:00
|
|
|
private final JdbcTemplate jdbc;
|
|
|
|
|
private final SecretCipher secretCipher;
|
2026-09-07 17:04:23 +08:00
|
|
|
private final Map<String, ProviderConfig> providers;
|
|
|
|
|
|
|
|
|
|
public OpenAiCompatibleProvider(
|
|
|
|
|
ObjectMapper objectMapper,
|
2026-09-07 17:23:18 +08:00
|
|
|
org.springframework.core.env.Environment environment,
|
|
|
|
|
JdbcTemplate jdbc,
|
|
|
|
|
SecretCipher secretCipher
|
2026-09-07 17:04:23 +08:00
|
|
|
) {
|
|
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
|
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
2026-09-07 17:23:18 +08:00
|
|
|
this.jdbc = jdbc;
|
|
|
|
|
this.secretCipher = secretCipher;
|
2026-09-07 17:04:23 +08:00
|
|
|
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) {
|
2026-09-07 17:23:18 +08:00
|
|
|
ProviderConfig provider = resolve(providerId);
|
2026-09-07 17:04:23 +08:00
|
|
|
return provider != null && !provider.apiKey().isBlank();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Map<String, Object> chat(String providerId, String upstreamModel, List<?> messages) {
|
2026-09-07 17:23:18 +08:00
|
|
|
ProviderConfig provider = resolve(providerId);
|
2026-09-07 17:04:23 +08:00
|
|
|
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("/+$", "");
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-07 17:23:18 +08:00
|
|
|
private ProviderConfig resolve(String providerId) {
|
|
|
|
|
ProviderConfig fixed = providers.get(providerId);
|
|
|
|
|
if (fixed != null && !fixed.apiKey().isBlank()) return fixed;
|
|
|
|
|
try {
|
|
|
|
|
Map<String, Object> row = jdbc.queryForMap("SELECT base_url, credential_ciphertext FROM model_providers WHERE id = ? AND status = 'active'", providerId);
|
|
|
|
|
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
|
|
|
|
String apiKey = ciphertext.isBlank() ? "" : secretCipher.decrypt(ciphertext);
|
|
|
|
|
String baseUrl = row.get("base_url") == null ? "" : String.valueOf(row.get("base_url"));
|
|
|
|
|
return new ProviderConfig(apiKey, baseUrl);
|
|
|
|
|
} catch (EmptyResultDataAccessException exception) {
|
|
|
|
|
return fixed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-07 17:04:23 +08:00
|
|
|
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) {}
|
|
|
|
|
}
|