feat: distinguish model vendors and reuse playground keys
This commit is contained in:
1 parent
0a44a0802b
commit
281849ceb8
13 files changed
+222
-46
No files matched your search
@@ -49,7 +49,7 @@ public class CatalogService {
|
||||
|
||||
public List<Map<String, Object>> listModels(boolean includeDisabled) {
|
||||
String statusClause = " WHERE m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL" + (includeDisabled ? "" : " AND m.status = 'active' AND p.status = 'active' AND d.status = 'active'");
|
||||
return jdbc.query("SELECT m.* FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id" + statusClause + " ORDER BY m.public_name, m.source_label", this::mapModel);
|
||||
return jdbc.query("SELECT m.*, d.vendor_name FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id" + statusClause + " ORDER BY m.public_name, m.source_label", this::mapModel);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listDefinitions(boolean includeDisabled) {
|
||||
@@ -58,6 +58,7 @@ public class CatalogService {
|
||||
item.put("id", rs.getString("id"));
|
||||
item.put("publicName", rs.getString("public_name"));
|
||||
item.put("modelKey", rs.getString("model_key"));
|
||||
item.put("vendorName", rs.getString("vendor_name"));
|
||||
item.put("type", rs.getString("model_type"));
|
||||
item.put("mode", rs.getString("call_mode"));
|
||||
item.put("description", rs.getString("description"));
|
||||
@@ -78,13 +79,14 @@ public class CatalogService {
|
||||
String name = required(value, "publicName");
|
||||
String type = required(value, "type");
|
||||
String mode = required(value, "mode");
|
||||
String vendorName = optional(value, "vendorName", "未标注");
|
||||
String status = status(value);
|
||||
if (name.length() > 160 || !MODEL_TYPES.contains(type) || !CALL_MODES.contains(mode)) throw new ApiException("INVALID_MODEL", "模型名称、类型或调用模式无效");
|
||||
if (name.length() > 160 || vendorName.length() > 120 || !MODEL_TYPES.contains(type) || !CALL_MODES.contains(mode)) throw new ApiException("INVALID_MODEL", "模型名称、厂商、类型或调用模式无效");
|
||||
if (creating) {
|
||||
id = UUID.randomUUID().toString();
|
||||
jdbc.update("INSERT INTO model_definitions (id, model_key, public_name, model_type, call_mode, description, input_types, output_types, status) VALUES (?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), ?)", id, optional(value, "modelKey", null), name, type, mode, optional(value, "description", ""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), status);
|
||||
jdbc.update("INSERT INTO model_definitions (id, model_key, public_name, vendor_name, model_type, call_mode, description, input_types, output_types, status) VALUES (?, ?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), ?)", id, optional(value, "modelKey", null), name, vendorName, type, mode, optional(value, "description", ""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), status);
|
||||
} else {
|
||||
jdbc.update("UPDATE model_definitions SET model_key=?, public_name=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), status=?, updated_at=now() WHERE id=?", optional(value,"modelKey",null), name, type, mode, optional(value,"description",""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), status, id);
|
||||
jdbc.update("UPDATE model_definitions SET model_key=?, public_name=?, vendor_name=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), status=?, updated_at=now() WHERE id=?", optional(value,"modelKey",null), name, vendorName, type, mode, optional(value,"description",""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), status, id);
|
||||
jdbc.update("UPDATE model_catalog SET model_key=?, public_name=?, model_type=?, call_mode=?, description=?, input_types=CAST(? AS jsonb), output_types=CAST(? AS jsonb), updated_at=now() WHERE logical_model_id=? AND deleted_at IS NULL", optional(value,"modelKey",null), name, type, mode, optional(value,"description",""), jsonArray(value.get("inputTypes")), jsonArray(value.get("outputTypes")), id);
|
||||
}
|
||||
auth.audit(creating ? "model_definition.created" : "model_definition.updated", admin.id(), new AuthService.RequestMeta(null,null), Map.of("modelId",id));
|
||||
@@ -371,6 +373,16 @@ public class CatalogService {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> requireOwnedKey(String sessionToken, String keyId, AuthService auth) {
|
||||
var user = auth.requireUser(sessionToken);
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT * FROM platform_api_keys WHERE id=? AND user_id=? AND status='active' AND deleted_at IS NULL",
|
||||
this::mapApiKey, UUID.fromString(keyId), user.id());
|
||||
} catch (IllegalArgumentException | EmptyResultDataAccessException exception) {
|
||||
throw new ApiException("INVALID_API_KEY", "请选择当前账号下已启用的 API Key", 403);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> activeModel(String id) {
|
||||
return findActiveModel(id);
|
||||
}
|
||||
@@ -406,12 +418,12 @@ public class CatalogService {
|
||||
}
|
||||
|
||||
private Map<String, Object> findModel(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_catalog WHERE id = ? AND deleted_at IS NULL", this::mapModel, id); }
|
||||
try { return jdbc.queryForObject("SELECT m.*, d.vendor_name FROM model_catalog m JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.deleted_at IS NULL", this::mapModel, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findActiveModel(String id) {
|
||||
try { return jdbc.queryForObject("SELECT m.* FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.status = 'active' AND p.status='active' AND d.status='active' AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL", this::mapModel, id); }
|
||||
try { return jdbc.queryForObject("SELECT m.*, d.vendor_name FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id JOIN model_definitions d ON d.id=m.logical_model_id WHERE m.id = ? AND m.status = 'active' AND p.status='active' AND d.status='active' AND m.deleted_at IS NULL AND p.deleted_at IS NULL AND d.deleted_at IS NULL", this::mapModel, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
@@ -451,6 +463,7 @@ public class CatalogService {
|
||||
model.put("logicalModelId", resultSet.getString("logical_model_id"));
|
||||
model.put("providerId", resultSet.getString("provider_id"));
|
||||
putIfNotNull(model, "modelKey", resultSet.getString("model_key"));
|
||||
putIfNotNull(model, "vendorName", resultSet.getString("vendor_name"));
|
||||
putIfNotNull(model, "sourceLabel", resultSet.getString("source_label"));
|
||||
model.put("publicName", resultSet.getString("public_name"));
|
||||
model.put("upstreamModel", resultSet.getString("upstream_model"));
|
||||
|
||||
+6
-1
@@ -63,7 +63,12 @@ public class GatewayController {
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
HttpServletResponse servletResponse
|
||||
) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse);
|
||||
}
|
||||
|
||||
// Both the public gateway and the authenticated playground share admission and billing.
|
||||
public ResponseEntity<Map<String, Object>> executeChat(Map<String, Object> key,
|
||||
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.moyi.controlplane.auth.AuthController;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/playground/keys/{keyId}")
|
||||
public class PlaygroundController {
|
||||
private final AuthService auth;
|
||||
private final CatalogService catalog;
|
||||
private final GatewayController gateway;
|
||||
private final AsyncTaskService tasks;
|
||||
|
||||
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
||||
this.auth = auth;
|
||||
this.catalog = catalog;
|
||||
this.gateway = gateway;
|
||||
this.tasks = tasks;
|
||||
}
|
||||
|
||||
@PostMapping(value="/chat/completions", consumes="application/json")
|
||||
public ResponseEntity<Map<String, Object>> chat(@PathVariable String keyId,
|
||||
@RequestBody Map<String, Object> input,
|
||||
@RequestHeader(value="Idempotency-Key", required=false) String idempotencyKey,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
return gateway.executeChat(requireKey(request, keyId), input, idempotencyKey, response);
|
||||
}
|
||||
|
||||
@PostMapping(value="/{type:3d|video}/tasks", consumes="application/json")
|
||||
public ResponseEntity<Map<String, Object>> createTask(@PathVariable String keyId, @PathVariable String type,
|
||||
@RequestBody Map<String, Object> input, HttpServletRequest request) {
|
||||
return ResponseEntity.accepted().body(Map.of("task", tasks.createForKey(requireKey(request, keyId), type, input)));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks/{taskId}")
|
||||
public Map<String, Object> task(@PathVariable String keyId, @PathVariable String taskId, HttpServletRequest request) {
|
||||
return Map.of("task", tasks.getForKey(requireKey(request, keyId), taskId));
|
||||
}
|
||||
|
||||
private Map<String, Object> requireKey(HttpServletRequest request, String keyId) {
|
||||
// A custom header prevents cross-site forms from spending a cookie-authenticated wallet.
|
||||
// Cross-origin scripts must pass the existing explicit CORS origin allowlist.
|
||||
if (!"playground".equals(request.getHeader("X-Moyi-Source")))
|
||||
throw new ApiException("PLAYGROUND_REQUEST_REQUIRED", "请通过在线调试页面发起调用", 403);
|
||||
return catalog.requireOwnedKey(AuthController.sessionToken(request), keyId, auth);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,11 @@ public class AsyncTaskService {
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
return createForKey(requireKey(authorization), taskType, input);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
|
||||
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||
@@ -77,7 +81,10 @@ public class AsyncTaskService {
|
||||
}
|
||||
|
||||
public Map<String, Object> get(String authorization, String taskId) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
return getForKey(requireKey(authorization), taskId);
|
||||
}
|
||||
|
||||
public Map<String, Object> getForKey(Map<String, Object> key, String taskId) {
|
||||
UUID userId = catalogService.keyOwnerId(UUID.fromString(String.valueOf(key.get("id"))));
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT * FROM model_tasks WHERE id = ? AND user_id = ?", this::mapTask, taskId, userId);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
ALTER TABLE model_definitions
|
||||
ADD COLUMN IF NOT EXISTS vendor_name varchar(120) NOT NULL DEFAULT '未标注';
|
||||
|
||||
-- Give the models imported by the relay catalog a useful initial manufacturer.
|
||||
-- Administrators can correct the label from 模型与分组 at any time.
|
||||
UPDATE model_definitions
|
||||
SET vendor_name = CASE
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(openai|gpt-|^gpt|o1|o3|o4)' THEN 'OpenAI'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(glm|chatglm|zhipu|智谱)' THEN '智谱 AI'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(kimi|moonshot|月之暗面)' THEN '月之暗面'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(doubao|seed|豆包)' THEN '字节跳动'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ 'deepseek' THEN 'DeepSeek'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(claude|anthropic)' THEN 'Anthropic'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(gemini|google)' THEN 'Google'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(qwen|tongyi|通义)' THEN '阿里云'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(llama|meta)' THEN 'Meta'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ 'mistral' THEN 'Mistral AI'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(grok|xai)' THEN 'xAI'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ 'minimax' THEN 'MiniMax'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ 'tripo' THEN 'Tripo'
|
||||
WHEN lower(coalesce(model_key, '') || ' ' || coalesce(public_name, '')) ~ '(runway|midjourney)' THEN '其他图像/视频厂商'
|
||||
ELSE '未标注'
|
||||
END
|
||||
WHERE vendor_name = '未标注';
|
||||
+2
-1
@@ -106,8 +106,9 @@ class PlatformAdminPostgresTest {
|
||||
var channel = catalog.createProvider(Map.of("code","test-relay","name","Relay","protocol","openai-compatible","sourceType","relay","baseUrl","https://example.test/v1","credentialType","api-key","credentialSecret","only-a-test-secret"), ownerToken, auth);
|
||||
String channelId = channel.get("id").toString();
|
||||
String cipher = jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?", String.class, channelId);
|
||||
var model = catalog.saveDefinition(null,Map.of("publicName","Test Model","type","text","mode","sync"),ownerToken,auth);
|
||||
var model = catalog.saveDefinition(null,Map.of("publicName","Test Model","vendorName","OpenAI","type","text","mode","sync"),ownerToken,auth);
|
||||
String modelId = model.get("id").toString();
|
||||
assertEquals("OpenAI", model.get("vendorName"));
|
||||
var first = catalog.createModel(Map.of("logicalModelId",modelId,"providerId",channelId,"sourceLabel","Primary","upstreamModel","upstream-a"),ownerToken,auth);
|
||||
String firstId = first.get("id").toString();
|
||||
var second = catalog.createModel(Map.of("logicalModelId",modelId,"providerId",channelId,"sourceLabel","Backup","upstreamModel","upstream-b"),ownerToken,auth);
|
||||
|
||||
Reference in new issue
Block a user