feat: route upstream keys by model group
This commit is contained in:
1 parent
ce0a718686
commit
0876dfe08e
9 files changed
+195
-41
No files matched your search
+69
-15
@@ -47,10 +47,18 @@ public class AsyncProviderClient {
|
||||
return submit(providerId, upstreamModel, taskType, Map.of("prompt", prompt));
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, String prompt) {
|
||||
return submit(providerId, upstreamModel, routeId, taskType, Map.of("prompt", prompt));
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String taskType, Map<String,Object> input) {
|
||||
return submit(providerId, upstreamModel, null, taskType, input);
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, Map<String,Object> input) {
|
||||
if (input == null) input = Map.of();
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
|
||||
Provider provider = provider(providerId);
|
||||
Provider provider = provider(providerId, routeId, upstreamModel);
|
||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||
if (isTripo(provider.code(), provider.baseUrl())) {
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
@@ -60,7 +68,7 @@ public class AsyncProviderClient {
|
||||
else if (!prompt.isBlank()) body.put("prompt", prompt);
|
||||
body.put("texture", true);
|
||||
body.put("pbr", true);
|
||||
return request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), provider.nextApiKey(), body, "上游 3D 任务创建失败");
|
||||
return request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), provider.nextApiKey(routeId, upstreamModel, environment, keyCursors), body, "上游 3D 任务创建失败");
|
||||
}
|
||||
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
|
||||
List<Map<String,Object>> content = new ArrayList<>();
|
||||
@@ -71,25 +79,30 @@ public class AsyncProviderClient {
|
||||
content.add(Map.of("type", "image_url", "image_url", Map.of("url", media)));
|
||||
}
|
||||
Map<String,Object> body = Map.of("model", upstreamModel, "content", content);
|
||||
return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(), body, "上游视频任务创建失败");
|
||||
return request("POST", trim(provider.baseUrl()) + "/contents/generations/tasks", provider.nextApiKey(routeId, upstreamModel, environment, keyCursors), body, "上游视频任务创建失败");
|
||||
}
|
||||
if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
||||
return submitOpenAiCompatible(provider, upstreamModel, taskType, input);
|
||||
return submitOpenAiCompatible(provider, routeId, upstreamModel, taskType, input);
|
||||
}
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String providerTaskId) {
|
||||
Provider provider = provider(providerId);
|
||||
return poll(providerId, null, providerTaskId);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String routeId, String providerTaskId) {
|
||||
Provider provider = provider(providerId, routeId, null);
|
||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||
String url;
|
||||
String apiKey = provider.nextApiKey(routeId, null, environment, keyCursors);
|
||||
if (isTripo(provider.code(), provider.baseUrl())) url = tripoBase(provider.baseUrl()) + "/tasks/" + encode(providerTaskId);
|
||||
else if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) url = trim(provider.baseUrl()) + "/contents/generations/tasks/" + encode(providerTaskId);
|
||||
else if ("openai-compatible".equalsIgnoreCase(provider.protocol())) {
|
||||
return pollOpenAiCompatible(provider, providerTaskId);
|
||||
return pollOpenAiCompatible(provider, routeId, providerTaskId);
|
||||
}
|
||||
else throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
||||
return request("GET", url, provider.nextApiKey(), null, "上游异步任务查询失败");
|
||||
return request("GET", url, apiKey, null, "上游异步任务查询失败");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,7 +111,7 @@ public class AsyncProviderClient {
|
||||
* generation completes in the request, so mark the normalized result for
|
||||
* AsyncTaskService to settle immediately instead of inventing a task id.
|
||||
*/
|
||||
private Map<String,Object> submitOpenAiCompatible(Provider provider, String model, String taskType, Map<String,Object> input) {
|
||||
private Map<String,Object> submitOpenAiCompatible(Provider provider, String routeId, String model, String taskType, Map<String,Object> input) {
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
|
||||
String media = mediaUrl(input);
|
||||
String base = trim(provider.baseUrl());
|
||||
@@ -112,8 +125,8 @@ public class AsyncProviderClient {
|
||||
copyParameter(input, body, "response_format");
|
||||
copyParameter(input, body, "size");
|
||||
Map<String,Object> response = media == null
|
||||
? request("POST", base + "/images/generations", provider.nextApiKey(), body, "上游图片任务创建失败")
|
||||
: requestMultipart("POST", base + "/images/edits", provider.nextApiKey(), body, "image", media, "上游图片任务创建失败");
|
||||
? request("POST", base + "/images/generations", provider.nextApiKey(routeId, model, environment, keyCursors), body, "上游图片任务创建失败")
|
||||
: requestMultipart("POST", base + "/images/edits", provider.nextApiKey(routeId, model, environment, keyCursors), body, "image", media, "上游图片任务创建失败");
|
||||
Map<String,Object> result = compatibleResult(response);
|
||||
if (result != null) return Map.of("_moyiInlineResult", result);
|
||||
return response;
|
||||
@@ -125,7 +138,7 @@ public class AsyncProviderClient {
|
||||
// The standard OpenAI video endpoint is multipart even when no
|
||||
// reference file is supplied. JSON requests are rejected by the
|
||||
// official API and by relays that proxy it faithfully.
|
||||
String apiKey = provider.nextApiKey();
|
||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||
try {
|
||||
return requestMultipart("POST", base + "/videos", apiKey, body,
|
||||
media == null ? null : "input_reference", media, "上游视频任务创建失败");
|
||||
@@ -147,7 +160,7 @@ public class AsyncProviderClient {
|
||||
body.put("voice", input.getOrDefault("voice", "alloy"));
|
||||
body.put("response_format", input.getOrDefault("response_format", "mp3"));
|
||||
copyParameter(input, body, "speed");
|
||||
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", provider.nextApiKey(), body, "上游语音任务创建失败"));
|
||||
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", provider.nextApiKey(routeId, model, environment, keyCursors), body, "上游语音任务创建失败"));
|
||||
}
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道的媒体接口尚未配置", 501);
|
||||
}
|
||||
@@ -171,9 +184,9 @@ public class AsyncProviderClient {
|
||||
return value;
|
||||
}
|
||||
|
||||
private Map<String,Object> pollOpenAiCompatible(Provider provider, String providerTaskId) {
|
||||
private Map<String,Object> pollOpenAiCompatible(Provider provider, String routeId, String providerTaskId) {
|
||||
String base = trim(provider.baseUrl());
|
||||
String apiKey = provider.nextApiKey();
|
||||
String apiKey = provider.nextApiKey(routeId, null, environment, keyCursors);
|
||||
try {
|
||||
return request("GET", base + "/videos/" + encode(providerTaskId), apiKey, null, "上游异步任务查询失败");
|
||||
} catch (ApiException first) {
|
||||
@@ -363,6 +376,10 @@ public class AsyncProviderClient {
|
||||
}
|
||||
|
||||
private Provider provider(String id) {
|
||||
return provider(id, null, null);
|
||||
}
|
||||
|
||||
private Provider provider(String id, String routeId, String upstreamModel) {
|
||||
try {
|
||||
Map<String,Object> row = jdbc.queryForMap("SELECT code, protocol, base_url, credential_ciphertext FROM model_providers WHERE id=? AND status='active' AND deleted_at IS NULL", id);
|
||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||
@@ -378,7 +395,7 @@ public class AsyncProviderClient {
|
||||
addEnvironmentKeys(keys, "TRIPO_API_KEY");
|
||||
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
|
||||
}
|
||||
if (keys.isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
if (keys.isEmpty() && routeKeys(code, id, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
|
||||
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
||||
}
|
||||
@@ -400,6 +417,22 @@ public class AsyncProviderClient {
|
||||
String configured = environment.getProperty(name);
|
||||
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
|
||||
}
|
||||
|
||||
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
||||
for (String prefix : List.of("MOYI_UPSTREAM_API_KEYS_ROUTE_", "MOYI_UPSTREAM_API_KEYS_MODEL_")) {
|
||||
if (prefix.startsWith("MOYI_UPSTREAM_API_KEYS_ROUTE_") && (routeId == null || routeId.isBlank())) continue;
|
||||
String alias = prefix.endsWith("MODEL_") ? upstreamModel : routeId;
|
||||
if (alias == null || alias.isBlank()) continue;
|
||||
String configured = environment.getProperty(prefix + normalize(alias));
|
||||
if (configured == null || configured.isBlank()) continue;
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
if (!keys.isEmpty()) return keys.stream().distinct().toList();
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
private boolean isTripo(String code, String baseUrl) {
|
||||
String normalizedCode = code == null ? "" : code.toLowerCase(java.util.Locale.ROOT);
|
||||
String normalizedUrl = baseUrl == null ? "" : baseUrl.toLowerCase(java.util.Locale.ROOT);
|
||||
@@ -407,5 +440,26 @@ public class AsyncProviderClient {
|
||||
}
|
||||
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
||||
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||
if (routeKeys.isEmpty()) return nextApiKey();
|
||||
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||
}
|
||||
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
||||
if (environment == null) return List.of();
|
||||
for (String prefix : List.of("MOYI_UPSTREAM_API_KEYS_ROUTE_", "MOYI_UPSTREAM_API_KEYS_MODEL_")) {
|
||||
if (prefix.startsWith("MOYI_UPSTREAM_API_KEYS_ROUTE_") && (routeId == null || routeId.isBlank())) continue;
|
||||
String selector = prefix.endsWith("MODEL_") ? upstreamModel : routeId;
|
||||
if (selector == null || selector.isBlank()) continue;
|
||||
String value = environment.getProperty(prefix + normalize(selector));
|
||||
if (value == null || value.isBlank()) continue;
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
if (!keys.isEmpty()) return keys.stream().distinct().toList();
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
}
|
||||
}
|
||||
@@ -180,7 +180,7 @@ public class AsyncTaskService {
|
||||
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
||||
);
|
||||
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
for (Map<String,Object> row : pending) pollProviderTask(row);
|
||||
dispatchQueuedTasks();
|
||||
}
|
||||
@@ -205,7 +205,7 @@ public class AsyncTaskService {
|
||||
usage.updateTask(taskId, "running", null, null, null);
|
||||
try {
|
||||
Map<String,Object> input = taskInput(task);
|
||||
Map<String,Object> upstream = submitProviderTask(task, input);
|
||||
Map<String,Object> upstream = submitProviderTask(task, input);
|
||||
// Some OpenAI-compatible image/audio endpoints return the
|
||||
// generated media in the submit response rather than creating
|
||||
// a pollable job. Persist that result and settle the task in
|
||||
@@ -234,7 +234,7 @@ public class AsyncTaskService {
|
||||
// workers reliably.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))");
|
||||
String sql = """
|
||||
SELECT t.id, t.api_key_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input
|
||||
SELECT t.id, t.api_key_id, t.model_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input
|
||||
FROM model_tasks t
|
||||
JOIN model_catalog m ON m.id=t.model_id
|
||||
JOIN model_providers p ON p.id=m.provider_id
|
||||
@@ -344,15 +344,16 @@ public class AsyncTaskService {
|
||||
|
||||
private Map<String,Object> submitProviderTask(Map<String,Object> task, Map<String,Object> input) {
|
||||
String providerId = String.valueOf(task.get("provider_id"));
|
||||
String routeId = String.valueOf(task.get("model_id"));
|
||||
String model = String.valueOf(task.get("upstream_model"));
|
||||
String type = String.valueOf(task.get("task_type"));
|
||||
// Keep the legacy overload for rows created before V30. Apart from
|
||||
// compatibility with existing integrations, this makes a queued
|
||||
// prompt-only task indistinguishable from the original submit path.
|
||||
if (input.size() == 1 && input.containsKey("prompt")) {
|
||||
return providerClient.submit(providerId, model, type, String.valueOf(input.get("prompt")));
|
||||
return providerClient.submit(providerId, model, routeId, type, String.valueOf(input.get("prompt")));
|
||||
}
|
||||
return providerClient.submit(providerId, model, type, input);
|
||||
return providerClient.submit(providerId, model, routeId, type, input);
|
||||
}
|
||||
|
||||
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
|
||||
@@ -380,7 +381,7 @@ public class AsyncTaskService {
|
||||
}
|
||||
Map<String,Object> response;
|
||||
try {
|
||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id")));
|
||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("model_id")), String.valueOf(row.get("provider_task_id")));
|
||||
} catch (RuntimeException exception) {
|
||||
// Even a permanent authentication/configuration error only proves
|
||||
// that polling failed, not that an already accepted job stopped.
|
||||
|
||||
Reference in new issue
Block a user