This is an automated email from the ASF dual-hosted git repository.

gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/master by this push:
     new 823430bca [improve] : Modified loading application.yml (#2256)
823430bca is described below

commit 823430bca97ebb2d9d2c62f9f3742051f9b4c694
Author: linDong <[email protected]>
AuthorDate: Sun Jul 14 23:04:35 2024 +0800

    [improve] : Modified loading application.yml (#2256)
    
    Co-authored-by: tomsun28 <[email protected]>
---
 .../hertzbeat/manager/config/AiProperties.java     | 54 ++++++++++++++++++++++
 .../hertzbeat/manager/controller/AiController.java | 19 ++++----
 .../manager/service/impl/AiServiceFactoryImpl.java | 10 +++-
 .../manager/service/impl/AlibabaAiServiceImpl.java | 21 ++++-----
 .../manager/service/impl/KimiAiServiceImpl.java    | 21 ++++-----
 .../service/impl/SparkDeskAiServiceImpl.java       | 25 +++++-----
 .../manager/service/impl/ZhiPuServiceImpl.java     | 19 ++++----
 manager/src/main/resources/application-test.yml    | 17 +++----
 8 files changed, 121 insertions(+), 65 deletions(-)

diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java
new file mode 100644
index 000000000..a3a1040ff
--- /dev/null
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/config/AiProperties.java
@@ -0,0 +1,54 @@
+/*
+ * 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.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * AiProperties
+ */
+@Configuration
+@ConfigurationProperties(prefix = "ai")
+@Data
+public class AiProperties {
+
+    /**
+     * AI Type: zhiPu, alibabaAi, kimiAi, sparkDesk
+     */
+    private String type;
+
+    /**
+     * Model name: glm-4, qwen-turboo, moonshot-v1-8k, generalv3.5
+     */
+    private String model;
+
+    /**
+     * API key
+     */
+    private String apiKey;
+
+    /**
+     * At present, only IFLYTEK large model needs to be filled in
+     */
+    private String apiSecret;
+
+
+
+}
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 eed6d631a..9c2c3fd7c 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
@@ -21,11 +21,12 @@ import static 
org.springframework.http.MediaType.TEXT_EVENT_STREAM_VALUE;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
 import io.swagger.v3.oas.annotations.tags.Tag;
+import org.apache.hertzbeat.manager.config.AiProperties;
 import org.apache.hertzbeat.manager.service.AiService;
 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.util.Assert;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -44,28 +45,24 @@ public class AiController {
     /**
      * AI beanFactory
      */
-    @Autowired
+    @Autowired(required = false)
     private AiServiceFactoryImpl aiServiceFactory;
 
-    /**
-     * Types of artificial intelligence
-     */
-    @Value("${ai.type:zhiPu}")
-    private String type;
+    @Autowired
+    private AiProperties aiProperties;
 
     /**
      * request AI
      * @param text                  request text
-     * @param currentlyDisabledType Currently disabled, later released
      * @return                      AI response
      */
     @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE})
     @Operation(summary = "Artificial intelligence questions and Answers",
             description = "Artificial intelligence questions and Answers")
-    public Flux<ServerSentEvent<String>> requestAi(@Parameter(description = 
"Request text", example = "Who are you") @RequestParam("text") String text,
-                                  @Parameter(description = "Types of 
artificial intelligence", example = "zhiPu") @RequestParam(value = "type", 
required = false) String currentlyDisabledType)  {
+    public Flux<ServerSentEvent<String>> requestAi(@Parameter(description = 
"Request text", example = "Who are you") @RequestParam("text") String text)  {
 
-        AiService aiServiceImplBean = 
aiServiceFactory.getAiServiceImplBean(type);
+        Assert.notNull(aiServiceFactory, "please check that your type value is 
consistent with the documentation on the website");
+        AiService aiServiceImplBean = 
aiServiceFactory.getAiServiceImplBean(aiProperties.getType());
 
         return aiServiceImplBean.requestAi(text);
     }
diff --git 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AiServiceFactoryImpl.java
 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AiServiceFactoryImpl.java
index 55a752f2f..8a18e0ce1 100644
--- 
a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AiServiceFactoryImpl.java
+++ 
b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AiServiceFactoryImpl.java
@@ -26,22 +26,28 @@ import javax.annotation.PostConstruct;
 import org.apache.hertzbeat.common.constants.AiTypeEnum;
 import org.apache.hertzbeat.manager.service.AiService;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Component;
 import org.springframework.util.Assert;
+import org.springframework.util.CollectionUtils;
 
 /**
  * AI bean factory
  */
 @Component
+@ConditionalOnProperty(prefix = "ai", name = "type")
 public class AiServiceFactoryImpl {
 
-    @Autowired
+    @Autowired(required = false)
     private List<AiService> aiService;
 
     private Map<AiTypeEnum, AiService> aiServiceFactoryMap = new HashMap<>();
 
     @PostConstruct
     public void init() {
+        if (CollectionUtils.isEmpty(aiService)) {
+            return;
+        }
         aiServiceFactoryMap = aiService.stream()
                 .collect(Collectors.toMap(AiService::getType, 
Function.identity()));
     }
@@ -49,7 +55,7 @@ public class AiServiceFactoryImpl {
     public AiService getAiServiceImplBean(String type) {
         Assert.notNull(type, "type is null");
         AiTypeEnum typeByName = AiTypeEnum.getTypeByName(type);
-        Assert.notNull(typeByName, "The current type is not supported");
+        Assert.notNull(typeByName, "The current type is not supported,please 
check that your type value is consistent with the documentation on the 
website");
         AiService aiServiceImpl = aiServiceFactoryMap.get(typeByName);
         Assert.notNull(aiServiceImpl, "No bean for current type found");
         return aiServiceImpl;
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 07b4881ca..55dce71c6 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
@@ -23,11 +23,12 @@ 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.config.AiProperties;
 import org.apache.hertzbeat.manager.pojo.dto.AiMessage;
 import org.apache.hertzbeat.manager.pojo.dto.AliAiRequestParamDTO;
 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.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -45,15 +46,12 @@ import reactor.core.publisher.Flux;
  * alibaba Ai
  */
 @Service("AlibabaAiServiceImpl")
-@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false)
+@ConditionalOnProperty(prefix = "ai", name = "type", havingValue = "alibabaAi")
 @Slf4j
 public class AlibabaAiServiceImpl implements AiService {
 
-    @Value("${ai.model:qwen-turbo}")
-    private String model;
-    @Value("${ai.api-key}")
-    private String apiKey;
-
+    @Autowired
+    private AiProperties aiProperties;
 
     private WebClient webClient;
 
@@ -62,7 +60,7 @@ public class AlibabaAiServiceImpl implements AiService {
         this.webClient = WebClient.builder()
                 .baseUrl(AiConstants.AliAiConstants.URL)
                 .defaultHeader(HttpHeaders.CONTENT_TYPE, 
MediaType.APPLICATION_JSON_VALUE)
-                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
+                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + 
aiProperties.getApiKey())
                 //sse
                 .defaultHeader(HttpHeaders.ACCEPT, 
MediaType.TEXT_EVENT_STREAM_VALUE)
                 .exchangeStrategies(ExchangeStrategies.builder()
@@ -78,10 +76,10 @@ public class AlibabaAiServiceImpl implements AiService {
 
     @Override
     public Flux<ServerSentEvent<String>> requestAi(String text) {
-        checkParam(text, apiKey);
+        checkParam(text, aiProperties.getModel(), aiProperties.getApiKey());
         try {
             AliAiRequestParamDTO aliAiRequestParamDTO = 
AliAiRequestParamDTO.builder()
-                    .model(model)
+                    .model(aiProperties.getModel())
                     .input(AliAiRequestParamDTO.Input.builder()
                             .messages(List.of(new 
AiMessage(AiConstants.AliAiConstants.REQUEST_ROLE, text)))
                             .build())
@@ -122,8 +120,9 @@ public class AlibabaAiServiceImpl implements AiService {
     }
 
 
-    private void checkParam(String param, String apiKey) {
+    private void checkParam(String param, String apiKey, String model) {
         Assert.notNull(param, "text is null");
+        Assert.notNull(param, "model 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 f4efc1f6b..29c46fa71 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
@@ -22,11 +22,12 @@ 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.config.AiProperties;
 import org.apache.hertzbeat.manager.pojo.dto.AiMessage;
 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.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -42,15 +43,12 @@ import reactor.core.publisher.Flux;
  * Kimi Ai
  */
 @Service("KimiAiServiceImpl")
-@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false)
+@ConditionalOnProperty(prefix = "ai", name = "type", havingValue = "kimiAi")
 @Slf4j
 public class KimiAiServiceImpl implements AiService {
 
-    @Value("${ai.model:moonshot-v1-8k}")
-    private String model;
-
-    @Value("${ai.api-key}")
-    private String apiKey;
+    @Autowired
+    private AiProperties aiProperties;
 
     private WebClient webClient;
 
@@ -59,7 +57,7 @@ public class KimiAiServiceImpl implements AiService {
         this.webClient = WebClient.builder()
                 .baseUrl(AiConstants.KimiAiConstants.URL)
                 .defaultHeader(HttpHeaders.CONTENT_TYPE, 
MediaType.APPLICATION_JSON_VALUE)
-                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
+                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + 
aiProperties.getApiKey())
                 .exchangeStrategies(ExchangeStrategies.builder()
                         .codecs(item -> 
item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024))
                         .build())
@@ -74,9 +72,9 @@ public class KimiAiServiceImpl implements AiService {
     @Override
     public Flux<ServerSentEvent<String>> requestAi(String text) {
         try {
-            checkParam(text, apiKey);
+            checkParam(text, aiProperties.getModel(), 
aiProperties.getApiKey());
             OpenAiRequestParamDTO zhiPuRequestParamDTO = 
OpenAiRequestParamDTO.builder()
-                    .model(model)
+                    .model(aiProperties.getModel())
                     .stream(Boolean.TRUE)
                     .maxTokens(AiConstants.KimiAiConstants.MAX_TOKENS)
                     .temperature(AiConstants.KimiAiConstants.TEMPERATURE)
@@ -101,8 +99,9 @@ public class KimiAiServiceImpl implements AiService {
 
     }
 
-    private void checkParam(String param, String apiKey) {
+    private void checkParam(String param, String model, String apiKey) {
         Assert.notNull(param, "text is null");
+        Assert.notNull(param, "model 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 9167ed2d9..3b1271d51 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
@@ -22,11 +22,12 @@ 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.config.AiProperties;
 import org.apache.hertzbeat.manager.pojo.dto.AiMessage;
 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.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -43,24 +44,23 @@ import reactor.core.publisher.Flux;
  * sparkDesk AI
  */
 @Service("SparkDeskAiServiceImpl")
-@ConditionalOnProperty(prefix = "ai", name = {"api-key", "api-secret"}, 
matchIfMissing = false)
+@ConditionalOnProperty(prefix = "ai", name = "type", havingValue = "sparkDesk")
 @Slf4j
 public class SparkDeskAiServiceImpl implements AiService {
 
-    @Value("${ai.model:generalv3.5}")
-    private String model;
 
-    @Value("${ai.api-key}")
-    private String apiKey;
-    @Value("${ai.api-secret}")
-    private String apiSecret;
+    @Autowired
+    private AiProperties aiProperties;
 
     private WebClient webClient;
 
     @PostConstruct
     private void init() {
         StringBuilder sb = new StringBuilder();
-        String bearer = sb.append("Bearer 
").append(apiKey).append(":").append(apiSecret).toString();
+        String bearer = sb.append("Bearer ")
+                          .append(aiProperties.getApiKey())
+                          
.append(":").append(aiProperties.getApiSecret()).toString();
+
         this.webClient = WebClient.builder()
                 .baseUrl(AiConstants.SparkDeskConstants.SPARK_ULTRA_URL)
                 .defaultHeader(HttpHeaders.CONTENT_TYPE, 
MediaType.APPLICATION_JSON_VALUE)
@@ -80,9 +80,9 @@ public class SparkDeskAiServiceImpl implements AiService {
     public Flux<ServerSentEvent<String>> requestAi(String text) {
 
         try {
-            checkParam(text, apiKey);
+            checkParam(text, aiProperties.getApiKey(), 
aiProperties.getModel());
             OpenAiRequestParamDTO zhiPuRequestParamDTO = 
OpenAiRequestParamDTO.builder()
-                    .model(model)
+                    .model(aiProperties.getModel())
                     //sse
                     .stream(Boolean.TRUE)
                     .maxTokens(AiConstants.SparkDeskConstants.MAX_TOKENS)
@@ -102,8 +102,9 @@ public class SparkDeskAiServiceImpl implements AiService {
         }
     }
 
-    private void checkParam(String param, String apiKey) {
+    private void checkParam(String param, String apiKey, String model) {
         Assert.notNull(param, "text is null");
+        Assert.notNull(param, "model 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 543e78df6..b6111c247 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
@@ -23,11 +23,12 @@ 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.config.AiProperties;
 import org.apache.hertzbeat.manager.pojo.dto.AiMessage;
 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.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.MediaType;
@@ -44,23 +45,21 @@ import reactor.core.publisher.Flux;
  * ZhiPu AI
  */
 @Service("ZhiPuServiceImpl")
-@ConditionalOnProperty(prefix = "ai", name = "api-key", matchIfMissing = false)
+@ConditionalOnProperty(prefix = "ai", name = "type", havingValue = "zhiPu")
 @Slf4j
 public class ZhiPuServiceImpl implements AiService {
-    @Value("${ai.model:glm-4}")
-    private String model;
-    @Value("${ai.api-key}")
-    private String apiKey;
 
-    private WebClient webClient;
+    @Autowired
+    private AiProperties aiProperties;
 
+    private WebClient webClient;
 
     @PostConstruct
     private void init() {
         this.webClient = WebClient.builder()
                 .baseUrl(AiConstants.ZhiPuConstants.URL)
                 .defaultHeader(HttpHeaders.CONTENT_TYPE, 
MediaType.APPLICATION_JSON_VALUE)
-                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey)
+                .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + 
aiProperties.getApiKey())
                 .exchangeStrategies(ExchangeStrategies.builder()
                         .codecs(item -> 
item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024))
                         .build())
@@ -75,9 +74,9 @@ public class ZhiPuServiceImpl implements AiService {
     @Override
     public Flux<ServerSentEvent<String>> requestAi(String text) {
         try {
-            checkParam(text, model, apiKey);
+            checkParam(text, aiProperties.getModel(), 
aiProperties.getApiKey());
             OpenAiRequestParamDTO zhiPuRequestParamDTO = 
OpenAiRequestParamDTO.builder()
-                    .model(model)
+                    .model(aiProperties.getModel())
                     //sse
                     .stream(Boolean.TRUE)
                     .maxTokens(AiConstants.ZhiPuConstants.MAX_TOKENS)
diff --git a/manager/src/main/resources/application-test.yml 
b/manager/src/main/resources/application-test.yml
index 9ba86b7c5..24c06555d 100644
--- a/manager/src/main/resources/application-test.yml
+++ b/manager/src/main/resources/application-test.yml
@@ -86,13 +86,14 @@ scheduler:
     enabled: true
     port: 1158
 
-#AI config
+# AI config
+# See the documentation for details : 
https://hertzbeat.apache.org/zh-cn/docs/help/aiConfig
 ai:
-  #AI Type:zhiPu
-  type: zhiPu
-  #Model name:glm-4
-  model: glm-4
-  #api key
-  api-key: xxx
+  # AI Type:zhiPu、alibabaAi、kimiAi、sparkDesk
+  type:
+  # Model name:glm-4、qwen-turboo、moonshot-v1-8k、generalv3.5
+  model:
+  # api key
+  api-key:
   #At present, only IFLYTEK large model needs to be filled in
-  api-secret: xxx
\ No newline at end of file
+  api-secret:
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to