feat: add java sync provider gateway
This commit is contained in:
1 parent
9da175c040
commit
31a34757be
4 files changed
+114
-6
No files matched your search
+13
-1
@@ -2,6 +2,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 java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -20,9 +21,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RequestMapping("/v1")
|
||||
public class GatewayController {
|
||||
private final CatalogService catalogService;
|
||||
private final OpenAiCompatibleProvider provider;
|
||||
|
||||
public GatewayController(CatalogService catalogService) {
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider) {
|
||||
this.catalogService = catalogService;
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
@GetMapping("/models")
|
||||
@@ -54,6 +57,15 @@ public class GatewayController {
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
||||
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
||||
String providerId = String.valueOf(model.get("providerId"));
|
||||
if (provider.isConfigured(providerId)) {
|
||||
if (Boolean.TRUE.equals(input.get("stream"))) throw new ApiException("STREAM_NOT_IMPLEMENTED", "流式转发将在下一节点开放", 501);
|
||||
List<?> messages = input.get("messages") instanceof List<?> values ? values : List.of();
|
||||
Map<String, Object> upstream = provider.chat(providerId, String.valueOf(model.get("upstreamModel")), messages);
|
||||
upstream.put("model", modelId);
|
||||
catalogService.touchKey(key);
|
||||
return ResponseEntity.ok(upstream);
|
||||
}
|
||||
String prompt = extractPrompt(input.get("messages"));
|
||||
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
|
||||
catalogService.touchKey(key);
|
||||
|
||||
Reference in new issue
Block a user