From 1588da99b5d963f1c17abb398532918091d72106 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 6 Jul 2024 13:15:22 +0800 Subject: [PATCH 1/5] [improve] : Optimize refactoring code, and supplement documentation --- .../common/constants/AiConstants.java | 24 ++--- home/docs/help/ai_config.md | 17 ++++ .../current/help/ai_config.md | 17 ++++ .../manager/controller/AiController.java | 7 +- ...ramDTO.java => OpenAiRequestParamDTO.java} | 4 +- ...imiAiResponse.java => OpenAiResponse.java} | 26 +++++- .../pojo/dto/SparkDeskRequestParamDTO.java | 64 ------------- .../manager/pojo/dto/SparkDeskResponse.java | 93 ------------------- .../manager/pojo/dto/ZhiPuAiResponse.java | 93 ------------------- .../pojo/dto/ZhiPuRequestParamDTO.java | 70 -------------- .../hertzbeat/manager/service/AiService.java | 3 +- .../service/impl/AlibabaAiServiceImpl.java | 71 +++++++------- .../service/impl/KimiAiServiceImpl.java | 55 +++++------ .../service/impl/SparkDeskAiServiceImpl.java | 28 ++---- .../service/impl/ZhiPuServiceImpl.java | 61 +++++------- 15 files changed, 173 insertions(+), 460 deletions(-) rename manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/{KimiAiRequestParamDTO.java => OpenAiRequestParamDTO.java} (97%) rename manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/{KimiAiResponse.java => OpenAiResponse.java} (71%) delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskRequestParamDTO.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskResponse.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAiResponse.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuRequestParamDTO.java diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java b/common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java index f021e5d8211..01bea868355 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java @@ -38,9 +38,9 @@ interface ZhiPuConstants { String REQUEST_ROLE = "user"; /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 3072 */ - Integer MAX_TOKENS = 1024; + Integer MAX_TOKENS = 3072; /** * The sampling temperature, which controls the randomness of the output, must be positive @@ -48,7 +48,7 @@ interface ZhiPuConstants { * the more random and creative the output will be. The smaller the value, the more stable or certain the output will be * You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time */ - double TEMPERATURE = 0.95; + float TEMPERATURE = 0.7f; } @@ -68,9 +68,9 @@ interface AliAiConstants { String REQUEST_ROLE = "user"; /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 3072 */ - Integer MAX_TOKENS = 1024; + Integer MAX_TOKENS = 3072; /** * The sampling temperature, which controls the randomness of the output, must be positive @@ -78,7 +78,7 @@ interface AliAiConstants { * the more random and creative the output will be. The smaller the value, the more stable or certain the output will be * You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time */ - float TEMPERATURE = 0.9f; + float TEMPERATURE = 0.7f; } @@ -99,9 +99,9 @@ interface KimiAiConstants { String REQUEST_ROLE = "user"; /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 3072 */ - Integer MAX_TOKENS = 1024; + Integer MAX_TOKENS = 3072; /** * The sampling temperature, which controls the randomness of the output, must be positive @@ -109,7 +109,7 @@ interface KimiAiConstants { * the more random and creative the output will be. The smaller the value, the more stable or certain the output will be * You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time */ - float TEMPERATURE = 0.9f; + float TEMPERATURE = 0.7f; } @@ -129,9 +129,9 @@ interface SparkDeskConstants { String REQUEST_ROLE = "user"; /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 3072 */ - Integer MAX_TOKENS = 1024; + Integer MAX_TOKENS = 3072; /** * The sampling temperature, which controls the randomness of the output, must be positive @@ -139,7 +139,7 @@ interface SparkDeskConstants { * the more random and creative the output will be. The smaller the value, the more stable or certain the output will be * You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time */ - float TEMPERATURE = 0.95f; + float TEMPERATURE = 0.7f; } diff --git a/home/docs/help/ai_config.md b/home/docs/help/ai_config.md index 7e70ee668c5..ebb263a0a38 100644 --- a/home/docs/help/ai_config.md +++ b/home/docs/help/ai_config.md @@ -41,3 +41,20 @@ keywords: [AI] | model | moonshot-v1-8k、moonshot-v1-32k、moonshot-v1-128k | | | api-key | xxxxxxxxxxx |https://platform.moonshot.cn/console/api-keys| +#### sparkDesk AI +QuickStart: https://www.xfyun.cn/doc/platform/quickguide.html + +| Name of the parameter | Example | Link | +|--------------|-------------------------------------------------|---------------------------------------------------------------| +| type | sparkDesk (must be exactly the same as example) | | +| model | general、generalv2、generalv3、generalv3.5、4.0Ultra | | +| api-key | xxxxxxxxxxx |https://console.xfyun.cn/services/cbm| +| api-secret | xxxxxxxxxxx |https://console.xfyun.cn/services/cbm| + +| sparkDesk version | model | +|-------------------|-----------------------------------------------| +| Spark4.0 Ultra | 4.0Ultra | +| Spark Max |generalv3.5 | +| Spark Pro | generalv3 | +| Spark V2.0 |generalv2 | +| Spark Lite(free) |general | \ No newline at end of file diff --git a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md index 2f74143873f..bf8d7570c59 100644 --- a/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md +++ b/home/i18n/zh-cn/docusaurus-plugin-content-docs/current/help/ai_config.md @@ -41,3 +41,20 @@ keywords: [人工智能 AI] | model | moonshot-v1-8k、moonshot-v1-32k、moonshot-v1-128k | 无 | | api-key | xxxxxxxxxxx | https://platform.moonshot.cn/console/api-keys | +#### 科大讯飞AI +快速入门:https://www.xfyun.cn/doc/platform/quickguide.html + +| 参数名称 | 示例 | 链接 | +|--------------|-------------------------------------------------|---------------------------------------------------------------| +| type | sparkDesk (must be exactly the same as example) | | +| model | general、generalv2、generalv3、generalv3.5、4.0Ultra | | +| api-key | xxxxxxxxxxx |https://console.xfyun.cn/services/cbm| +| api-secret | xxxxxxxxxxx |https://console.xfyun.cn/services/cbm| + +| 模型版本 | 模型类型(application.yml的model参数) | +|-----------------|-------------------------------| +| Spark4.0 Ultra | 4.0Ultra | +| Spark Max | generalv3.5 | +| Spark Pro | generalv3 | +| Spark V2.0 | generalv2 | +| Spark Lite(免费版) | general | \ No newline at end of file diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java index 0e456260892..2de31b0658c 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java @@ -23,6 +23,7 @@ import org.apache.hertzbeat.manager.service.impl.AiServiceFactoryImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -44,7 +45,7 @@ public class AiController { @Autowired private AiServiceFactoryImpl aiServiceFactory; - @Value("${aiConfig.type:0}") + @Value("${aiConfig.type:zhiPu}") private String type; /** @@ -54,8 +55,8 @@ public class AiController { * @return AI response */ @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE}) - public Flux requestAi(@RequestParam("text") String text, - @RequestParam(value = "type", required = false) String currentlyDisabledType) { + public Flux> requestAi(@RequestParam("text") String text, + @RequestParam(value = "type", required = false) String currentlyDisabledType) { AiService aiServiceImplBean = aiServiceFactory.getAiServiceImplBean(type); return aiServiceImplBean.requestAi(text); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiRequestParamDTO.java similarity index 97% rename from manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiRequestParamDTO.java rename to manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiRequestParamDTO.java index 94b442bc4ea..7c51e1abec0 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiRequestParamDTO.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiRequestParamDTO.java @@ -25,14 +25,14 @@ import lombok.NoArgsConstructor; /** - * Kimi Request param + * openAi Request param */ @Data @AllArgsConstructor @NoArgsConstructor @Builder -public class KimiAiRequestParamDTO { +public class OpenAiRequestParamDTO { /** * ai version diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java similarity index 71% rename from manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiResponse.java rename to manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java index a4795ad89e0..0fb463379be 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiResponse.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java @@ -17,19 +17,24 @@ package org.apache.hertzbeat.manager.pojo.dto; +import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; +import java.util.Objects; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.codec.ServerSentEvent; /** - * Kimi AiResponse + * openAi Response */ @Data @AllArgsConstructor @NoArgsConstructor -public class KimiAiResponse { +@Slf4j +public class OpenAiResponse { /** * Task order number generated by the AI open platform. Use this order number when invoking the request result interface @@ -89,5 +94,22 @@ public class Tokens { private Integer totalTokens; } + public static ServerSentEvent convertToResponse(String aiRes) { + try { + OpenAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, OpenAiResponse.class); + if (Objects.nonNull(zhiPuAiResponse)) { + OpenAiResponse.Choice choice = zhiPuAiResponse.getChoices().get(0); + String content = choice.getDelta().getContent(); + return ServerSentEvent.builder() + .data(content) + .build(); + } + } catch (Exception e) { + log.info("convertToResponse Exception:{}", e.toString()); + throw e; + } + + return ServerSentEvent.builder().build(); + } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskRequestParamDTO.java deleted file mode 100644 index 9123ae800c7..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskRequestParamDTO.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.pojo.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.List; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * SparkDeskRequestParamDTO - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -@Builder -public class SparkDeskRequestParamDTO { - - /** - * ai version - */ - private String model; - - /** - * request message - */ - private List messages; - - /** - * The sampling temperature, which controls the randomness of the output, must be positive - * The value ranges from 0.0 to 1.0, and cannot be equal to 0. The default value is 0.95. - * The larger the value, the more random and creative the output will be. The smaller the value, the more stable or certain the output will be - * You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time - */ - private float temperature; - - /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 - */ - @JsonProperty("max_tokens") - private Integer maxTokens; - - /** - * stream response - */ - private Boolean stream = Boolean.FALSE; -} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskResponse.java deleted file mode 100644 index c5b84e87c6d..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SparkDeskResponse.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.pojo.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.List; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * SparkDeskResponse - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -public class SparkDeskResponse { - - /** - * Task order number generated by the AI open platform. Use this order number when invoking the request result interface - */ - private String id; - - /** - * The request creation time is a Unix timestamp in seconds - */ - private Long created; - - /** - * response message - */ - private List choices; - - /** - * Returns the number of tokens invoked by the model at the end. - */ - private Tokens usage; - - /** - * Choice - */ - @Data - @AllArgsConstructor - @NoArgsConstructor - public class Choice { - private int index; - private AiMessage delta; - } - - /** - * Tokens - */ - @Data - @AllArgsConstructor - @NoArgsConstructor - public class Tokens { - - /** - * The number of tokens entered by users - */ - @JsonProperty("prompt_tokens") - private Integer promptTokens; - - /** - * The number of tokens that the model outputs - */ - @JsonProperty("completion_tokens") - private Integer completionTokens; - - /** - * Total number of tokens - */ - @JsonProperty("total_tokens") - private Integer totalTokens; - } - -} - diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAiResponse.java deleted file mode 100644 index 2b64384c526..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAiResponse.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.pojo.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.List; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * ZhiPuAiResponse - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -public class ZhiPuAiResponse { - - /** - * Task order number generated by the AI open platform. Use this order number when invoking the request result interface - */ - private String id; - - /** - * The request creation time is a Unix timestamp in seconds - */ - private Long created; - - /** - * response message - */ - private List choices; - - /** - * Returns the number of tokens invoked by the model at the end. - */ - private Tokens usage; - - /** - * Choice - */ - @Data - @AllArgsConstructor - @NoArgsConstructor - public class Choice { - private int index; - private AiMessage delta; - } - - /** - * Tokens - */ - @Data - @AllArgsConstructor - @NoArgsConstructor - public class Tokens { - - /** - * The number of tokens entered by users - */ - @JsonProperty("prompt_tokens") - private Integer promptTokens; - - /** - * The number of tokens that the model outputs - */ - @JsonProperty("completion_tokens") - private Integer completionTokens; - - /** - * Total number of tokens - */ - @JsonProperty("total_tokens") - private Integer totalTokens; - } - -} - diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuRequestParamDTO.java deleted file mode 100644 index 3c85b16fd9e..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuRequestParamDTO.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.hertzbeat.manager.pojo.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.List; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - - -/** - * ZhiPu Request param - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -@Builder -public class ZhiPuRequestParamDTO { - - - /** - * ai version - */ - private String model; - - /** - * request message - */ - private List messages; - - /** - * The sampling temperature, which controls the randomness of the output, must be positive - * The value ranges from 0.0 to 1.0, and cannot be equal to 0. The default value is 0.95. - * The larger the value, the more random and creative the output will be. The smaller the value, the more stable or certain the output will be - * You are advised to adjust top_p or temperature parameters based on application scenarios, but do not adjust the two parameters at the same time - */ - private double temperature; - - /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 - */ - @JsonProperty("max_tokens") - private Integer maxTokens; - - /** - * stream response - */ - private Boolean stream = Boolean.FALSE; - -} - - - diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/AiService.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/AiService.java index ad2292ac774..22ccd55d799 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/AiService.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/AiService.java @@ -19,6 +19,7 @@ import org.apache.hertzbeat.common.constants.AiTypeEnum; +import org.springframework.http.codec.ServerSentEvent; import reactor.core.publisher.Flux; @@ -38,6 +39,6 @@ public interface AiService { * @param text text * @return AI response */ - Flux requestAi(String text); + Flux> requestAi(String text); } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java index 06e79a9925e..fe5bd613d1a 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -74,39 +75,47 @@ public AiTypeEnum getType() { } @Override - public Flux requestAi(String text) { + public Flux> requestAi(String text) { checkParam(text, apiKey); - - AliAiRequestParamDTO aliAiRequestParamDTO = AliAiRequestParamDTO.builder() - .model(model) - .input(AliAiRequestParamDTO.Input.builder() - .messages(List.of(new AiMessage(AiConstants.AliAiConstants.REQUEST_ROLE, text))) - .build()) - .parameters(AliAiRequestParamDTO.Parameters.builder() - .maxTokens(AiConstants.AliAiConstants.MAX_TOKENS) - .temperature(AiConstants.AliAiConstants.TEMPERATURE) - .enableSearch(true) - .resultFormat("message") - .incrementalOutput(true) - .build()) - .build(); - - - return webClient.post() - .body(BodyInserters.fromValue(aliAiRequestParamDTO)) - .retrieve() - .bodyToFlux(AliAiResponse.class) - .map(aliAiResponse -> { - if (Objects.nonNull(aliAiResponse)) { - List choices = aliAiResponse.getOutput().getChoices(); - if (CollectionUtils.isEmpty(choices)) { - return ""; + try { + AliAiRequestParamDTO aliAiRequestParamDTO = AliAiRequestParamDTO.builder() + .model(model) + .input(AliAiRequestParamDTO.Input.builder() + .messages(List.of(new AiMessage(AiConstants.AliAiConstants.REQUEST_ROLE, text))) + .build()) + .parameters(AliAiRequestParamDTO.Parameters.builder() + .maxTokens(AiConstants.AliAiConstants.MAX_TOKENS) + .temperature(AiConstants.AliAiConstants.TEMPERATURE) + .enableSearch(true) + .resultFormat("message") + .incrementalOutput(true) + .build()) + .build(); + + + return webClient.post() + .body(BodyInserters.fromValue(aliAiRequestParamDTO)) + .retrieve() + .bodyToFlux(AliAiResponse.class) + .map(aliAiResponse -> { + if (Objects.nonNull(aliAiResponse)) { + List choices = aliAiResponse.getOutput().getChoices(); + if (CollectionUtils.isEmpty(choices)) { + return ServerSentEvent.builder().build(); + } + String content = choices.get(0).getMessage().getContent(); + return ServerSentEvent.builder() + .data(content) + .build(); } - return choices.get(0).getMessage().getContent(); - } - return ""; - }) - .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); + return ServerSentEvent.builder().build(); + }) + .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); + + } catch (Exception e) { + log.info("KimiAiServiceImpl.requestAi exception:{}", e.toString()); + throw e; + } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java index a9fc8746fa7..21c5e0a9a20 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java @@ -17,20 +17,19 @@ package org.apache.hertzbeat.manager.service.impl; -import com.alibaba.fastjson.JSON; import java.util.List; -import java.util.Objects; import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.common.constants.AiConstants; import org.apache.hertzbeat.common.constants.AiTypeEnum; import org.apache.hertzbeat.manager.pojo.dto.AiMessage; -import org.apache.hertzbeat.manager.pojo.dto.KimiAiRequestParamDTO; -import org.apache.hertzbeat.manager.pojo.dto.KimiAiResponse; +import org.apache.hertzbeat.manager.pojo.dto.OpenAiRequestParamDTO; +import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.web.reactive.function.BodyInserters; @@ -71,39 +70,33 @@ public AiTypeEnum getType() { } @Override - public Flux requestAi(String text) { - - checkParam(text, apiKey); - KimiAiRequestParamDTO zhiPuRequestParamDTO = KimiAiRequestParamDTO.builder() - .model(model) - .stream(Boolean.TRUE) - .maxTokens(AiConstants.KimiAiConstants.MAX_TOKENS) - .temperature(AiConstants.KimiAiConstants.TEMPERATURE) - .messages(List.of(new AiMessage(AiConstants.KimiAiConstants.REQUEST_ROLE, text))) - .build(); + public Flux> requestAi(String text) { + try { + checkParam(text, apiKey); + OpenAiRequestParamDTO zhiPuRequestParamDTO = OpenAiRequestParamDTO.builder() + .model(model) + .stream(Boolean.TRUE) + .maxTokens(AiConstants.KimiAiConstants.MAX_TOKENS) + .temperature(AiConstants.KimiAiConstants.TEMPERATURE) + .messages(List.of(new AiMessage(AiConstants.KimiAiConstants.REQUEST_ROLE, text))) + .build(); - return webClient.post() - .body(BodyInserters.fromValue(zhiPuRequestParamDTO)) - .retrieve() - .bodyToFlux(String.class) - .filter(aiResponse -> !"[DONE]".equals(aiResponse)) - .map(this::convertToResponse) - .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); + return webClient.post() + .body(BodyInserters.fromValue(zhiPuRequestParamDTO)) + .retrieve() + .bodyToFlux(String.class) + .filter(aiResponse -> !"[DONE]".equals(aiResponse)) + .map(OpenAiResponse::convertToResponse) + .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); - } - private String convertToResponse(String aiRes) { - try { - KimiAiResponse kimiAiResponse = JSON.parseObject(aiRes, KimiAiResponse.class); - if (Objects.nonNull(kimiAiResponse)) { - KimiAiResponse.Choice choice = kimiAiResponse.getChoices().get(0); - return choice.getDelta().getContent(); - } } catch (Exception e) { - log.info("convertToResponse Exception:{}", e.toString()); + log.info("KimiAiServiceImpl.requestAi exception:{}", e.toString()); + throw e; } - return ""; + + } private void checkParam(String param, String apiKey) { diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java index 88a240884d3..55e22b3370f 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java @@ -17,20 +17,19 @@ package org.apache.hertzbeat.manager.service.impl; -import com.alibaba.fastjson.JSON; import java.util.List; -import java.util.Objects; import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.common.constants.AiConstants; import org.apache.hertzbeat.common.constants.AiTypeEnum; import org.apache.hertzbeat.manager.pojo.dto.AiMessage; -import org.apache.hertzbeat.manager.pojo.dto.SparkDeskRequestParamDTO; -import org.apache.hertzbeat.manager.pojo.dto.SparkDeskResponse; +import org.apache.hertzbeat.manager.pojo.dto.OpenAiRequestParamDTO; +import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.web.reactive.function.BodyInserters; @@ -76,11 +75,11 @@ public AiTypeEnum getType() { } @Override - public Flux requestAi(String text) { + public Flux> requestAi(String text) { try { checkParam(text, apiKey); - SparkDeskRequestParamDTO zhiPuRequestParamDTO = SparkDeskRequestParamDTO.builder() + OpenAiRequestParamDTO zhiPuRequestParamDTO = OpenAiRequestParamDTO.builder() .model(model) //sse .stream(Boolean.TRUE) @@ -94,28 +93,13 @@ public Flux requestAi(String text) { .retrieve() .bodyToFlux(String.class) .filter(aiResponse -> !"[DONE]".equals(aiResponse)) - .map(this::convertToResponse); + .map(OpenAiResponse::convertToResponse); } catch (Exception e) { log.info("SparkDeskAiServiceImpl.requestAi exception:{}", e.toString()); throw e; } } - private String convertToResponse(String aiRes) { - try { - SparkDeskResponse sparkDeskResponse = JSON.parseObject(aiRes, SparkDeskResponse.class); - if (Objects.nonNull(sparkDeskResponse)) { - SparkDeskResponse.Choice choice = sparkDeskResponse.getChoices().get(0); - return choice.getDelta().getContent(); - } - } catch (Exception e) { - log.info("convertToResponse Exception:{}", e.toString()); - } - - return ""; - } - - private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); Assert.notNull(apiKey, "aiConfig.api-key is null"); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java index d56fdb071b5..93c8d09ca70 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java @@ -17,20 +17,20 @@ package org.apache.hertzbeat.manager.service.impl; -import com.alibaba.fastjson.JSON; + import java.util.List; -import java.util.Objects; import javax.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.common.constants.AiConstants; import org.apache.hertzbeat.common.constants.AiTypeEnum; import org.apache.hertzbeat.manager.pojo.dto.AiMessage; -import org.apache.hertzbeat.manager.pojo.dto.ZhiPuAiResponse; -import org.apache.hertzbeat.manager.pojo.dto.ZhiPuRequestParamDTO; +import org.apache.hertzbeat.manager.pojo.dto.OpenAiRequestParamDTO; +import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.stereotype.Service; import org.springframework.util.Assert; import org.springframework.web.reactive.function.BodyInserters; @@ -71,43 +71,32 @@ public AiTypeEnum getType() { } @Override - public Flux requestAi(String text) { - checkParam(text, model, apiKey); - ZhiPuRequestParamDTO zhiPuRequestParamDTO = ZhiPuRequestParamDTO.builder() - .model(model) - //sse - .stream(Boolean.TRUE) - .maxTokens(AiConstants.ZhiPuConstants.MAX_TOKENS) - .temperature(AiConstants.ZhiPuConstants.TEMPERATURE) - .messages(List.of(new AiMessage(AiConstants.ZhiPuConstants.REQUEST_ROLE, text))) - .build(); - - return webClient.post() - .body(BodyInserters.fromValue(zhiPuRequestParamDTO)) - .retrieve() - .bodyToFlux(String.class) - .filter(aiResponse -> !"[DONE]".equals(aiResponse)) - .map(this::convertToResponse) - .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); - - - } - - private String convertToResponse(String aiRes) { + public Flux> requestAi(String text) { try { - ZhiPuAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, ZhiPuAiResponse.class); - if (Objects.nonNull(zhiPuAiResponse)) { - ZhiPuAiResponse.Choice choice = zhiPuAiResponse.getChoices().get(0); - return choice.getDelta().getContent(); - } + checkParam(text, model, apiKey); + OpenAiRequestParamDTO zhiPuRequestParamDTO = OpenAiRequestParamDTO.builder() + .model(model) + //sse + .stream(Boolean.TRUE) + .maxTokens(AiConstants.ZhiPuConstants.MAX_TOKENS) + .temperature(AiConstants.ZhiPuConstants.TEMPERATURE) + .messages(List.of(new AiMessage(AiConstants.ZhiPuConstants.REQUEST_ROLE, text))) + .build(); + + return webClient.post() + .body(BodyInserters.fromValue(zhiPuRequestParamDTO)) + .retrieve() + .bodyToFlux(String.class) + .filter(aiResponse -> !"[DONE]".equals(aiResponse)) + .map(OpenAiResponse::convertToResponse) + .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); + } catch (Exception e) { - log.info("convertToResponse Exception:{}", e.toString()); + log.info("ZhiPuServiceImpl.requestAi exception:{}", e.toString()); + throw e; } - - return ""; } - private void checkParam(String param, String model, String apiKey) { Assert.notNull(param, "text is null"); Assert.notNull(apiKey, "aiConfig.api-key is null"); From 9b5c1e969c7661cbb9e48ad716f9c0db3b79956d Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 6 Jul 2024 16:31:20 +0800 Subject: [PATCH 2/5] [improve] : Optimize refactoring code, and supplement documentation --- .../apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java index 0fb463379be..3c97739f253 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/OpenAiResponse.java @@ -96,9 +96,9 @@ public class Tokens { public static ServerSentEvent convertToResponse(String aiRes) { try { - OpenAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, OpenAiResponse.class); - if (Objects.nonNull(zhiPuAiResponse)) { - OpenAiResponse.Choice choice = zhiPuAiResponse.getChoices().get(0); + OpenAiResponse openAiResponse = JSON.parseObject(aiRes, OpenAiResponse.class); + if (Objects.nonNull(openAiResponse)) { + OpenAiResponse.Choice choice = openAiResponse.getChoices().get(0); String content = choice.getDelta().getContent(); return ServerSentEvent.builder() .data(content) From c6c9c61c9bc134b617dbae1a4c74dc662cae457d Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 7 Jul 2024 22:41:14 +0800 Subject: [PATCH 3/5] [improve] : Delete Chinese comments, modify word errors, add Operation comments --- .../hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java | 2 +- .../hertzbeat/manager/service/impl/KimiAiServiceImpl.java | 2 +- .../manager/service/impl/SparkDeskAiServiceImpl.java | 4 ++-- .../hertzbeat/manager/service/impl/ZhiPuServiceImpl.java | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java index fe5bd613d1a..8505c6fa466 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java @@ -49,7 +49,7 @@ public class AlibabaAiServiceImpl implements AiService { @Value("${aiConfig.model:qwen-turbo}") private String model; - @Value("${aiConfig.api-key}") + @Value("${aiConfig.api-key:default}") private String apiKey; diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java index 21c5e0a9a20..0cb4507716b 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java @@ -47,7 +47,7 @@ public class KimiAiServiceImpl implements AiService { @Value("${aiConfig.model:moonshot-v1-8k}") private String model; - @Value("${aiConfig.api-key}") + @Value("${aiConfig.api-key:default}") private String apiKey; private WebClient webClient; diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java index 55e22b3370f..ca522514c46 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java @@ -48,9 +48,9 @@ public class SparkDeskAiServiceImpl implements AiService { @Value("${aiConfig.model:generalv3.5}") private String model; - @Value("${aiConfig.api-key}") + @Value("${aiConfig.api-key:default}") private String apiKey; - @Value("${aiConfig.api-secret}") + @Value("${aiConfig.api-secret:default}") private String apiSecret; private WebClient webClient; diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java index 93c8d09ca70..a50182f1bf4 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java @@ -47,7 +47,7 @@ public class ZhiPuServiceImpl implements AiService { @Value("${aiConfig.model:glm-4}") private String model; - @Value("${aiConfig.api-key}") + @Value("${aiConfig.api-key:default}") private String apiKey; private WebClient webClient; From 7e36ca6fe554f731afee536b7adf671e9c69e33f Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 7 Jul 2024 22:59:59 +0800 Subject: [PATCH 4/5] [improve] : Delete Chinese comments, modify word errors, add Operation comments --- .../hertzbeat/manager/controller/AiController.java | 2 +- .../manager/service/impl/AlibabaAiServiceImpl.java | 8 +++++--- .../manager/service/impl/KimiAiServiceImpl.java | 8 +++++--- .../manager/service/impl/SparkDeskAiServiceImpl.java | 10 ++++++---- .../manager/service/impl/ZhiPuServiceImpl.java | 8 +++++--- manager/src/main/resources/application.yml | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java index 2de31b0658c..24b03b37245 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java @@ -45,7 +45,7 @@ public class AiController { @Autowired private AiServiceFactoryImpl aiServiceFactory; - @Value("${aiConfig.type:zhiPu}") + @Value("${ai.type:zhiPu}") private String type; /** diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java index 8505c6fa466..07b4881ca9e 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java @@ -28,6 +28,7 @@ import org.apache.hertzbeat.manager.pojo.dto.AliAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -44,12 +45,13 @@ * alibaba Ai */ @Service("AlibabaAiServiceImpl") +@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false) @Slf4j public class AlibabaAiServiceImpl implements AiService { - @Value("${aiConfig.model:qwen-turbo}") + @Value("${ai.model:qwen-turbo}") private String model; - @Value("${aiConfig.api-key:default}") + @Value("${ai.api-key}") private String apiKey; @@ -122,6 +124,6 @@ public Flux> requestAi(String text) { private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "aiConfig.api-key is null"); + Assert.notNull(apiKey, "ai.api-key is null"); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java index 0cb4507716b..f4efc1f6b1e 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java @@ -27,6 +27,7 @@ import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -41,13 +42,14 @@ * Kimi Ai */ @Service("KimiAiServiceImpl") +@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false) @Slf4j public class KimiAiServiceImpl implements AiService { - @Value("${aiConfig.model:moonshot-v1-8k}") + @Value("${ai.model:moonshot-v1-8k}") private String model; - @Value("${aiConfig.api-key:default}") + @Value("${ai.api-key}") private String apiKey; private WebClient webClient; @@ -101,6 +103,6 @@ public Flux> requestAi(String text) { private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "aiConfig.api-key is null"); + Assert.notNull(apiKey, "ai.api-key is null"); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java index ca522514c46..0ae2fb88e8a 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java @@ -27,6 +27,7 @@ import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -42,15 +43,16 @@ * sparkDesk AI */ @Service("SparkDeskAiServiceImpl") +@ConditionalOnProperty(prefix = "ai",name = {"api-key", "api-secret"}, matchIfMissing = false) @Slf4j public class SparkDeskAiServiceImpl implements AiService { - @Value("${aiConfig.model:generalv3.5}") + @Value("${ai.model:generalv3.5}") private String model; - @Value("${aiConfig.api-key:default}") + @Value("${ai.api-key:default}") private String apiKey; - @Value("${aiConfig.api-secret:default}") + @Value("${ai.api-secret}") private String apiSecret; private WebClient webClient; @@ -102,6 +104,6 @@ public Flux> requestAi(String text) { private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "aiConfig.api-key is null"); + Assert.notNull(apiKey, "ai.api-key is null"); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java index a50182f1bf4..b2dbfa1fa44 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java @@ -28,6 +28,7 @@ import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -43,11 +44,12 @@ * ZhiPu AI */ @Service("ZhiPuServiceImpl") +@ConditionalOnProperty(prefix = "ai",name = "api-key", matchIfMissing = false) @Slf4j public class ZhiPuServiceImpl implements AiService { - @Value("${aiConfig.model:glm-4}") + @Value("${ai.model:glm-4}") private String model; - @Value("${aiConfig.api-key:default}") + @Value("${ai.api-key}") private String apiKey; private WebClient webClient; @@ -99,7 +101,7 @@ public Flux> requestAi(String text) { private void checkParam(String param, String model, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "aiConfig.api-key is null"); + Assert.notNull(apiKey, "ai.api-key is null"); } diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index 6feb9575dc4..146faa1590b 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -198,7 +198,7 @@ scheduler: # AI config # See the documentation for details : https://hertzbeat.apache.org/zh-cn/docs/help/aiConfig -aiConfig: +ai: # AI Type:zhiPu、alibabaAi、kimiAi、sparkDesk type: # Model name:glm-4、qwen-turboo、moonshot-v1-8k、generalv3.5 From 64efdbe8480345b37946992488517797f79e714b Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Mon, 8 Jul 2024 11:56:10 +0800 Subject: [PATCH 5/5] [improve] : Optimize refactoring code, and supplement documentation --- .../hertzbeat/manager/controller/AiController.java | 2 +- .../manager/service/impl/AlibabaAiServiceImpl.java | 8 +++----- .../manager/service/impl/KimiAiServiceImpl.java | 8 +++----- .../manager/service/impl/SparkDeskAiServiceImpl.java | 10 ++++------ .../manager/service/impl/ZhiPuServiceImpl.java | 8 +++----- manager/src/main/resources/application.yml | 2 +- 6 files changed, 15 insertions(+), 23 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java index 24b03b37245..2de31b0658c 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java @@ -45,7 +45,7 @@ public class AiController { @Autowired private AiServiceFactoryImpl aiServiceFactory; - @Value("${ai.type:zhiPu}") + @Value("${aiConfig.type:zhiPu}") private String type; /** diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java index 07b4881ca9e..fe5bd613d1a 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java @@ -28,7 +28,6 @@ import org.apache.hertzbeat.manager.pojo.dto.AliAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -45,13 +44,12 @@ * alibaba Ai */ @Service("AlibabaAiServiceImpl") -@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false) @Slf4j public class AlibabaAiServiceImpl implements AiService { - @Value("${ai.model:qwen-turbo}") + @Value("${aiConfig.model:qwen-turbo}") private String model; - @Value("${ai.api-key}") + @Value("${aiConfig.api-key}") private String apiKey; @@ -124,6 +122,6 @@ public Flux> requestAi(String text) { private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "ai.api-key is null"); + Assert.notNull(apiKey, "aiConfig.api-key is null"); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java index f4efc1f6b1e..21c5e0a9a20 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/KimiAiServiceImpl.java @@ -27,7 +27,6 @@ import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -42,14 +41,13 @@ * Kimi Ai */ @Service("KimiAiServiceImpl") -@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false) @Slf4j public class KimiAiServiceImpl implements AiService { - @Value("${ai.model:moonshot-v1-8k}") + @Value("${aiConfig.model:moonshot-v1-8k}") private String model; - @Value("${ai.api-key}") + @Value("${aiConfig.api-key}") private String apiKey; private WebClient webClient; @@ -103,6 +101,6 @@ public Flux> requestAi(String text) { private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "ai.api-key is null"); + Assert.notNull(apiKey, "aiConfig.api-key is null"); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java index 0ae2fb88e8a..d286304586f 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/SparkDeskAiServiceImpl.java @@ -27,7 +27,6 @@ import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -43,16 +42,15 @@ * sparkDesk AI */ @Service("SparkDeskAiServiceImpl") -@ConditionalOnProperty(prefix = "ai",name = {"api-key", "api-secret"}, matchIfMissing = false) @Slf4j public class SparkDeskAiServiceImpl implements AiService { - @Value("${ai.model:generalv3.5}") + @Value("${aiConfig.model:generalv3.5}") private String model; - @Value("${ai.api-key:default}") + @Value("${aiConfig.api-key:default}") private String apiKey; - @Value("${ai.api-secret}") + @Value("${aiConfig.api-secret}") private String apiSecret; private WebClient webClient; @@ -104,6 +102,6 @@ public Flux> requestAi(String text) { private void checkParam(String param, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "ai.api-key is null"); + Assert.notNull(apiKey, "aiConfig.api-key is null"); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java index b2dbfa1fa44..93c8d09ca70 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java @@ -28,7 +28,6 @@ import org.apache.hertzbeat.manager.pojo.dto.OpenAiResponse; import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; @@ -44,12 +43,11 @@ * ZhiPu AI */ @Service("ZhiPuServiceImpl") -@ConditionalOnProperty(prefix = "ai",name = "api-key", matchIfMissing = false) @Slf4j public class ZhiPuServiceImpl implements AiService { - @Value("${ai.model:glm-4}") + @Value("${aiConfig.model:glm-4}") private String model; - @Value("${ai.api-key}") + @Value("${aiConfig.api-key}") private String apiKey; private WebClient webClient; @@ -101,7 +99,7 @@ public Flux> requestAi(String text) { private void checkParam(String param, String model, String apiKey) { Assert.notNull(param, "text is null"); - Assert.notNull(apiKey, "ai.api-key is null"); + Assert.notNull(apiKey, "aiConfig.api-key is null"); } diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index 146faa1590b..6feb9575dc4 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -198,7 +198,7 @@ scheduler: # AI config # See the documentation for details : https://hertzbeat.apache.org/zh-cn/docs/help/aiConfig -ai: +aiConfig: # AI Type:zhiPu、alibabaAi、kimiAi、sparkDesk type: # Model name:glm-4、qwen-turboo、moonshot-v1-8k、generalv3.5