|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2023 Alibaba Group Holding Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package com.alibaba.higress.sdk.service.ai; |
| 14 | + |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Locale; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.function.BiConsumer; |
| 20 | + |
| 21 | +import org.apache.commons.collections4.MapUtils; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | + |
| 24 | +import com.alibaba.fastjson.JSON; |
| 25 | +import com.alibaba.fastjson.JSONException; |
| 26 | +import com.alibaba.fastjson.JSONObject; |
| 27 | +import com.alibaba.higress.sdk.constant.HigressConstants; |
| 28 | +import com.alibaba.higress.sdk.exception.ValidationException; |
| 29 | +import com.alibaba.higress.sdk.model.ServiceSource; |
| 30 | +import com.alibaba.higress.sdk.model.ai.LlmProviderEndpoint; |
| 31 | +import com.alibaba.higress.sdk.model.ai.LlmProviderType; |
| 32 | +import com.alibaba.higress.sdk.service.kubernetes.crd.mcp.V1McpBridge; |
| 33 | + |
| 34 | +public class VertexLlmProviderHandler extends AbstractLlmProviderHandler { |
| 35 | + |
| 36 | + private static final String VERTEX_AUTH_KEY_KEY = "vertexAuthKey"; |
| 37 | + private static final String VERTEX_REGION_KEY = "vertexRegion"; |
| 38 | + private static final String VERTEX_PROJECT_ID_KEY = "vertexProjectId"; |
| 39 | + private static final String VERTEX_AUTH_SERVICE_NAME_KEY = "vertexAuthServiceName"; |
| 40 | + private static final String VERTEX_TOKEN_REFRESH_AHEAD_KEY = "vertexTokenRefreshAhead"; |
| 41 | + private static final String GEMINI_SAFETY_SETTING_KEY = "geminiSafetySetting"; |
| 42 | + |
| 43 | + private static final String DOMAIN_FORMAT = "%s-aiplatform.googleapis.com"; |
| 44 | + |
| 45 | + private static final String DEFAULT_AUTH_SERVICE_NAME = |
| 46 | + "vertex-auth" + HigressConstants.INTERNAL_RESOURCE_NAME_SUFFIX; |
| 47 | + private static final String AUTH_SERVICE_DOMAIN = "oauth2.googleapis.com"; |
| 48 | + private static final List<ServiceSource> EXTRA_SERVICE_SOURCES; |
| 49 | + |
| 50 | + static { |
| 51 | + ServiceSource authServiceSource = new ServiceSource(); |
| 52 | + authServiceSource.setName(DEFAULT_AUTH_SERVICE_NAME); |
| 53 | + authServiceSource.setType(V1McpBridge.REGISTRY_TYPE_DNS); |
| 54 | + authServiceSource.setProtocol(V1McpBridge.PROTOCOL_HTTPS); |
| 55 | + authServiceSource.setPort(443); |
| 56 | + authServiceSource.setDomain(AUTH_SERVICE_DOMAIN); |
| 57 | + EXTRA_SERVICE_SOURCES = Collections.singletonList(authServiceSource); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getType() { |
| 62 | + return LlmProviderType.VERTEX; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void normalizeConfigs(Map<String, Object> configurations) { |
| 67 | + if (MapUtils.isEmpty(configurations)) { |
| 68 | + throw new ValidationException("Missing Vertex specific configurations."); |
| 69 | + } |
| 70 | + |
| 71 | + String region = MapUtils.getString(configurations, VERTEX_REGION_KEY); |
| 72 | + if (StringUtils.isEmpty(region)) { |
| 73 | + throw new ValidationException(VERTEX_REGION_KEY + " cannot be empty."); |
| 74 | + } |
| 75 | + configurations.put(VERTEX_REGION_KEY, region.toLowerCase(Locale.ROOT)); |
| 76 | + |
| 77 | + String projectId = MapUtils.getString(configurations, VERTEX_PROJECT_ID_KEY); |
| 78 | + if (StringUtils.isEmpty(projectId)) { |
| 79 | + throw new ValidationException(VERTEX_PROJECT_ID_KEY + " cannot be empty."); |
| 80 | + } |
| 81 | + |
| 82 | + String authKey = MapUtils.getString(configurations, VERTEX_AUTH_KEY_KEY); |
| 83 | + if (StringUtils.isEmpty(authKey)) { |
| 84 | + throw new ValidationException(VERTEX_AUTH_KEY_KEY + " cannot be empty."); |
| 85 | + } |
| 86 | + JSONObject authKeyObject; |
| 87 | + try { |
| 88 | + authKeyObject = JSON.parseObject(authKey); |
| 89 | + } catch (JSONException ex) { |
| 90 | + throw new ValidationException(VERTEX_AUTH_KEY_KEY + " must contain a valid JSON object.", ex); |
| 91 | + } |
| 92 | + final BiConsumer<JSONObject, String> ensureJsonStringProperty = (jsonObject, key) -> { |
| 93 | + Object value = jsonObject.get(key); |
| 94 | + if (!(value instanceof String)) { |
| 95 | + throw new ValidationException( |
| 96 | + VERTEX_AUTH_KEY_KEY + " must contain a valid JSON object with a string property: " + key); |
| 97 | + } |
| 98 | + }; |
| 99 | + ensureJsonStringProperty.accept(authKeyObject, "client_email"); |
| 100 | + ensureJsonStringProperty.accept(authKeyObject, "private_key_id"); |
| 101 | + ensureJsonStringProperty.accept(authKeyObject, "private_key"); |
| 102 | + ensureJsonStringProperty.accept(authKeyObject, "token_uri"); |
| 103 | + |
| 104 | + Integer tokenRefreshAhead = MapUtils.getInteger(configurations, VERTEX_TOKEN_REFRESH_AHEAD_KEY); |
| 105 | + if (tokenRefreshAhead != null && tokenRefreshAhead < 0) { |
| 106 | + throw new ValidationException(VERTEX_TOKEN_REFRESH_AHEAD_KEY + " must be a non-negative number."); |
| 107 | + } |
| 108 | + |
| 109 | + Object rawGeminiSafetySetting = configurations.get(GEMINI_SAFETY_SETTING_KEY); |
| 110 | + if (rawGeminiSafetySetting != null) { |
| 111 | + if (!(rawGeminiSafetySetting instanceof Map)) { |
| 112 | + throw new ValidationException(GEMINI_SAFETY_SETTING_KEY + " must be an object."); |
| 113 | + } |
| 114 | + Map<?, ?> geminiSafetySetting = (Map<?, ?>)rawGeminiSafetySetting; |
| 115 | + for (Map.Entry<?, ?> entry : geminiSafetySetting.entrySet()) { |
| 116 | + if (!(entry.getKey() instanceof String) || !(entry.getValue() instanceof String)) { |
| 117 | + throw new ValidationException( |
| 118 | + GEMINI_SAFETY_SETTING_KEY + " must be an object with string key-value pairs."); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + configurations.put(VERTEX_AUTH_SERVICE_NAME_KEY, DEFAULT_AUTH_SERVICE_NAME); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected List<LlmProviderEndpoint> getProviderEndpoints(Map<String, Object> providerConfig) { |
| 128 | + String region = MapUtils.getString(providerConfig, VERTEX_REGION_KEY); |
| 129 | + if (StringUtils.isEmpty(region)) { |
| 130 | + throw new ValidationException(VERTEX_REGION_KEY + " cannot be empty."); |
| 131 | + } |
| 132 | + String domain = String.format(DOMAIN_FORMAT, region); |
| 133 | + return Collections.singletonList(new LlmProviderEndpoint(V1McpBridge.PROTOCOL_HTTPS, domain, 443, "/")); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public List<ServiceSource> getExtraServiceSources(String providerName, Map<String, Object> providerConfig, |
| 138 | + boolean forDelete) { |
| 139 | + return forDelete ? Collections.emptyList() : EXTRA_SERVICE_SOURCES; |
| 140 | + } |
| 141 | +} |
0 commit comments