Skip to content

Commit

Permalink
feat: Update AI console function. (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Oct 21, 2024
1 parent d625928 commit 76c6abd
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import javax.validation.ValidationException;
import javax.validation.constraints.NotBlank;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -35,7 +34,6 @@
import com.alibaba.higress.sdk.model.CommonPageQuery;
import com.alibaba.higress.sdk.model.PaginatedResult;
import com.alibaba.higress.sdk.model.consumer.Consumer;
import com.alibaba.higress.sdk.model.consumer.Credential;
import com.alibaba.higress.sdk.service.consumer.ConsumerService;

@RestController("ConsumersController")
Expand All @@ -53,17 +51,13 @@ public void setConsumerService(ConsumerService consumerService) {
@GetMapping
public ResponseEntity<PaginatedResponse<Consumer>> list(CommonPageQuery query) {
PaginatedResult<Consumer> consumers = consumerService.list(query);
if (CollectionUtils.isNotEmpty(consumers.getData())) {
consumers.getData().forEach(ConsumersController::maskSensitiveData);
}
return ControllerUtil.buildResponseEntity(consumers);
}

@PostMapping
public ResponseEntity<Response<Consumer>> add(@RequestBody Consumer consumer) {
consumer.validate(false);
Consumer newConsumer = consumerService.addOrUpdate(consumer);
maskSensitiveData(newConsumer);
return ControllerUtil.buildResponseEntity(newConsumer);
}

Expand All @@ -83,7 +77,6 @@ public ResponseEntity<Response<Consumer>> put(@PathVariable("name") @NotBlank St
}
consumer.validate(true);
Consumer updatedConsumer = consumerService.addOrUpdate(consumer);
maskSensitiveData(updatedConsumer);
return ControllerUtil.buildResponseEntity(updatedConsumer);
}

Expand All @@ -92,10 +85,4 @@ public ResponseEntity<Response<Consumer>> delete(@PathVariable("name") @NotBlank
consumerService.delete(name);
return ResponseEntity.noContent().build();
}

private static void maskSensitiveData(Consumer consumer) {
if (consumer != null && CollectionUtils.isNotEmpty(consumer.getCredentials())) {
consumer.getCredentials().forEach(Credential::maskSensitiveData);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import javax.validation.ValidationException;
import javax.validation.constraints.NotBlank;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand Down Expand Up @@ -52,25 +51,18 @@ public void setLlmProviderService(LlmProviderService llmProviderService) {
@GetMapping
public ResponseEntity<PaginatedResponse<LlmProvider>> list(CommonPageQuery query) {
PaginatedResult<LlmProvider> providers = llmProviderService.list(query);
if (CollectionUtils.isNotEmpty(providers.getData())) {
providers.getData().forEach(LlmProvider::maskSensitiveData);
}
return ControllerUtil.buildResponseEntity(providers);
}

@PostMapping
public ResponseEntity<Response<LlmProvider>> add(@RequestBody LlmProvider certificate) {
LlmProvider newProvider = llmProviderService.addOrUpdate(certificate);
newProvider.maskSensitiveData();
return ControllerUtil.buildResponseEntity(newProvider);
}

@GetMapping(value = "/{name}")
public ResponseEntity<Response<LlmProvider>> query(@PathVariable("name") @NotBlank String name) {
LlmProvider provider = llmProviderService.query(name);
if (provider != null){
provider.maskSensitiveData();
}
return ControllerUtil.buildResponseEntity(provider);
}

Expand All @@ -83,7 +75,6 @@ public ResponseEntity<Response<LlmProvider>> put(@PathVariable("name") @NotBlank
throw new ValidationException("Provider name in the URL doesn't match the one in the body.");
}
LlmProvider updatedProvider = llmProviderService.addOrUpdate(provider);
updatedProvider.maskSensitiveData();
return ControllerUtil.buildResponseEntity(updatedProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public class AiRoute {
private String version;
private List<String> domains;
private List<AiUpstream> upstreams;
private Boolean authEnabled;
private List<String> consumers;
private AiUpstream fallbackUpstream;
private AiRouteAuthConfig authConfig;
private AiRouteFallbackConfig fallbackConfig;

public void validate() {
if (StringUtils.isBlank(name)) {
Expand All @@ -47,8 +46,12 @@ public void validate() {
if (CollectionUtils.isEmpty(upstreams)){
throw new ValidationException("upstreams cannot be empty.");
}
if (Boolean.TRUE.equals(authEnabled) && CollectionUtils.isEmpty(consumers)){
throw new ValidationException("consumers cannot be empty when authEnabled is true.");
upstreams.forEach(AiUpstream::validate);
if (authConfig != null){
authConfig.validate();
}
if (fallbackConfig != null){
fallbackConfig.validate();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2022-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.alibaba.higress.sdk.model.ai;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.alibaba.higress.sdk.exception.ValidationException;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AiRouteAuthConfig {

private Boolean enabled;
private List<String> allowedConsumers;

public void validate() {
if (Boolean.TRUE.equals(enabled) && CollectionUtils.isEmpty(allowedConsumers)) {
throw new ValidationException("allowedConsumers cannot be empty when auth is enabled.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2022-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.alibaba.higress.sdk.model.ai;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;

import com.alibaba.higress.sdk.exception.ValidationException;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AiRouteFallbackConfig {

private Boolean enabled;
private List<AiUpstream> upstreams;
private String fallbackStrategy;

public void validate() {
if (!Boolean.TRUE.equals(enabled)) {
return;
}
if (StringUtils.isNotEmpty(fallbackStrategy)) {
switch (fallbackStrategy) {
case AiRouteFallbackStrategy.RANDOM:
case AiRouteFallbackStrategy.SEQUENCE:
break;
default:
throw new ValidationException("unknown fallback strategy: " + fallbackStrategy);
}
}
if (CollectionUtils.isEmpty(upstreams)) {
throw new ValidationException("upstreams cannot be empty when fallback is enabled.");
}
upstreams.forEach(AiUpstream::validate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2022-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.alibaba.higress.sdk.model.ai;

public final class AiRouteFallbackStrategy {

private AiRouteFallbackStrategy() {
}

public static final String RANDOM = "RAND";

public static final String SEQUENCE = "SEQ";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
package com.alibaba.higress.sdk.model.ai;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.higress.sdk.exception.ValidationException;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -27,4 +31,10 @@ public class AiUpstream {

private String provider;
private Integer weight;

public void validate() {
if (StringUtils.isEmpty(provider)) {
throw new ValidationException("provider cannot be null or empty.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.higress.sdk.util.StringUtil;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -53,13 +50,4 @@ public void validate(boolean forUpdate) {
throw new IllegalArgumentException("Unknown protocol: " + protocol);
}
}

public void maskSensitiveData() {
if (CollectionUtils.isEmpty(tokens)) {
return;
}
for (int i = 0; i < tokens.size(); ++i) {
tokens.set(i, StringUtil.mask(tokens.get(i)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@ public void setProperty(String name, Object value) {
}

public void validate(boolean forUpdate) {}

public void maskSensitiveData() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.apache.commons.lang3.StringUtils;

import com.alibaba.higress.sdk.exception.ValidationException;
import com.alibaba.higress.sdk.util.StringUtil;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -78,10 +77,4 @@ public void validate(boolean forUpdate) {
throw new ValidationException("value cannot be blank.");
}
}

@Override
public void maskSensitiveData() {
super.maskSensitiveData();
this.value = StringUtil.mask(this.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class HigressServiceProviderImpl implements HigressServiceProvider {
consumerService = new ConsumerServiceImpl(wasmPluginService, wasmPluginInstanceService);
llmProviderService =
new LlmProviderServiceImpl(serviceSourceService, wasmPluginService, wasmPluginInstanceService);
aiRouteService =
new AiRouteServiceImpl(kubernetesModelConverter, kubernetesClientService, routeService, llmProviderService);
aiRouteService = new AiRouteServiceImpl(kubernetesModelConverter, kubernetesClientService, routeService,
llmProviderService, consumerService);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public void saveConfig(LlmProvider provider, Map<String, Object> configurations)
List<String> tokens = provider.getTokens();
if (CollectionUtils.isNotEmpty(tokens)) {
configurations.put(PROVIDER_API_TOKENS_KEY, tokens);
} else {
configurations.remove(PROVIDER_API_TOKENS_KEY);
}

TokenFailoverConfig failoverConfig = provider.getTokenFailoverConfig();
Expand Down
Loading

0 comments on commit 76c6abd

Please sign in to comment.