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);
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
package com.moyi.controlplane.provider;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OpenAiCompatibleProvider {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final HttpClient httpClient;
|
||||
private final Map<String, ProviderConfig> providers;
|
||||
|
||||
public OpenAiCompatibleProvider(
|
||||
ObjectMapper objectMapper,
|
||||
org.springframework.core.env.Environment environment
|
||||
) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||||
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"))
|
||||
);
|
||||
}
|
||||
|
||||
public boolean isConfigured(String providerId) {
|
||||
ProviderConfig provider = providers.get(providerId);
|
||||
return provider != null && !provider.apiKey().isBlank();
|
||||
}
|
||||
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, List<?> messages) {
|
||||
ProviderConfig provider = providers.get(providerId);
|
||||
if (provider == null || provider.apiKey().isBlank()) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||||
}
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("model", upstreamModel);
|
||||
body.put("messages", messages == null ? List.of() : messages);
|
||||
body.put("stream", false);
|
||||
String json = writeJson(body);
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(normalizeUrl(provider.baseUrl()) + "/chat/completions"))
|
||||
.timeout(Duration.ofSeconds(90))
|
||||
.header("Authorization", "Bearer " + provider.apiKey())
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json))
|
||||
.build();
|
||||
try {
|
||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||||
}
|
||||
return objectMapper.readValue(response.body(), new com.fasterxml.jackson.core.type.TypeReference<>() {});
|
||||
} catch (ApiException exception) {
|
||||
throw exception;
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeUrl(String value) {
|
||||
return value == null ? "" : value.replaceAll("/+$", "");
|
||||
}
|
||||
|
||||
private String writeJson(Object value) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(value);
|
||||
} catch (JsonProcessingException exception) {
|
||||
throw new ApiException("INVALID_REQUEST", "请求内容无法编码", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private record ProviderConfig(String apiKey, String baseUrl) {}
|
||||
}
|
||||
@@ -49,3 +49,13 @@ moyi:
|
||||
oss-bucket: ${OSS_BUCKET:}
|
||||
oss-access-key-id: ${OSS_ACCESS_KEY_ID:}
|
||||
oss-access-key-secret: ${OSS_ACCESS_KEY_SECRET:}
|
||||
providers:
|
||||
deepseek:
|
||||
api-key: ${DEEPSEEK_API_KEY:}
|
||||
base-url: ${DEEPSEEK_BASE_URL:https://api.deepseek.com}
|
||||
kimi:
|
||||
api-key: ${KIMI_API_KEY:}
|
||||
base-url: ${KIMI_BASE_URL:https://api.moonshot.ai/v1}
|
||||
zhipu:
|
||||
api-key: ${ZHIPU_API_KEY:}
|
||||
base-url: ${ZHIPU_BASE_URL:https://open.bigmodel.cn/api/paas/v4}
|
||||
Reference in new issue
Block a user