feat: add admin model usage audit
This commit is contained in:
1 parent
473479fc4c
commit
13f7fbbfcb
11 files changed
+566
-4
No files matched your search
+21
-2
@@ -3,6 +3,7 @@ package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -23,11 +24,13 @@ public class GatewayController {
|
||||
private final CatalogService catalogService;
|
||||
private final OpenAiCompatibleProvider provider;
|
||||
private final GatewayGuard guard;
|
||||
private final UsageAuditService usage;
|
||||
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard) {
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, UsageAuditService usage) {
|
||||
this.catalogService = catalogService;
|
||||
this.provider = provider;
|
||||
this.guard = guard;
|
||||
this.usage = usage;
|
||||
}
|
||||
|
||||
@GetMapping("/models")
|
||||
@@ -55,6 +58,7 @@ public class GatewayController {
|
||||
@RequestBody Map<String, Object> input
|
||||
) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
long startedAt = System.currentTimeMillis();
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
@@ -63,7 +67,13 @@ public class GatewayController {
|
||||
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
|
||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
try (GatewayGuard.Slot slot = guard.acquire(model)) {
|
||||
return invoke(key, modelId, model, input);
|
||||
ResponseEntity<Map<String, Object>> result = invoke(key, modelId, model, input);
|
||||
Map<String, Object> body = result.getBody();
|
||||
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "succeeded", (int) (System.currentTimeMillis() - startedAt), number(body, "usage.prompt_tokens"), number(body, "usage.completion_tokens"), input, body, null, null);
|
||||
return result;
|
||||
} catch (ApiException exception) {
|
||||
usage.recordCall(UUID.fromString(String.valueOf(key.get("userId"))), UUID.fromString(String.valueOf(key.get("id"))), modelId, String.valueOf(model.get("providerId")), "failed", (int) (System.currentTimeMillis() - startedAt), 0, 0, input, null, exception.getCode(), exception.getMessage());
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,4 +113,13 @@ public class GatewayController {
|
||||
return content == null ? "" : String.valueOf(content);
|
||||
}).reduce((left, right) -> left + "\n" + right).orElse("");
|
||||
}
|
||||
|
||||
private long number(Map<String, Object> body, String path) {
|
||||
if (body == null) return 0;
|
||||
Object usageValue = body.get("usage");
|
||||
if (!(usageValue instanceof Map<?, ?> map)) return 0;
|
||||
String key = path.substring(path.indexOf('.') + 1);
|
||||
Object value = map.get(key);
|
||||
return value instanceof Number number ? number.longValue() : 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user