complete reset async billing and upstream key rotation
This commit is contained in:
1 parent
b54b6a8ac4
commit
a80896e3c7
19 files changed
+531
-50
No files matched your search
+39
-10
@@ -12,7 +12,10 @@ import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
@@ -27,6 +30,7 @@ public class OpenAiCompatibleProvider {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final SecretCipher secretCipher;
|
||||
private final Map<String, ProviderConfig> providers;
|
||||
private final org.springframework.core.env.Environment environment;
|
||||
private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; });
|
||||
private final int timeoutSeconds;
|
||||
|
||||
@@ -40,17 +44,18 @@ public class OpenAiCompatibleProvider {
|
||||
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||||
this.jdbc = jdbc;
|
||||
this.secretCipher = secretCipher;
|
||||
this.environment = environment;
|
||||
this.timeoutSeconds = Math.max(1,Math.min(90,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90)));
|
||||
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"))
|
||||
"provider-deepseek", configured("provider-deepseek", environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com"), environment),
|
||||
"provider-kimi", configured("provider-kimi", environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1"), environment),
|
||||
"provider-zhipu", configured("provider-zhipu", environment.getProperty("moyi.providers.zhipu.api-key", ""), environment.getProperty("moyi.providers.zhipu.base-url", "https://open.bigmodel.cn/api/paas/v4"), environment)
|
||||
);
|
||||
}
|
||||
|
||||
public boolean isConfigured(String providerId) {
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
return provider != null && !provider.apiKey().isBlank();
|
||||
return provider != null && provider.hasKey();
|
||||
}
|
||||
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, Map<String,Object> input) {
|
||||
@@ -68,7 +73,7 @@ public class OpenAiCompatibleProvider {
|
||||
|
||||
private StreamResponse open(String providerId, String upstreamModel, Map<String,Object> input, boolean streaming) {
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
if (provider == null || provider.apiKey().isBlank()) {
|
||||
if (provider == null || !provider.hasKey()) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||||
}
|
||||
Map<String, Object> body = new LinkedHashMap<>(input);
|
||||
@@ -76,11 +81,12 @@ public class OpenAiCompatibleProvider {
|
||||
body.put("stream", streaming);
|
||||
if(streaming) body.put("stream_options",Map.of("include_usage",true));
|
||||
else body.remove("stream_options");
|
||||
String apiKey = provider.nextApiKey();
|
||||
String json = writeJson(body);
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
||||
.timeout(Duration.ofSeconds(timeoutSeconds))
|
||||
.header("Authorization", "Bearer " + provider.apiKey())
|
||||
.header("Authorization", "Bearer " + apiKey)
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
@@ -176,13 +182,14 @@ public class OpenAiCompatibleProvider {
|
||||
|
||||
private ProviderConfig resolve(String providerId) {
|
||||
ProviderConfig fixed = providers.get(providerId);
|
||||
if (fixed != null && !fixed.apiKey().isBlank()) return fixed;
|
||||
if (fixed != null && fixed.hasKey()) return fixed;
|
||||
try {
|
||||
Map<String, Object> row = jdbc.queryForMap("SELECT base_url, credential_ciphertext FROM model_providers WHERE id = ? AND status = 'active'", providerId);
|
||||
Map<String, Object> row = jdbc.queryForMap("SELECT code, base_url, credential_ciphertext FROM model_providers WHERE id = ? AND status = 'active'", providerId);
|
||||
String providerCode = row.get("code") == null ? "" : String.valueOf(row.get("code"));
|
||||
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);
|
||||
return configured(providerId, providerCode, apiKey, baseUrl, environment);
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
return fixed;
|
||||
}
|
||||
@@ -196,5 +203,27 @@ public class OpenAiCompatibleProvider {
|
||||
}
|
||||
}
|
||||
|
||||
private record ProviderConfig(String apiKey, String baseUrl) {}
|
||||
private ProviderConfig configured(String providerId, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment) {
|
||||
return configured(providerId, providerId, fallbackKey, baseUrl, environment);
|
||||
}
|
||||
|
||||
private ProviderConfig configured(String providerId, String providerCode, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
||||
addEnvironmentKeys(keys, providerId, environment);
|
||||
if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment);
|
||||
return new ProviderConfig(keys.stream().distinct().toList(), baseUrl);
|
||||
}
|
||||
|
||||
private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) {
|
||||
String envName = "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
||||
String configured = environment == null ? null : environment.getProperty(envName);
|
||||
if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
}
|
||||
|
||||
private record ProviderConfig(List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
||||
private ProviderConfig(List<String> apiKeys, String baseUrl) { this(apiKeys, baseUrl, new AtomicInteger()); }
|
||||
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user