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 da7c967375e..eed6d631ad2 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 @@ -25,6 +25,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; @@ -61,7 +62,7 @@ public class AiController { @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE}) @Operation(summary = "Artificial intelligence questions and Answers", description = "Artificial intelligence questions and Answers") - public Flux requestAi(@Parameter(description = "Request text", example = "Who are you") @RequestParam("text") String text, + public Flux> 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) { AiService aiServiceImplBean = aiServiceFactory.getAiServiceImplBean(type); 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..3c97739f253 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 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) + .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 6b2bd65006a..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 @@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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; @@ -76,39 +77,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 6ace14282a5..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 @@ -17,21 +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.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.boot.autoconfigure.condition.ConditionalOnProperty; 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; @@ -73,39 +72,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 cdb4350d152..9167ed2d9a2 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,21 +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.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.boot.autoconfigure.condition.ConditionalOnProperty; 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; @@ -78,11 +77,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) @@ -96,28 +95,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, "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 04c95992b44..543e78df690 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,21 +17,21 @@ 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.boot.autoconfigure.condition.ConditionalOnProperty; 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; @@ -73,43 +73,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, "ai.api-key is null");