From f3b9bdf6248a0e789e2ba473bf93e94c918b6928 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 20 Jun 2024 01:06:50 +0800 Subject: [PATCH 01/29] [feature] : ai --- .../common/constants/AIConstants.java | 15 +++ .../manager/controller/AIController.java | 42 ++++++++ .../controller/MonitorsController.java | 19 ++-- .../hertzbeat/manager/pojo/dto/AIMessage.java | 24 +++++ .../manager/pojo/dto/AIResponse.java | 26 +++++ .../pojo/dto/ChatGptRequestParamDTO.java | 51 ++++++++++ .../hertzbeat/manager/service/AIService.java | 25 +++++ .../service/impl/ChatGptServiceImpl.java | 97 +++++++++++++++++++ manager/src/main/resources/ai.yml | 25 +++++ manager/src/main/resources/application.yml | 2 +- 10 files changed, 318 insertions(+), 8 deletions(-) create mode 100644 common/src/main/java/org/apache/hertzbeat/common/constants/AIConstants.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/controller/AIController.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/AIService.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java create mode 100644 manager/src/main/resources/ai.yml 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 new file mode 100644 index 00000000000..015d5e967b4 --- /dev/null +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/AIConstants.java @@ -0,0 +1,15 @@ +package org.apache.hertzbeat.common.constants; + +/** + * AI constants + */ +public interface AIConstants { + + /** + * chatgpt constants + */ + interface ChatgptConstants { + + String url="https://api.openai.com/v1/chat/completions"; + } +} 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 new file mode 100644 index 00000000000..c5229becf3d --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AIController.java @@ -0,0 +1,42 @@ +package org.apache.hertzbeat.manager.controller; + +import com.alibaba.fastjson.JSON; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.common.entity.dto.Message; +import org.apache.hertzbeat.manager.pojo.dto.AIResponse; +import org.apache.hertzbeat.manager.pojo.dto.MonitorDto; +import org.apache.hertzbeat.manager.service.AIService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +import static org.apache.hertzbeat.common.constants.CommonConstants.MONITOR_NOT_EXIST_CODE; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; + +/** + * AI Management API + */ +@Tag(name = "AI Manage API") +@RestController +@RequestMapping(value = "/api/ai", produces = {APPLICATION_JSON_VALUE}) +@Slf4j +public class AIController { + + @Resource(name = "chatGptServiceImpl") + private AIService aiService; + + + @GetMapping(path = "/test") + @Operation(summary = "Obtain monitoring information based on monitoring ID", description = "Obtain monitoring information based on monitoring ID") + public ResponseEntity getMonitor( + @Parameter(description = "Monitoring task ID", example = "6565463543") @RequestParam("param") String param) { + + AIResponse aiResponse = aiService.aiResponse(param); + return ResponseEntity.ok(JSON.toJSONString(aiResponse)); + } +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index d75d3ad94f9..610941e1a67 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -18,6 +18,8 @@ package org.apache.hertzbeat.manager.controller; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; + +import com.alibaba.fastjson.JSON; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; @@ -31,6 +33,8 @@ import java.util.List; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.manager.Monitor; +import org.apache.hertzbeat.manager.pojo.dto.AIResponse; +import org.apache.hertzbeat.manager.service.AIService; import org.apache.hertzbeat.manager.service.MonitorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -48,6 +52,8 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; + /** * Monitor and manage batch API * 监控管理批量API @@ -63,7 +69,8 @@ public class MonitorsController { @Autowired private MonitorService monitorService; - + @Resource(name = "chatGptServiceImpl") + private AIService aiService; @GetMapping @Operation(summary = "Obtain a list of monitoring information based on query filter items", description = "Obtain a list of monitoring information based on query filter items") @@ -156,14 +163,12 @@ public ResponseEntity>> getAppMonitors( @DeleteMapping @Operation(summary = "Delete monitoring items in batches according to the monitoring ID list", description = "Delete monitoring items in batches according to the monitoring ID list") - public ResponseEntity> deleteMonitors( + public ResponseEntity deleteMonitors( @Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List ids ) { - if (ids != null && !ids.isEmpty()) { - monitorService.deleteMonitors(new HashSet<>(ids)); - } - Message message = Message.success(); - return ResponseEntity.ok(message); + AIResponse aiResponse = aiService.aiResponse("今天天气怎么样"); + return ResponseEntity.ok(JSON.toJSONString(aiResponse)); + } @DeleteMapping("manage") diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java new file mode 100644 index 00000000000..6da5545a56e --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java @@ -0,0 +1,24 @@ +package org.apache.hertzbeat.manager.pojo.dto; + + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * ai message + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class AIMessage { + /** + * role + */ + private String role; + + /** + * content + */ + private String content; +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java new file mode 100644 index 00000000000..99988796810 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java @@ -0,0 +1,26 @@ +package org.apache.hertzbeat.manager.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class AIResponse { + + /** + * response message + */ + private List choices; + + @Data + @AllArgsConstructor + @NoArgsConstructor + public static class Choice { + private int index; + private AIMessage message; + } +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java new file mode 100644 index 00000000000..bb2bbed0331 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java @@ -0,0 +1,51 @@ +package org.apache.hertzbeat.manager.pojo.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * chatGpt Request param + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class ChatGptRequestParamDTO { + + + /** + * ai version + */ + private String model; + + /** + * request message + */ + private List messages; + + /** + * Number of message responses + */ + private int maxCompletions = 1; + + /** + * The default value is 1 and ranges from 0 to 1. This parameter regulates the randomness of the response. + * Higher values increase randomness, while lower values increase focus and certainty + */ + private double temperature = 1; + + /** + * There is no limit by default, + * but this parameter allows you to specify the maximum number of tokens to be generated in the response. + */ + private Integer maxTokens; + + +} + + + 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 new file mode 100644 index 00000000000..e3213d94a77 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/AIService.java @@ -0,0 +1,25 @@ +package org.apache.hertzbeat.manager.service; + +import org.apache.hertzbeat.manager.pojo.dto.AIResponse; + +import java.util.Map; + +/** + * AI Service + */ +public interface AIService { + + /** + * get AI type + * @return + */ + int getType(); + + /** + * ai response + * @param param + * @return + */ + AIResponse aiResponse(String param); + +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java new file mode 100644 index 00000000000..66b1a167101 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java @@ -0,0 +1,97 @@ +package org.apache.hertzbeat.manager.service.impl; + +import com.alibaba.fastjson.JSON; +import org.apache.hertzbeat.common.constants.AIConstants; +import org.apache.hertzbeat.manager.pojo.dto.AIMessage; +import org.apache.hertzbeat.manager.pojo.dto.AIResponse; +import org.apache.hertzbeat.manager.pojo.dto.ChatGptRequestParamDTO; +import org.apache.hertzbeat.manager.service.AIService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; + +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; +import org.springframework.web.client.RestTemplate; + +import javax.annotation.Resource; +import java.util.List; + +/** + * chatgpt + */ +@Component("chatGptServiceImpl") +public class ChatGptServiceImpl implements AIService { + +// @Value("${openai.chatgpt.model}") +// private String model; +// +// @Value("${openai.chatgpt.api.key}") +// private String apiKey; +// +// @Value("${openai.chatgpt.max_completions:1}") +// private int maxCompletions; +// +// @Value("${openai.chatgpt.temperature:1}") +// private double temperature; +// +// @Value("${openai.chatgpt.max_tokens}") +// private int maxTokens; +// + @Resource + private RestTemplate restTemplate; + + public static final String MODEL = "gpt-3.5-turbo"; + public static final String API_KEY = "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"; + public static final int MAX_COMPLETIONS = 1; + public static final double TEMPERATURE = 1; + public static final int MAX_TOKENS = 1000; + + + + @Override + public int getType() { + return 0; + } + + @Override + public AIResponse aiResponse(String param) { + checkParam(param,MODEL,API_KEY); + + ChatGptRequestParamDTO chatGptRequestParamDTO = ChatGptRequestParamDTO.builder() + .model(MODEL) + .maxTokens(MAX_TOKENS) + .maxCompletions(MAX_COMPLETIONS) + .messages(List.of(new AIMessage("user", param))) + .temperature(TEMPERATURE).build(); + + + HttpHeaders headers = new HttpHeaders(); + headers.add("Authorization", "Bearer " + API_KEY); + System.out.println(JSON.toJSONString(chatGptRequestParamDTO)); + AIResponse aiResponse = restTemplate.postForObject( + AIConstants.ChatgptConstants.url, + new HttpEntity<>(chatGptRequestParamDTO, headers), + AIResponse.class + ); + + return aiResponse; + } + + /** + * check param + * @param param + * @param model + * @param apiKey + */ + private void checkParam(String param, String model, String apiKey) { + Assert.notNull(param,"param is null"); + Assert.notNull(model,"ai.yml openai.chatgpt.api.key is null"); + Assert.notNull(apiKey,"ai.yml openai.chatgpt.api.key is null"); + } + +} diff --git a/manager/src/main/resources/ai.yml b/manager/src/main/resources/ai.yml new file mode 100644 index 00000000000..a92f9f06244 --- /dev/null +++ b/manager/src/main/resources/ai.yml @@ -0,0 +1,25 @@ +# AI Configuration template + + +openai: + chatgpt: + # 设置使用的GPT模型 + # Set the GPT model to use + model: gpt-3.5-turbo + + api: + # 替换为你的API密钥 + # Replace with your API key + key: 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 + + # 每次请求的最大完成数量,通常范围是1到5 + # Maximum number of completions per request, typically ranges from 1 to 5 + max_completions: 1 + + # 控制生成文本的随机性,范围是0到1,0表示最确定的输出,1表示随机性更高 + # Controls the randomness of text generation, range is from 0 to 1, 0 for the most deterministic output + temperature: 1 + + # 设置返回文本的最大token数量 + # Maximum number of tokens for the response text + max_tokens: 1000 diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index 3e5d9711681..600b11b6999 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -63,7 +63,7 @@ sureness: spring: config: activate: - on-profile: prod + on-profile: dev datasource: driver-class-name: org.h2.Driver From c28c373f1847d848677b6372ca67e383e917a9d2 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 20 Jun 2024 01:08:30 +0800 Subject: [PATCH 02/29] [feature] : ai --- .../src/main/resources/application-dev.yml | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 manager/src/main/resources/application-dev.yml diff --git a/manager/src/main/resources/application-dev.yml b/manager/src/main/resources/application-dev.yml new file mode 100644 index 00000000000..707161eae8f --- /dev/null +++ b/manager/src/main/resources/application-dev.yml @@ -0,0 +1,131 @@ +spring: + datasource: + driver-class-name: org.h2.Driver + username: sa + password: 123456 + url: jdbc:h2:./data/hertzbeat;MODE=MYSQL + hikari: + max-lifetime: 120000 + h2: + console: + path: /h2-console + enabled: true + jpa: + show-sql: false + database-platform: org.eclipse.persistence.platform.database.MySQLPlatform + database: h2 + properties: + eclipselink: + logging: + level: SEVERE + flyway: + enabled: true + clean-disabled: true + baseline-on-migrate: true + baseline-version: 1 + locations: + - classpath:db/migration/{vendor} + + mail: + host: smtp.exmail.qq.com + username: sss@tancloud.cn + password: sss + port: 465 + default-encoding: UTF-8 + properties: + mail: + smtp: + socketFactoryClass: javax.net.ssl.SSLSocketFactory + ssl: + enable: true + +management: + health: + mail: + enabled: off + endpoints: + web: + exposure: + include: + - 'metrics' + - 'health' + enabled-by-default: on + +sureness: + auths: + - digest + - basic + - jwt + +warehouse: + store: + jpa: + enabled: false + expire-time: 1h + victoria-metrics: + enabled: false +# url: http://82.157.76.80:8428 + url: http://localhost:8428 + username: root + password: root + influxdb: + enabled: false + # server-url: https://121.37.174.225:8635 + # username: rwuser + # password: Dromara@123 + server-url: http://localhost:8086 + username: root + password: root + td-engine: + enabled: false + driver-class-name: com.taosdata.jdbc.rs.RestfulDriver + url: jdbc:TAOS-RS://localhost:6041/hertzbeat + username: root + password: taosdata + iot-db: + enabled: false + host: 127.0.0.1 + rpc-port: 6667 + username: root + password: root + # org.dromara.hertzbeat.warehouse.config.IotDbVersion: V_0_13 || V_1_0 + version: V_1_0 + query-timeout-in-ms: -1 + # 数据存储时间:默认'7776000000'(90天,单位为毫秒,-1代表永不过期) + # data expire time, unit:ms, default '7776000000'(90 days, -1:never expire) + expire-time: '7776000000' + redis: + enabled: false + host: 127.0.0.1 + port: 6379 + password: 123456 + memory: + enabled: true + init-size: 16 + +common: + sms: + # tencent: + # secret-id: AKIDbQ4VhdMr89wUmotjIcgU2PaaMvOuBCzY + # secret-key: PaXGl0ziY9UcWFjyyiCPMr77rLkJYlyA + # app-id: 1400781637 + # sign-name: 探云科技 + # template-id: 1636532 + org-single: + url: http://172.16.18.22 + app-id: dcms + app-secret: 1bdb0b1b9d10bb1228dbd371a2db53e5 + company: 194370 + queue: + # memory or kafka + type: memory + # properties when queue type is kafka + kafka: + servers: 139.198.109.64:9092 + metrics-data-topic: async-metrics-data + alerts-data-topic: async-alerts-data + +scheduler: + server: + enabled: true + port: 1159 From fa3c5d67c37113a2151e47d3ed69b68609114047 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 20 Jun 2024 01:11:10 +0800 Subject: [PATCH 03/29] [feature] : ai --- manager/src/main/resources/ai.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager/src/main/resources/ai.yml b/manager/src/main/resources/ai.yml index a92f9f06244..73d2416db51 100644 --- a/manager/src/main/resources/ai.yml +++ b/manager/src/main/resources/ai.yml @@ -10,7 +10,7 @@ openai: api: # 替换为你的API密钥 # Replace with your API key - key: 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 + key: test # 每次请求的最大完成数量,通常范围是1到5 # Maximum number of completions per request, typically ranges from 1 to 5 From 0a562f0fe6cc2b291cc1788f246bda76d161a872 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 20 Jun 2024 01:18:12 +0800 Subject: [PATCH 04/29] [feature] : ai --- .../src/main/resources/application-dev.yml | 131 ------------------ 1 file changed, 131 deletions(-) delete mode 100644 manager/src/main/resources/application-dev.yml diff --git a/manager/src/main/resources/application-dev.yml b/manager/src/main/resources/application-dev.yml deleted file mode 100644 index 707161eae8f..00000000000 --- a/manager/src/main/resources/application-dev.yml +++ /dev/null @@ -1,131 +0,0 @@ -spring: - datasource: - driver-class-name: org.h2.Driver - username: sa - password: 123456 - url: jdbc:h2:./data/hertzbeat;MODE=MYSQL - hikari: - max-lifetime: 120000 - h2: - console: - path: /h2-console - enabled: true - jpa: - show-sql: false - database-platform: org.eclipse.persistence.platform.database.MySQLPlatform - database: h2 - properties: - eclipselink: - logging: - level: SEVERE - flyway: - enabled: true - clean-disabled: true - baseline-on-migrate: true - baseline-version: 1 - locations: - - classpath:db/migration/{vendor} - - mail: - host: smtp.exmail.qq.com - username: sss@tancloud.cn - password: sss - port: 465 - default-encoding: UTF-8 - properties: - mail: - smtp: - socketFactoryClass: javax.net.ssl.SSLSocketFactory - ssl: - enable: true - -management: - health: - mail: - enabled: off - endpoints: - web: - exposure: - include: - - 'metrics' - - 'health' - enabled-by-default: on - -sureness: - auths: - - digest - - basic - - jwt - -warehouse: - store: - jpa: - enabled: false - expire-time: 1h - victoria-metrics: - enabled: false -# url: http://82.157.76.80:8428 - url: http://localhost:8428 - username: root - password: root - influxdb: - enabled: false - # server-url: https://121.37.174.225:8635 - # username: rwuser - # password: Dromara@123 - server-url: http://localhost:8086 - username: root - password: root - td-engine: - enabled: false - driver-class-name: com.taosdata.jdbc.rs.RestfulDriver - url: jdbc:TAOS-RS://localhost:6041/hertzbeat - username: root - password: taosdata - iot-db: - enabled: false - host: 127.0.0.1 - rpc-port: 6667 - username: root - password: root - # org.dromara.hertzbeat.warehouse.config.IotDbVersion: V_0_13 || V_1_0 - version: V_1_0 - query-timeout-in-ms: -1 - # 数据存储时间:默认'7776000000'(90天,单位为毫秒,-1代表永不过期) - # data expire time, unit:ms, default '7776000000'(90 days, -1:never expire) - expire-time: '7776000000' - redis: - enabled: false - host: 127.0.0.1 - port: 6379 - password: 123456 - memory: - enabled: true - init-size: 16 - -common: - sms: - # tencent: - # secret-id: AKIDbQ4VhdMr89wUmotjIcgU2PaaMvOuBCzY - # secret-key: PaXGl0ziY9UcWFjyyiCPMr77rLkJYlyA - # app-id: 1400781637 - # sign-name: 探云科技 - # template-id: 1636532 - org-single: - url: http://172.16.18.22 - app-id: dcms - app-secret: 1bdb0b1b9d10bb1228dbd371a2db53e5 - company: 194370 - queue: - # memory or kafka - type: memory - # properties when queue type is kafka - kafka: - servers: 139.198.109.64:9092 - metrics-data-topic: async-metrics-data - alerts-data-topic: async-alerts-data - -scheduler: - server: - enabled: true - port: 1159 From 984295378d9b09b1c03932550aa68f87517aab3c Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 20 Jun 2024 01:20:49 +0800 Subject: [PATCH 05/29] [feature] : ai --- .../hertzbeat/manager/service/impl/ChatGptServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java index 66b1a167101..9acb243156f 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java @@ -46,7 +46,7 @@ public class ChatGptServiceImpl implements AIService { private RestTemplate restTemplate; public static final String MODEL = "gpt-3.5-turbo"; - public static final String API_KEY = "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"; + public static final String API_KEY = "无"; public static final int MAX_COMPLETIONS = 1; public static final double TEMPERATURE = 1; public static final int MAX_TOKENS = 1000; From 92838b09d7e90228ccefd8a44bd89745f3c140af Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 14:33:51 +0800 Subject: [PATCH 06/29] [feature] : ai --- .../common/constants/AIConstants.java | 13 +- manager/pom.xml | 7 ++ .../manager/controller/AIController.java | 39 +++--- .../controller/MonitorsController.java | 19 +-- .../manager/pojo/dto/AIResponse.java | 26 ---- .../pojo/dto/ChatGptRequestParamDTO.java | 51 -------- .../manager/pojo/dto/ZhiPuAIResponse.java | 68 +++++++++++ .../pojo/dto/zhiPuRequestParamDTO.java | 52 ++++++++ .../hertzbeat/manager/service/AIService.java | 10 +- .../service/impl/AIServiceFactoryImpl.java | 37 ++++++ .../service/impl/ChatGptServiceImpl.java | 97 --------------- .../service/impl/ZhiPuServiceImpl.java | 113 ++++++++++++++++++ manager/src/main/resources/ai.yml | 25 ---- manager/src/main/resources/application.yml | 9 ++ 14 files changed, 328 insertions(+), 238 deletions(-) delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAIResponse.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/zhiPuRequestParamDTO.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AIServiceFactoryImpl.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java delete mode 100644 manager/src/main/resources/ai.yml 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 015d5e967b4..37e78f97585 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 @@ -8,8 +8,17 @@ public interface AIConstants { /** * chatgpt constants */ - interface ChatgptConstants { + interface ZhiPuConstants { + + /** + * zhiPu request url + */ + String URL="https://open.bigmodel.cn/api/paas/v4/chat/completions"; + + /** + * request role param + */ + String requestRole = "user"; - String url="https://api.openai.com/v1/chat/completions"; } } diff --git a/manager/pom.xml b/manager/pom.xml index 50b31d82fb3..84c01b5a05d 100644 --- a/manager/pom.xml +++ b/manager/pom.xml @@ -106,6 +106,13 @@ org.springframework.boot spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-starter-webflux + + org.springframework.boot 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 c5229becf3d..b9739ea678b 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 @@ -4,39 +4,38 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.extern.slf4j.Slf4j; -import org.apache.hertzbeat.common.entity.dto.Message; -import org.apache.hertzbeat.manager.pojo.dto.AIResponse; -import org.apache.hertzbeat.manager.pojo.dto.MonitorDto; + 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.http.ResponseEntity; +import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.bind.annotation.*; - -import javax.annotation.Resource; - -import static org.apache.hertzbeat.common.constants.CommonConstants.MONITOR_NOT_EXIST_CODE; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import reactor.core.publisher.Flux; +import static org.springframework.http.MediaType.TEXT_EVENT_STREAM_VALUE; /** * AI Management API */ @Tag(name = "AI Manage API") @RestController -@RequestMapping(value = "/api/ai", produces = {APPLICATION_JSON_VALUE}) -@Slf4j +@RequestMapping(value = "/api/ai") public class AIController { - @Resource(name = "chatGptServiceImpl") - private AIService aiService; + @Autowired + private AIServiceFactoryImpl aiServiceFactory; - @GetMapping(path = "/test") - @Operation(summary = "Obtain monitoring information based on monitoring ID", description = "Obtain monitoring information based on monitoring ID") - public ResponseEntity getMonitor( - @Parameter(description = "Monitoring task ID", example = "6565463543") @RequestParam("param") String param) { + /** + * request AI + * @param param + * @param type + * @return + */ + @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE}) + public Flux> requestAI(@RequestParam("param") String param, + @RequestParam(value = "type",required = false) String type) { + AIService aiServiceImplBean = aiServiceFactory.getAIServiceImplBean(type); - AIResponse aiResponse = aiService.aiResponse(param); - return ResponseEntity.ok(JSON.toJSONString(aiResponse)); + return aiServiceImplBean.requestAI(param); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index 610941e1a67..43ba4370831 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -18,8 +18,6 @@ package org.apache.hertzbeat.manager.controller; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; - -import com.alibaba.fastjson.JSON; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; @@ -33,8 +31,6 @@ import java.util.List; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.manager.Monitor; -import org.apache.hertzbeat.manager.pojo.dto.AIResponse; -import org.apache.hertzbeat.manager.service.AIService; import org.apache.hertzbeat.manager.service.MonitorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -51,9 +47,6 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; - /** * Monitor and manage batch API * 监控管理批量API @@ -69,8 +62,6 @@ public class MonitorsController { @Autowired private MonitorService monitorService; - @Resource(name = "chatGptServiceImpl") - private AIService aiService; @GetMapping @Operation(summary = "Obtain a list of monitoring information based on query filter items", description = "Obtain a list of monitoring information based on query filter items") @@ -163,12 +154,14 @@ public ResponseEntity>> getAppMonitors( @DeleteMapping @Operation(summary = "Delete monitoring items in batches according to the monitoring ID list", description = "Delete monitoring items in batches according to the monitoring ID list") - public ResponseEntity deleteMonitors( + public ResponseEntity> deleteMonitors( @Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List ids ) { - AIResponse aiResponse = aiService.aiResponse("今天天气怎么样"); - return ResponseEntity.ok(JSON.toJSONString(aiResponse)); - + if (ids != null && !ids.isEmpty()) { + monitorService.deleteMonitors(new HashSet<>(ids)); + } + Message message = Message.success(); + return ResponseEntity.ok(message); } @DeleteMapping("manage") diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java deleted file mode 100644 index 99988796810..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIResponse.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.apache.hertzbeat.manager.pojo.dto; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; - -@Data -@AllArgsConstructor -@NoArgsConstructor -public class AIResponse { - - /** - * response message - */ - private List choices; - - @Data - @AllArgsConstructor - @NoArgsConstructor - public static class Choice { - private int index; - private AIMessage message; - } -} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java deleted file mode 100644 index bb2bbed0331..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ChatGptRequestParamDTO.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.apache.hertzbeat.manager.pojo.dto; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -import java.util.List; - -/** - * chatGpt Request param - */ -@Data -@AllArgsConstructor -@NoArgsConstructor -@Builder -public class ChatGptRequestParamDTO { - - - /** - * ai version - */ - private String model; - - /** - * request message - */ - private List messages; - - /** - * Number of message responses - */ - private int maxCompletions = 1; - - /** - * The default value is 1 and ranges from 0 to 1. This parameter regulates the randomness of the response. - * Higher values increase randomness, while lower values increase focus and certainty - */ - private double temperature = 1; - - /** - * There is no limit by default, - * but this parameter allows you to specify the maximum number of tokens to be generated in the response. - */ - private Integer maxTokens; - - -} - - - 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 new file mode 100644 index 00000000000..6a5cec1b41a --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAIResponse.java @@ -0,0 +1,68 @@ +package org.apache.hertzbeat.manager.pojo.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@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; + + @Data + @AllArgsConstructor + @NoArgsConstructor + public class Choice { + private int index; + private AIMessage delta; + } + + @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 new file mode 100644 index 00000000000..9ce99802524 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/zhiPuRequestParamDTO.java @@ -0,0 +1,52 @@ +package org.apache.hertzbeat.manager.pojo.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * chatGpt 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 e3213d94a77..c8d2a8d6416 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 @@ -1,8 +1,9 @@ package org.apache.hertzbeat.manager.service; -import org.apache.hertzbeat.manager.pojo.dto.AIResponse; -import java.util.Map; +import org.springframework.http.codec.ServerSentEvent; +import reactor.core.publisher.Flux; + /** * AI Service @@ -13,13 +14,14 @@ public interface AIService { * get AI type * @return */ - int getType(); + String getType(); /** * ai response + * * @param param * @return */ - AIResponse aiResponse(String param); + Flux> requestAI(String param); } 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 new file mode 100644 index 00000000000..c06779c1f54 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AIServiceFactoryImpl.java @@ -0,0 +1,37 @@ +package org.apache.hertzbeat.manager.service.impl; + +import org.apache.hertzbeat.manager.service.AIService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; + +import javax.annotation.PostConstruct; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +@Component +public class AIServiceFactoryImpl { + @Value("${aiConfig.type:0}") + private String type; + @Autowired + private List aiService; + private Map aiServiceFactoryMap = new HashMap<>();; + + @PostConstruct + public void init() { + aiServiceFactoryMap = aiService.stream() + .collect(Collectors.toMap(AIService::getType, Function.identity())); + } + + public AIService getAIServiceImplBean(String type) { + Assert.notNull(type,"type is null"); + AIService aiServiceImpl = aiServiceFactoryMap.get(type); + Assert.notNull(aiServiceImpl,"No bean for current type found"); + return aiServiceImpl; + } + +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java deleted file mode 100644 index 9acb243156f..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ChatGptServiceImpl.java +++ /dev/null @@ -1,97 +0,0 @@ -package org.apache.hertzbeat.manager.service.impl; - -import com.alibaba.fastjson.JSON; -import org.apache.hertzbeat.common.constants.AIConstants; -import org.apache.hertzbeat.manager.pojo.dto.AIMessage; -import org.apache.hertzbeat.manager.pojo.dto.AIResponse; -import org.apache.hertzbeat.manager.pojo.dto.ChatGptRequestParamDTO; -import org.apache.hertzbeat.manager.service.AIService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.PropertySource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; - -import org.springframework.stereotype.Component; -import org.springframework.util.Assert; -import org.springframework.web.client.RestTemplate; - -import javax.annotation.Resource; -import java.util.List; - -/** - * chatgpt - */ -@Component("chatGptServiceImpl") -public class ChatGptServiceImpl implements AIService { - -// @Value("${openai.chatgpt.model}") -// private String model; -// -// @Value("${openai.chatgpt.api.key}") -// private String apiKey; -// -// @Value("${openai.chatgpt.max_completions:1}") -// private int maxCompletions; -// -// @Value("${openai.chatgpt.temperature:1}") -// private double temperature; -// -// @Value("${openai.chatgpt.max_tokens}") -// private int maxTokens; -// - @Resource - private RestTemplate restTemplate; - - public static final String MODEL = "gpt-3.5-turbo"; - public static final String API_KEY = "无"; - public static final int MAX_COMPLETIONS = 1; - public static final double TEMPERATURE = 1; - public static final int MAX_TOKENS = 1000; - - - - @Override - public int getType() { - return 0; - } - - @Override - public AIResponse aiResponse(String param) { - checkParam(param,MODEL,API_KEY); - - ChatGptRequestParamDTO chatGptRequestParamDTO = ChatGptRequestParamDTO.builder() - .model(MODEL) - .maxTokens(MAX_TOKENS) - .maxCompletions(MAX_COMPLETIONS) - .messages(List.of(new AIMessage("user", param))) - .temperature(TEMPERATURE).build(); - - - HttpHeaders headers = new HttpHeaders(); - headers.add("Authorization", "Bearer " + API_KEY); - System.out.println(JSON.toJSONString(chatGptRequestParamDTO)); - AIResponse aiResponse = restTemplate.postForObject( - AIConstants.ChatgptConstants.url, - new HttpEntity<>(chatGptRequestParamDTO, headers), - AIResponse.class - ); - - return aiResponse; - } - - /** - * check param - * @param param - * @param model - * @param apiKey - */ - private void checkParam(String param, String model, String apiKey) { - Assert.notNull(param,"param is null"); - Assert.notNull(model,"ai.yml openai.chatgpt.api.key is null"); - Assert.notNull(apiKey,"ai.yml openai.chatgpt.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 new file mode 100644 index 00000000000..29b2c42b032 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java @@ -0,0 +1,113 @@ +package org.apache.hertzbeat.manager.service.impl; + +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.apache.hertzbeat.common.constants.AIConstants; +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.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; +import org.springframework.web.reactive.function.client.ExchangeStrategies; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.Objects; + +/** + * chatgpt + */ +@Service("ZhiPuServiceImpl") +@Slf4j +public class ZhiPuServiceImpl implements AIService { + @Value("${aiConfig.model:glm-4}") + private String MODEL; + @Value("${aiConfig.api-key}") + private String API_KEY; + + 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 " + API_KEY) + .exchangeStrategies(ExchangeStrategies.builder() + .codecs(item -> item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) + .build()) + .build(); + } + + @Override + public String getType() { + return "0"; + } + + /** + * request AI + * + * @param param + * @return + */ + @Override + public Flux> requestAI(String param) { + checkParam(param, MODEL, API_KEY); + zhiPuRequestParamDTO chatGptRequestParamDTO = zhiPuRequestParamDTO.builder() + .model(MODEL) + //sse + .stream(Boolean.TRUE) + .maxTokens(1024) + .temperature(0.95) + .messages(List.of(new AIMessage(AIConstants.ZhiPuConstants.requestRole, param))) + .build(); + + return webClient.post() + .body(BodyInserters.fromValue(chatGptRequestParamDTO)) + .retrieve() + .bodyToFlux(String.class) + .filter(aiResponse -> !"[DONE]".equals(aiResponse)) + .map(this::convertToResponse); + + } + + private ServerSentEvent convertToResponse(String aiRes) { + try { + ZhiPuAIResponse zhiPuAIResponse = JSON.parseObject(aiRes, ZhiPuAIResponse.class); + if (Objects.nonNull(zhiPuAIResponse)) { + ZhiPuAIResponse.Choice choice = zhiPuAIResponse.getChoices().get(0); + String content = choice.getDelta().getContent(); + // 返回封装好的ServerSentEvent + return ServerSentEvent.builder() + .data(content) + .build(); + } + } catch (Exception e) { + log.info("convertToResponse Exception:{}", e.toString()); + } + + return ServerSentEvent.builder().build(); + } + + /** + * check param + * + * @param param + * @param model + * @param apiKey + */ + private void checkParam(String param, String model, String apiKey) { + Assert.notNull(param, "param is null"); + Assert.notNull(apiKey, "aiConfig.api-key is null"); + } + + +} diff --git a/manager/src/main/resources/ai.yml b/manager/src/main/resources/ai.yml deleted file mode 100644 index 73d2416db51..00000000000 --- a/manager/src/main/resources/ai.yml +++ /dev/null @@ -1,25 +0,0 @@ -# AI Configuration template - - -openai: - chatgpt: - # 设置使用的GPT模型 - # Set the GPT model to use - model: gpt-3.5-turbo - - api: - # 替换为你的API密钥 - # Replace with your API key - key: test - - # 每次请求的最大完成数量,通常范围是1到5 - # Maximum number of completions per request, typically ranges from 1 to 5 - max_completions: 1 - - # 控制生成文本的随机性,范围是0到1,0表示最确定的输出,1表示随机性更高 - # Controls the randomness of text generation, range is from 0 to 1, 0 for the most deterministic output - temperature: 1 - - # 设置返回文本的最大token数量 - # Maximum number of tokens for the response text - max_tokens: 1000 diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index 600b11b6999..16b27674204 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -196,3 +196,12 @@ scheduler: server: enabled: true port: 1158 + +#AI config +aiConfig: + #AI Type:zhiPu = 0 + type: 0 + #Model name:glm-4 + model: glm-4 + #api key + api-key: \ No newline at end of file From 28ff307abc97476e6e8ef0e8a57dc2edaa90e665 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 14:37:20 +0800 Subject: [PATCH 07/29] [feature] : ai --- .../apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java | 1 - manager/src/main/resources/application.yml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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 29b2c42b032..253986506b9 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 @@ -85,7 +85,6 @@ private ServerSentEvent convertToResponse(String aiRes) { if (Objects.nonNull(zhiPuAIResponse)) { ZhiPuAIResponse.Choice choice = zhiPuAIResponse.getChoices().get(0); String content = choice.getDelta().getContent(); - // 返回封装好的ServerSentEvent return ServerSentEvent.builder() .data(content) .build(); diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index 16b27674204..215810c4358 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -63,7 +63,7 @@ sureness: spring: config: activate: - on-profile: dev + on-profile: prod datasource: driver-class-name: org.h2.Driver From ff8f89427a2a14449f686668b2a19ee728017373 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 14:40:27 +0800 Subject: [PATCH 08/29] [feature] : ai --- .../org/apache/hertzbeat/manager/controller/AIController.java | 3 +++ .../hertzbeat/manager/service/impl/AIServiceFactoryImpl.java | 3 +-- 2 files changed, 4 insertions(+), 2 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 b9739ea678b..f61aec98284 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 @@ -8,6 +8,7 @@ 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.web.bind.annotation.*; import reactor.core.publisher.Flux; @@ -24,6 +25,8 @@ public class AIController { @Autowired private AIServiceFactoryImpl aiServiceFactory; + @Value("${aiConfig.type:0}") + private String type; /** * request AI 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 c06779c1f54..ad66e897527 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 @@ -15,8 +15,7 @@ @Component public class AIServiceFactoryImpl { - @Value("${aiConfig.type:0}") - private String type; + @Autowired private List aiService; private Map aiServiceFactoryMap = new HashMap<>();; From 597ad832d50fb01aec861018ee066c8aaf279a43 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 14:42:10 +0800 Subject: [PATCH 09/29] [feature] : ai --- .../org/apache/hertzbeat/manager/controller/AIController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 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 f61aec98284..44cd6e2aaed 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 @@ -31,12 +31,12 @@ public class AIController { /** * request AI * @param param - * @param type + * @param currentlyDisabledType Currently disabled, later released * @return */ @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE}) public Flux> requestAI(@RequestParam("param") String param, - @RequestParam(value = "type",required = false) String type) { + @RequestParam(value = "type",required = false) String currentlyDisabledType) { AIService aiServiceImplBean = aiServiceFactory.getAIServiceImplBean(type); return aiServiceImplBean.requestAI(param); From 9ba43a6a884d6e4879759dc0af3eed468ea98714 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 16:12:50 +0800 Subject: [PATCH 10/29] [feature] : fix AIConstants --- .../hertzbeat/common/constants/AIConstants.java | 13 +++++++++++++ .../manager/service/impl/ZhiPuServiceImpl.java | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) 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 37e78f97585..2023f5e285a 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 @@ -1,5 +1,7 @@ package org.apache.hertzbeat.common.constants; +import com.fasterxml.jackson.annotation.JsonProperty; + /** * AI constants */ @@ -20,5 +22,16 @@ interface ZhiPuConstants { */ String requestRole = "user"; + /** + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + */ + Integer maxTokens =1024; + /** + * 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 + */ + double temperature = 0.95; + } } 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 253986506b9..377b231b32c 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 @@ -65,8 +65,8 @@ public Flux> requestAI(String param) { .model(MODEL) //sse .stream(Boolean.TRUE) - .maxTokens(1024) - .temperature(0.95) + .maxTokens(AIConstants.ZhiPuConstants.maxTokens) + .temperature(AIConstants.ZhiPuConstants.temperature) .messages(List.of(new AIMessage(AIConstants.ZhiPuConstants.requestRole, param))) .build(); From 341e1adee70a2ec141ac41550794b69e688d0173 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 16:13:28 +0800 Subject: [PATCH 11/29] [feature] : fix AIConstants --- .../java/org/apache/hertzbeat/common/constants/AIConstants.java | 1 + 1 file changed, 1 insertion(+) 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 2023f5e285a..73073a67864 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 @@ -26,6 +26,7 @@ interface ZhiPuConstants { * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 */ Integer maxTokens =1024; + /** * 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 From 03dd8b6430f97b0910d9835ff85e93cc15d2e697 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 16:14:53 +0800 Subject: [PATCH 12/29] [feature] : fix AIConstants --- .../org/apache/hertzbeat/common/constants/AIConstants.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 73073a67864..969a6c163a6 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 @@ -15,7 +15,7 @@ interface ZhiPuConstants { /** * zhiPu request url */ - String URL="https://open.bigmodel.cn/api/paas/v4/chat/completions"; + String URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"; /** * request role param @@ -25,7 +25,7 @@ interface ZhiPuConstants { /** * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 */ - Integer maxTokens =1024; + Integer maxTokens = 1024; /** * The sampling temperature, which controls the randomness of the output, must be positive From 69549210d3329d4e05259ab574dda361f6ac4d45 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sat, 22 Jun 2024 16:15:51 +0800 Subject: [PATCH 13/29] [feature] : fix AIConstants --- .../apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 377b231b32c..167a4e39b69 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 @@ -22,7 +22,7 @@ import java.util.Objects; /** - * chatgpt + * 智普AI */ @Service("ZhiPuServiceImpl") @Slf4j From af19c06d9a6b0ca62cc11b71625721584060a5b9 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 23 Jun 2024 13:32:13 +0800 Subject: [PATCH 14/29] [feature] : fix AIConstants --- .../org/apache/hertzbeat/common/constants/AIConstants.java | 4 ++-- .../apache/hertzbeat/manager/controller/AIController.java | 4 ---- ...{zhiPuRequestParamDTO.java => ZhiPuRequestParamDTO.java} | 4 ++-- .../manager/service/impl/AIServiceFactoryImpl.java | 2 -- .../hertzbeat/manager/service/impl/ZhiPuServiceImpl.java | 6 +++--- 5 files changed, 7 insertions(+), 13 deletions(-) rename manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/{zhiPuRequestParamDTO.java => ZhiPuRequestParamDTO.java} (95%) 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 969a6c163a6..fd0b612cfab 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 @@ -1,6 +1,6 @@ package org.apache.hertzbeat.common.constants; -import com.fasterxml.jackson.annotation.JsonProperty; + /** * AI constants @@ -8,7 +8,7 @@ public interface AIConstants { /** - * chatgpt constants + * zhiPu constants */ interface ZhiPuConstants { 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 44cd6e2aaed..2626515dcd7 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 @@ -1,10 +1,6 @@ package org.apache.hertzbeat.manager.controller; -import com.alibaba.fastjson.JSON; -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.service.AIService; import org.apache.hertzbeat.manager.service.impl.AIServiceFactoryImpl; import org.springframework.beans.factory.annotation.Autowired; 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 similarity index 95% rename from manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/zhiPuRequestParamDTO.java rename to manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuRequestParamDTO.java index 9ce99802524..ad35887107d 100644 --- 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 @@ -9,13 +9,13 @@ import java.util.List; /** - * chatGpt Request param + * zhiPu Request param */ @Data @AllArgsConstructor @NoArgsConstructor @Builder -public class zhiPuRequestParamDTO { +public class ZhiPuRequestParamDTO { /** 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 ad66e897527..16eb5970f71 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 @@ -2,10 +2,8 @@ import org.apache.hertzbeat.manager.service.AIService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.Assert; - import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.List; 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 167a4e39b69..a1886792d44 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 @@ -5,7 +5,7 @@ import org.apache.hertzbeat.common.constants.AIConstants; 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.ZhiPuRequestParamDTO; import org.apache.hertzbeat.manager.service.AIService; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; @@ -61,7 +61,7 @@ public String getType() { @Override public Flux> requestAI(String param) { checkParam(param, MODEL, API_KEY); - zhiPuRequestParamDTO chatGptRequestParamDTO = zhiPuRequestParamDTO.builder() + ZhiPuRequestParamDTO zhiPuRequestParamDTO = ZhiPuRequestParamDTO.builder() .model(MODEL) //sse .stream(Boolean.TRUE) @@ -71,7 +71,7 @@ public Flux> requestAI(String param) { .build(); return webClient.post() - .body(BodyInserters.fromValue(chatGptRequestParamDTO)) + .body(BodyInserters.fromValue(zhiPuRequestParamDTO)) .retrieve() .bodyToFlux(String.class) .filter(aiResponse -> !"[DONE]".equals(aiResponse)) From 0c8a5bd1aa7a840594b87f28e24dcdb3c7f71b92 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 23 Jun 2024 14:19:28 +0800 Subject: [PATCH 15/29] [feature] : fix AIConstants --- .../org/apache/hertzbeat/common/constants/AIConstants.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 fd0b612cfab..c85b66555bc 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 @@ -25,14 +25,14 @@ interface ZhiPuConstants { /** * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 */ - Integer maxTokens = 1024; + Integer maxTokens = 1024; /** * 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 */ - double temperature = 0.95; + double temperature = 0.95; } } From 60ec4c6d3bf610be75eb24215355f6b2640228f6 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 23 Jun 2024 18:03:29 +0800 Subject: [PATCH 16/29] [check] : check ci --- .../common/constants/AIConstants.java | 38 ---------- .../common/constants/AiConstants.java | 54 +++++++++++++ .../manager/controller/AIController.java | 40 ---------- .../manager/controller/AiController.java | 64 ++++++++++++++++ .../controller/MonitorsController.java | 3 + .../hertzbeat/manager/pojo/dto/AIMessage.java | 24 ------ .../hertzbeat/manager/pojo/dto/AiMessage.java | 41 ++++++++++ ...PuAIResponse.java => ZhiPuAiResponse.java} | 33 +++++++- .../pojo/dto/ZhiPuRequestParamDTO.java | 24 +++++- .../hertzbeat/manager/service/AIService.java | 27 ------- .../hertzbeat/manager/service/AiService.java | 43 +++++++++++ .../service/impl/AIServiceFactoryImpl.java | 34 --------- .../service/impl/AiServiceFactoryImpl.java | 55 ++++++++++++++ .../service/impl/ZhiPuServiceImpl.java | 76 ++++++++++--------- 14 files changed, 351 insertions(+), 205 deletions(-) delete mode 100644 common/src/main/java/org/apache/hertzbeat/common/constants/AIConstants.java create mode 100644 common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/controller/AIController.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AiMessage.java rename manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/{ZhiPuAIResponse.java => ZhiPuAiResponse.java} (59%) delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/AIService.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/AiService.java delete mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AIServiceFactoryImpl.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AiServiceFactoryImpl.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 deleted file mode 100644 index c85b66555bc..00000000000 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/AIConstants.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.apache.hertzbeat.common.constants; - - - -/** - * AI constants - */ -public interface AIConstants { - - /** - * zhiPu constants - */ - interface ZhiPuConstants { - - /** - * zhiPu request url - */ - String URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"; - - /** - * request role param - */ - String requestRole = "user"; - - /** - * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 - */ - Integer maxTokens = 1024; - - /** - * 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 - */ - double temperature = 0.95; - - } -} 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 new file mode 100644 index 00000000000..61432e7ee57 --- /dev/null +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/AiConstants.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.common.constants; + +/** + * AI constants + */ +public interface AiConstants { + + /** + * zhiPu constants + */ + interface ZhiPuConstants { + + /** + * zhiPu request url + */ + String URL = "https://open.bigmodel.cn/api/paas/v4/chat/completions"; + + /** + * request role param + */ + String REQUEST_ROLE = "user"; + + /** + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + */ + Integer MAX_TOKENS = 1024; + + /** + * 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 + */ + double TEMPERATURE = 0.95; + + } +} 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 deleted file mode 100644 index 2626515dcd7..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/AIController.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.apache.hertzbeat.manager.controller; - -import io.swagger.v3.oas.annotations.tags.Tag; -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.web.bind.annotation.*; -import reactor.core.publisher.Flux; -import static org.springframework.http.MediaType.TEXT_EVENT_STREAM_VALUE; - -/** - * AI Management API - */ -@Tag(name = "AI Manage API") -@RestController -@RequestMapping(value = "/api/ai") -public class AIController { - - @Autowired - private AIServiceFactoryImpl aiServiceFactory; - - @Value("${aiConfig.type:0}") - private String type; - - /** - * request AI - * @param param - * @param currentlyDisabledType Currently disabled, later released - * @return - */ - @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE}) - public Flux> requestAI(@RequestParam("param") String param, - @RequestParam(value = "type",required = false) String currentlyDisabledType) { - AIService aiServiceImplBean = aiServiceFactory.getAIServiceImplBean(type); - - return aiServiceImplBean.requestAI(param); - } -} 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 new file mode 100644 index 00000000000..c7f0d084680 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/AiController.java @@ -0,0 +1,64 @@ +/* + * 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.controller; + +import static org.springframework.http.MediaType.TEXT_EVENT_STREAM_VALUE; +import io.swagger.v3.oas.annotations.tags.Tag; +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.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Flux; + + +/** + * AI Management API + */ +@Tag(name = "AI Manage API") +@RestController +@RequestMapping(value = "/api/ai") +public class AiController { + + /** + * AI beanFactory + */ + @Autowired + private AiServiceFactoryImpl aiServiceFactory; + + @Value("${aiConfig.type:0}") + private String type; + + /** + * request AI + * @param text request text + * @param currentlyDisabledType Currently disabled, later released + * @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) { + AiService aiServiceImplBean = aiServiceFactory.getAiServiceImplBean(type); + + return aiServiceImplBean.requestAi(text); + } +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index 43ba4370831..e147f137684 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -47,6 +47,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; + /** * Monitor and manage batch API * 监控管理批量API @@ -62,6 +63,7 @@ public class MonitorsController { @Autowired private MonitorService monitorService; + @GetMapping @Operation(summary = "Obtain a list of monitoring information based on query filter items", description = "Obtain a list of monitoring information based on query filter items") @@ -151,6 +153,7 @@ public ResponseEntity>> getAppMonitors( return ResponseEntity.ok(message); } + @DeleteMapping @Operation(summary = "Delete monitoring items in batches according to the monitoring ID list", description = "Delete monitoring items in batches according to the monitoring ID list") diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java deleted file mode 100644 index 6da5545a56e..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AIMessage.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.apache.hertzbeat.manager.pojo.dto; - - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -/** - * ai message - */ -@Data -@NoArgsConstructor -@AllArgsConstructor -public class AIMessage { - /** - * role - */ - private String role; - - /** - * content - */ - private String content; -} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AiMessage.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AiMessage.java new file mode 100644 index 00000000000..61001a81e90 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AiMessage.java @@ -0,0 +1,41 @@ +/* + * 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 lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * ai message + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class AiMessage { + /** + * role + */ + private String role; + + /** + * content + */ + private String content; +} 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 similarity index 59% rename from manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAIResponse.java rename to manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/ZhiPuAiResponse.java index 6a5cec1b41a..2b64384c526 100644 --- 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 @@ -1,16 +1,35 @@ +/* + * 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; -import java.util.List; - +/** + * ZhiPuAiResponse + */ @Data @AllArgsConstructor @NoArgsConstructor -public class ZhiPuAIResponse { +public class ZhiPuAiResponse { /** * Task order number generated by the AI open platform. Use this order number when invoking the request result interface @@ -32,14 +51,20 @@ public class ZhiPuAIResponse { */ private Tokens usage; + /** + * Choice + */ @Data @AllArgsConstructor @NoArgsConstructor public class Choice { private int index; - private AIMessage delta; + private AiMessage delta; } + /** + * Tokens + */ @Data @AllArgsConstructor @NoArgsConstructor 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 index ad35887107d..47917ff7262 100644 --- 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 @@ -1,12 +1,29 @@ +/* + * 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; -import java.util.List; /** * zhiPu Request param @@ -26,11 +43,12 @@ public class ZhiPuRequestParamDTO { /** * request message */ - private List messages; + 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 + * 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; 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 deleted file mode 100644 index c8d2a8d6416..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/AIService.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.apache.hertzbeat.manager.service; - - -import org.springframework.http.codec.ServerSentEvent; -import reactor.core.publisher.Flux; - - -/** - * AI Service - */ -public interface AIService { - - /** - * get AI type - * @return - */ - String getType(); - - /** - * ai response - * - * @param param - * @return - */ - Flux> requestAI(String param); - -} 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 new file mode 100644 index 00000000000..158570daba0 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/AiService.java @@ -0,0 +1,43 @@ +/* + * 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.service; + + +import org.springframework.http.codec.ServerSentEvent; +import reactor.core.publisher.Flux; + + +/** + * AI Service + */ +public interface AiService { + + /** + * get AI type + * @return type + */ + String getType(); + + /** + * AI response + * @param text text + * @return AI response + */ + Flux> requestAi(String 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 deleted file mode 100644 index 16eb5970f71..00000000000 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AIServiceFactoryImpl.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.apache.hertzbeat.manager.service.impl; - -import org.apache.hertzbeat.manager.service.AIService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.util.Assert; -import javax.annotation.PostConstruct; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -@Component -public class AIServiceFactoryImpl { - - @Autowired - private List aiService; - private Map aiServiceFactoryMap = new HashMap<>();; - - @PostConstruct - public void init() { - aiServiceFactoryMap = aiService.stream() - .collect(Collectors.toMap(AIService::getType, Function.identity())); - } - - public AIService getAIServiceImplBean(String type) { - Assert.notNull(type,"type is null"); - AIService aiServiceImpl = aiServiceFactoryMap.get(type); - Assert.notNull(aiServiceImpl,"No bean for current type found"); - return aiServiceImpl; - } - -} 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 new file mode 100644 index 00000000000..c294205d9f7 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AiServiceFactoryImpl.java @@ -0,0 +1,55 @@ +/* + * 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.service.impl; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import javax.annotation.PostConstruct; +import org.apache.hertzbeat.manager.service.AiService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; + +/** + * AI bean factory + */ +@Component +public class AiServiceFactoryImpl { + + @Autowired + private List aiService; + + private Map aiServiceFactoryMap = new HashMap<>(); + + @PostConstruct + public void init() { + aiServiceFactoryMap = aiService.stream() + .collect(Collectors.toMap(AiService::getType, Function.identity())); + } + + public AiService getAiServiceImplBean(String type) { + Assert.notNull(type, "type is null"); + AiService aiServiceImpl = aiServiceFactoryMap.get(type); + Assert.notNull(aiServiceImpl, "No bean for current type found"); + return aiServiceImpl; + } + +} 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 a1886792d44..9ba7c9ea49e 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 @@ -1,12 +1,32 @@ +/* + * 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.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.manager.pojo.dto.AIMessage; -import org.apache.hertzbeat.manager.pojo.dto.ZhiPuAIResponse; +import org.apache.hertzbeat.common.constants.AiConstants; +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.service.AIService; +import org.apache.hertzbeat.manager.service.AiService; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -17,20 +37,18 @@ import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; -import javax.annotation.PostConstruct; -import java.util.List; -import java.util.Objects; + /** * 智普AI */ @Service("ZhiPuServiceImpl") @Slf4j -public class ZhiPuServiceImpl implements AIService { +public class ZhiPuServiceImpl implements AiService { @Value("${aiConfig.model:glm-4}") - private String MODEL; + private String model; @Value("${aiConfig.api-key}") - private String API_KEY; + private String apiKey; private WebClient webClient; @@ -38,9 +56,9 @@ public class ZhiPuServiceImpl implements AIService { @PostConstruct private void init() { this.webClient = WebClient.builder() - .baseUrl(AIConstants.ZhiPuConstants.URL) + .baseUrl(AiConstants.ZhiPuConstants.URL) .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) - .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + API_KEY) + .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey) .exchangeStrategies(ExchangeStrategies.builder() .codecs(item -> item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) .build()) @@ -52,22 +70,16 @@ public String getType() { return "0"; } - /** - * request AI - * - * @param param - * @return - */ @Override - public Flux> requestAI(String param) { - checkParam(param, MODEL, API_KEY); + public Flux> requestAi(String text) { + checkParam(text, model, apiKey); ZhiPuRequestParamDTO zhiPuRequestParamDTO = ZhiPuRequestParamDTO.builder() - .model(MODEL) + .model(model) //sse .stream(Boolean.TRUE) - .maxTokens(AIConstants.ZhiPuConstants.maxTokens) - .temperature(AIConstants.ZhiPuConstants.temperature) - .messages(List.of(new AIMessage(AIConstants.ZhiPuConstants.requestRole, param))) + .maxTokens(AiConstants.ZhiPuConstants.MAX_TOKENS) + .temperature(AiConstants.ZhiPuConstants.TEMPERATURE) + .messages(List.of(new AiMessage(AiConstants.ZhiPuConstants.REQUEST_ROLE, text))) .build(); return webClient.post() @@ -81,9 +93,9 @@ public Flux> requestAI(String param) { private ServerSentEvent convertToResponse(String aiRes) { try { - ZhiPuAIResponse zhiPuAIResponse = JSON.parseObject(aiRes, ZhiPuAIResponse.class); - if (Objects.nonNull(zhiPuAIResponse)) { - ZhiPuAIResponse.Choice choice = zhiPuAIResponse.getChoices().get(0); + ZhiPuAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, ZhiPuAiResponse.class); + if (Objects.nonNull(zhiPuAiResponse)) { + ZhiPuAiResponse.Choice choice = zhiPuAiResponse.getChoices().get(0); String content = choice.getDelta().getContent(); return ServerSentEvent.builder() .data(content) @@ -96,15 +108,9 @@ private ServerSentEvent convertToResponse(String aiRes) { return ServerSentEvent.builder().build(); } - /** - * check param - * - * @param param - * @param model - * @param apiKey - */ + private void checkParam(String param, String model, String apiKey) { - Assert.notNull(param, "param is null"); + Assert.notNull(param, "text is null"); Assert.notNull(apiKey, "aiConfig.api-key is null"); } From d53cb17caf8cfdc78c3aa9de1eb90bd5ae34fc60 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 23 Jun 2024 18:57:24 +0800 Subject: [PATCH 17/29] [check] : check ci --- manager/src/main/resources/application-test.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/manager/src/main/resources/application-test.yml b/manager/src/main/resources/application-test.yml index 5562e96e0d7..b81102bf5a1 100644 --- a/manager/src/main/resources/application-test.yml +++ b/manager/src/main/resources/application-test.yml @@ -85,3 +85,12 @@ scheduler: server: enabled: true port: 1158 + +#AI config +aiConfig: + #AI Type:zhiPu = 0 + type: 0 + #Model name:glm-4 + model: glm-4 + #api key + api-key: \ No newline at end of file From 7d466ba7c789e92701493b6634cfc5b37a38f140 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Tue, 25 Jun 2024 23:05:59 +0800 Subject: [PATCH 18/29] =?UTF-8?q?[improve]=20:=20fix=20AiService=20getType?= =?UTF-8?q?=20method=20data=20type=EF=BC=8Ccomplete=20information=20for=20?= =?UTF-8?q?=20LICENSE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/constants/AiTypeEnum.java | 44 +++++++++++++++++++ .../hertzbeat/manager/service/AiService.java | 3 +- .../service/impl/AiServiceFactoryImpl.java | 7 ++- .../service/impl/ZhiPuServiceImpl.java | 5 ++- material/licenses/LICENSE | 3 ++ material/licenses/backend/LICENSE | 3 ++ 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java b/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java new file mode 100644 index 00000000000..041b2dbb55c --- /dev/null +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java @@ -0,0 +1,44 @@ +/* + * 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.common.constants; + +/** + * Ai type Enum + */ +public enum AiTypeEnum { + + /** + * 智普 + */ + zhiPu; + + + /** + * get type + */ + public static AiTypeEnum getTypeByName(String type) { + for (AiTypeEnum aiTypeEnum : values()) { + if (aiTypeEnum.name().equals(type)) { + return aiTypeEnum; + } + + } + return null; + } + +} 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 158570daba0..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 @@ -18,6 +18,7 @@ package org.apache.hertzbeat.manager.service; +import org.apache.hertzbeat.common.constants.AiTypeEnum; import org.springframework.http.codec.ServerSentEvent; import reactor.core.publisher.Flux; @@ -31,7 +32,7 @@ public interface AiService { * get AI type * @return type */ - String getType(); + AiTypeEnum getType(); /** * AI response 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 c294205d9f7..55a752f2f82 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 @@ -23,6 +23,7 @@ import java.util.function.Function; import java.util.stream.Collectors; 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.stereotype.Component; @@ -37,7 +38,7 @@ public class AiServiceFactoryImpl { @Autowired private List aiService; - private Map aiServiceFactoryMap = new HashMap<>(); + private Map aiServiceFactoryMap = new HashMap<>(); @PostConstruct public void init() { @@ -47,7 +48,9 @@ public void init() { public AiService getAiServiceImplBean(String type) { Assert.notNull(type, "type is null"); - AiService aiServiceImpl = aiServiceFactoryMap.get(type); + AiTypeEnum typeByName = AiTypeEnum.getTypeByName(type); + Assert.notNull(typeByName, "The current type is not supported"); + 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/ZhiPuServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ZhiPuServiceImpl.java index 9ba7c9ea49e..0e5f6953ff8 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,6 +23,7 @@ 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; @@ -66,8 +67,8 @@ private void init() { } @Override - public String getType() { - return "0"; + public AiTypeEnum getType() { + return AiTypeEnum.zhiPu; } @Override diff --git a/material/licenses/LICENSE b/material/licenses/LICENSE index 93406a95ad3..db28f69455a 100644 --- a/material/licenses/LICENSE +++ b/material/licenses/LICENSE @@ -392,6 +392,9 @@ The text of each license is the standard Apache 2.0 license. https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf/3.2.3 Apache-2.0 + https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-webflux/3.2.3 Apache-2.0 + https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-reactor-netty/3.2.3 Apache-2.0 + https://mvnrepository.com/artifact/org.springframework/spring-webflux/6.1.4 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/3.2.3 Apache-2.0 diff --git a/material/licenses/backend/LICENSE b/material/licenses/backend/LICENSE index 6e81db6869f..4f00a926f5b 100644 --- a/material/licenses/backend/LICENSE +++ b/material/licenses/backend/LICENSE @@ -392,6 +392,9 @@ The text of each license is the standard Apache 2.0 license. https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-logging/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf/3.2.3 Apache-2.0 + https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-webflux/3.2.3 Apache-2.0 + https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-reactor-netty/3.2.3 Apache-2.0 + https://mvnrepository.com/artifact/org.springframework/spring-webflux/6.1.4 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-validation/3.2.3 Apache-2.0 https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web/3.2.3 Apache-2.0 From 3be60b8dbab2dea8b9c3b172189378734b337e79 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 27 Jun 2024 00:19:00 +0800 Subject: [PATCH 19/29] [feature] :support alibaba Ai --- .../common/constants/AiConstants.java | 31 +++++ .../common/constants/AiTypeEnum.java | 7 +- .../controller/MonitorsController.java | 13 ++- .../pojo/dto/AliAiRequestParamDTO.java | 93 +++++++++++++++ .../manager/pojo/dto/AliAiResponse.java | 77 +++++++++++++ .../service/impl/AlibabaAiServiceImpl.java | 108 ++++++++++++++++++ 6 files changed, 323 insertions(+), 6 deletions(-) create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.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 61432e7ee57..1f97630132b 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 @@ -50,5 +50,36 @@ interface ZhiPuConstants { */ double TEMPERATURE = 0.95; + } + /** + * alibaba Ai constants + */ + interface AliAiConstants { + + /** + * alibabaAi request url + */ + String URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation"; + + /** + * request role param + */ + String REQUEST_ROLE = "user"; + + /** + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + */ + Integer MAX_TOKENS = 1024; + + /** + * 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 + */ + float TEMPERATURE = 0.9f; + + + } } diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java b/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java index 041b2dbb55c..87d7fc4d813 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java @@ -25,7 +25,12 @@ public enum AiTypeEnum { /** * 智普 */ - zhiPu; + zhiPu, + + /** + * alibabaAi + */ + alibabaAi; /** diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index 46e1129b3d7..bf47a3264b2 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -31,6 +31,7 @@ import java.util.List; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.manager.Monitor; +import org.apache.hertzbeat.manager.service.AiService; import org.apache.hertzbeat.manager.service.MonitorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -48,6 +49,8 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; + /** * Monitor and manage batch API */ @@ -63,6 +66,9 @@ public class MonitorsController { @Autowired private MonitorService monitorService; + @Resource(name = "AlibabaAiServiceImpl") + private AiService aiService; + @GetMapping @Operation(summary = "Obtain a list of monitoring information based on query filter items", description = "Obtain a list of monitoring information based on query filter items") @@ -159,11 +165,8 @@ public ResponseEntity>> getAppMonitors( public ResponseEntity> deleteMonitors( @Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List ids ) { - if (ids != null && !ids.isEmpty()) { - monitorService.deleteMonitors(new HashSet<>(ids)); - } - Message message = Message.success(); - return ResponseEntity.ok(message); + aiService.requestAi("你是谁啊"); + return null; } @DeleteMapping("manage") diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java new file mode 100644 index 00000000000..df04e7cbf8e --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java @@ -0,0 +1,93 @@ +package org.apache.hertzbeat.manager.pojo.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * Alibaba Ai Request param + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class AliAiRequestParamDTO { + + /** + * ai version + */ + private String model; + + /** + * Enter information about the model + */ + private Input input; + + /** + * Parameters used to control model generation + */ + private Parameters parameters; + + /** + * Input + */ + @Data + @AllArgsConstructor + @NoArgsConstructor + @Builder + public static class Input { + + /** + * request message + */ + private List messages; + + } + + /** + * Parameters + */ + @Data + @AllArgsConstructor + @NoArgsConstructor + @Builder + public static class Parameters { + + /** + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + */ + @JsonProperty("max_tokens") + private Integer maxTokens; + + /** + * Used to control the degree of randomness and variety. Specifically, the temperature value controls the + * degree to which the probability distribution for each candidate word is smoothed when text is generated. + * A higher temperature will reduce the peak value of the probability distribution, so that more low-probability + * words will be selected and the results will be more diversified. A lower temperature will increase the peak of the probability distribution, + * making it easier for high-probability words to be selected and producing more certain results. + */ + private float temperature; + + /** + * The Internet search service is built into the model, and this parameter controls whether the model refers to the Internet search results when generating text. The value can be: + * true: If Internet search is enabled, the model uses the search results as reference information in the text generation process, but the model "decides" whether to use the Internet search results based on its internal logic. + * false: Turn off Internet search. + */ + @JsonProperty("enable_search") + private boolean enableSearch; + + /** + * Set return format,default message + */ + @JsonProperty("result_format") + private String resultFormat; + + } + + + +} diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java new file mode 100644 index 00000000000..7fd03a14aaa --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java @@ -0,0 +1,77 @@ +package org.apache.hertzbeat.manager.pojo.dto; + + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * AliAiResponse + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class AliAiResponse { + + + /** + * response message + */ + private List choices; + + /** + * Returns the number of tokens invoked by the model at the end. + */ + private AliAiResponse.Tokens usage; + + /** + * Choice + */ + @Data + @AllArgsConstructor + @NoArgsConstructor + public class Choice { + /** + * Stop cause: + * null: being generated + * stop: stop token causes the end + * length: indicates that the generation length ends + */ + @JsonProperty("finish_reason") + private String finishReason; + private AiMessage message; + } + + /** + * Tokens + */ + @Data + @AllArgsConstructor + @NoArgsConstructor + public class Tokens { + + /** + * The number of tokens of the model output content + */ + @JsonProperty("output_tokens") + private Integer outputTokens; + + /** + * The number of tokens entered this request. + * When enable_search is set to true, the number of tokens entered is greater than the number of + * tokens you entered the request because you need to add search related content. + */ + @JsonProperty("input_tokens") + private Integer inputTokens; + + /** + * usage.output_tokens and usage.input_tokens sum + */ + @JsonProperty("total_tokens") + private Integer totalTokens; + } + +} 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 new file mode 100644 index 00000000000..12e823862da --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/AlibabaAiServiceImpl.java @@ -0,0 +1,108 @@ +package org.apache.hertzbeat.manager.service.impl; + +import com.alibaba.fastjson.JSON; +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.*; +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; +import org.springframework.web.reactive.function.client.ExchangeStrategies; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; + +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.Objects; + + +/** + * alibaba Ai + */ +@Service("AlibabaAiServiceImpl") +@Slf4j +public class AlibabaAiServiceImpl implements AiService { + + @Value("${aiConfig.model:qwen-turbo}") + private String model; + @Value("${aiConfig.api-key}") + private String apiKey; + + + 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) + //sse + .defaultHeader(HttpHeaders.ACCEPT,MediaType.TEXT_EVENT_STREAM_VALUE) + .exchangeStrategies(ExchangeStrategies.builder() + .codecs(item -> item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) + .build()) + .build(); + } + + @Override + public AiTypeEnum getType() { + return AiTypeEnum.alibabaAi; + } + + @Override + 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") + .build()) + .build(); + + System.out.println(JSON.toJSONString(aliAiRequestParamDTO) + "apikey" + apiKey); + webClient.post() + .body(BodyInserters.fromValue(aliAiRequestParamDTO)) + .retrieve() + .bodyToFlux(String.class) + .doOnNext(System.out::println) // 打印每个元素 + .subscribe(); + + return null; + } + + + private ServerSentEvent convertToResponse(String aiRes) { + try { + ZhiPuAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, ZhiPuAiResponse.class); + if (Objects.nonNull(zhiPuAiResponse)) { + ZhiPuAiResponse.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()); + } + + return ServerSentEvent.builder().build(); + } + private void checkParam(String param, String apiKey) { + Assert.notNull(param, "text is null"); + Assert.notNull(apiKey, "aiConfig.api-key is null"); + } +} From 487bed52926b4a0dd720a2deea0d9b607d2b447a Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 27 Jun 2024 22:02:47 +0800 Subject: [PATCH 20/29] [feature] :support alibaba Ai --- .../common/constants/AiConstants.java | 2 +- .../manager/controller/AiController.java | 3 +- .../controller/MonitorsController.java | 13 ++-- .../pojo/dto/AliAiRequestParamDTO.java | 33 ++++++++- .../manager/pojo/dto/AliAiResponse.java | 45 +++++++++-- .../hertzbeat/manager/service/AiService.java | 3 +- .../service/impl/AlibabaAiServiceImpl.java | 74 +++++++++++-------- .../service/impl/ZhiPuServiceImpl.java | 16 ++-- .../src/main/resources/application-test.yml | 6 +- manager/src/main/resources/application.yml | 13 ++-- manager/src/main/resources/sureness.yml | 1 + 11 files changed, 136 insertions(+), 73 deletions(-) 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 1f97630132b..621085b063e 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 @@ -51,6 +51,7 @@ interface ZhiPuConstants { double TEMPERATURE = 0.95; } + /** * alibaba Ai constants */ @@ -80,6 +81,5 @@ interface AliAiConstants { float TEMPERATURE = 0.9f; - } } 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 c7f0d084680..0e456260892 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,7 +23,6 @@ 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; @@ -55,7 +54,7 @@ public class AiController { * @return AI response */ @GetMapping(path = "/get", produces = {TEXT_EVENT_STREAM_VALUE}) - public Flux> requestAi(@RequestParam("text") String text, + public Flux requestAi(@RequestParam("text") String text, @RequestParam(value = "type", required = false) String currentlyDisabledType) { AiService aiServiceImplBean = aiServiceFactory.getAiServiceImplBean(type); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index bf47a3264b2..e9f96d67ed9 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -31,7 +31,6 @@ import java.util.List; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.manager.Monitor; -import org.apache.hertzbeat.manager.service.AiService; import org.apache.hertzbeat.manager.service.MonitorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; @@ -49,8 +48,6 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; -import javax.annotation.Resource; - /** * Monitor and manage batch API */ @@ -66,9 +63,6 @@ public class MonitorsController { @Autowired private MonitorService monitorService; - @Resource(name = "AlibabaAiServiceImpl") - private AiService aiService; - @GetMapping @Operation(summary = "Obtain a list of monitoring information based on query filter items", description = "Obtain a list of monitoring information based on query filter items") @@ -165,8 +159,11 @@ public ResponseEntity>> getAppMonitors( public ResponseEntity> deleteMonitors( @Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List ids ) { - aiService.requestAi("你是谁啊"); - return null; + if (ids != null && !ids.isEmpty()) { + monitorService.cancelManageMonitors(new HashSet<>(ids)); + } + Message message = Message.success(); + return ResponseEntity.ok(message); } @DeleteMapping("manage") diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java index df04e7cbf8e..8d30624c00b 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiRequestParamDTO.java @@ -1,12 +1,29 @@ +/* + * 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; -import java.util.List; /** * Alibaba Ai Request param @@ -73,8 +90,10 @@ public static class Parameters { private float temperature; /** - * The Internet search service is built into the model, and this parameter controls whether the model refers to the Internet search results when generating text. The value can be: - * true: If Internet search is enabled, the model uses the search results as reference information in the text generation process, but the model "decides" whether to use the Internet search results based on its internal logic. + * The Internet search service is built into the model, and this parameter controls whether the model refers + * to the Internet search results when generating text. The value can be: + * true: If Internet search is enabled, the model uses the search results as reference information in the text + * generation process, but the model "decides" whether to use the Internet search results based on its internal logic. * false: Turn off Internet search. */ @JsonProperty("enable_search") @@ -86,6 +105,14 @@ public static class Parameters { @JsonProperty("result_format") private String resultFormat; + /** + * Control whether incremental output is enabled in stream output mode, that is, whether the subsequent output content contains + * the output content. If the value is set to True, the incremental output mode will be enabled, and the subsequent output will + * not contain the output content, you need to splice the overall output by yourself. Set to False to contain the output. + */ + @JsonProperty("incremental_output") + private boolean incrementalOutput; + } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java index 7fd03a14aaa..a27b44a9680 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java @@ -1,13 +1,29 @@ +/* + * 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; -import java.util.List; - /** * AliAiResponse */ @@ -16,16 +32,30 @@ @NoArgsConstructor public class AliAiResponse { - /** - * response message + * response */ - private List choices; + private AliAiOutput output; /** * Returns the number of tokens invoked by the model at the end. */ - private AliAiResponse.Tokens usage; + private Tokens usage; + + /** + * AliAiOutput + */ + @Data + @AllArgsConstructor + @NoArgsConstructor + public class AliAiOutput { + + /** + * response message + */ + private List choices; + + } /** * Choice @@ -33,7 +63,8 @@ public class AliAiResponse { @Data @AllArgsConstructor @NoArgsConstructor - public class Choice { + public static class Choice { + /** * Stop cause: * null: being generated 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 22ccd55d799..ad2292ac774 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,7 +19,6 @@ import org.apache.hertzbeat.common.constants.AiTypeEnum; -import org.springframework.http.codec.ServerSentEvent; import reactor.core.publisher.Flux; @@ -39,6 +38,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 12e823862da..06e79a9925e 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 @@ -1,26 +1,43 @@ +/* + * 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.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.*; +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.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; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; -import javax.annotation.PostConstruct; -import java.util.List; -import java.util.Objects; - /** * alibaba Ai @@ -40,11 +57,11 @@ public class AlibabaAiServiceImpl implements AiService { @PostConstruct private void init() { this.webClient = WebClient.builder() - .baseUrl(AiConstants.ZhiPuConstants.URL) + .baseUrl(AiConstants.AliAiConstants.URL) .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey) //sse - .defaultHeader(HttpHeaders.ACCEPT,MediaType.TEXT_EVENT_STREAM_VALUE) + .defaultHeader(HttpHeaders.ACCEPT, MediaType.TEXT_EVENT_STREAM_VALUE) .exchangeStrategies(ExchangeStrategies.builder() .codecs(item -> item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) .build()) @@ -57,7 +74,7 @@ public AiTypeEnum getType() { } @Override - public Flux> requestAi(String text) { + public Flux requestAi(String text) { checkParam(text, apiKey); AliAiRequestParamDTO aliAiRequestParamDTO = AliAiRequestParamDTO.builder() @@ -70,37 +87,30 @@ public Flux> requestAi(String text) { .temperature(AiConstants.AliAiConstants.TEMPERATURE) .enableSearch(true) .resultFormat("message") + .incrementalOutput(true) .build()) .build(); - System.out.println(JSON.toJSONString(aliAiRequestParamDTO) + "apikey" + apiKey); - webClient.post() + + return webClient.post() .body(BodyInserters.fromValue(aliAiRequestParamDTO)) .retrieve() - .bodyToFlux(String.class) - .doOnNext(System.out::println) // 打印每个元素 - .subscribe(); + .bodyToFlux(AliAiResponse.class) + .map(aliAiResponse -> { + if (Objects.nonNull(aliAiResponse)) { + List choices = aliAiResponse.getOutput().getChoices(); + if (CollectionUtils.isEmpty(choices)) { + return ""; + } + return choices.get(0).getMessage().getContent(); + } + return ""; + }) + .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); - return null; } - private ServerSentEvent convertToResponse(String aiRes) { - try { - ZhiPuAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, ZhiPuAiResponse.class); - if (Objects.nonNull(zhiPuAiResponse)) { - ZhiPuAiResponse.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()); - } - - return ServerSentEvent.builder().build(); - } 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 0e5f6953ff8..f1407672028 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 @@ -31,7 +31,6 @@ 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; @@ -72,7 +71,7 @@ public AiTypeEnum getType() { } @Override - public Flux> requestAi(String text) { + public Flux requestAi(String text) { checkParam(text, model, apiKey); ZhiPuRequestParamDTO zhiPuRequestParamDTO = ZhiPuRequestParamDTO.builder() .model(model) @@ -88,25 +87,24 @@ public Flux> requestAi(String text) { .retrieve() .bodyToFlux(String.class) .filter(aiResponse -> !"[DONE]".equals(aiResponse)) - .map(this::convertToResponse); + .map(this::convertToResponse) + .doOnError(error -> log.info("AiResponse Exception:{}", error.toString())); + } - private ServerSentEvent convertToResponse(String aiRes) { + private String convertToResponse(String aiRes) { try { ZhiPuAiResponse zhiPuAiResponse = JSON.parseObject(aiRes, ZhiPuAiResponse.class); if (Objects.nonNull(zhiPuAiResponse)) { ZhiPuAiResponse.Choice choice = zhiPuAiResponse.getChoices().get(0); - String content = choice.getDelta().getContent(); - return ServerSentEvent.builder() - .data(content) - .build(); + return choice.getDelta().getContent(); } } catch (Exception e) { log.info("convertToResponse Exception:{}", e.toString()); } - return ServerSentEvent.builder().build(); + return ""; } diff --git a/manager/src/main/resources/application-test.yml b/manager/src/main/resources/application-test.yml index b81102bf5a1..0faf4d01f0b 100644 --- a/manager/src/main/resources/application-test.yml +++ b/manager/src/main/resources/application-test.yml @@ -88,9 +88,9 @@ scheduler: #AI config aiConfig: - #AI Type:zhiPu = 0 - type: 0 + #AI Type:zhiPu + type: zhiPu #Model name:glm-4 model: glm-4 #api key - api-key: \ No newline at end of file + api-key: xxx \ No newline at end of file diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index c4663d32df0..c1089081d78 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -200,11 +200,12 @@ scheduler: enabled: true port: 1158 -#AI config +# AI config +# See the documentation for details : https://hertzbeat.apache.org/zh-cn/docs/help/aiConfig aiConfig: - #AI Type:zhiPu = 0 - type: 0 - #Model name:glm-4 - model: glm-4 - #api key + # AI Type:zhiPu、alibabaAi + type: + # Model name:glm-4、qwen-turbo + model: + # api key api-key: \ No newline at end of file diff --git a/manager/src/main/resources/sureness.yml b/manager/src/main/resources/sureness.yml index c7253ff88c8..f967f632a8b 100644 --- a/manager/src/main/resources/sureness.yml +++ b/manager/src/main/resources/sureness.yml @@ -63,6 +63,7 @@ resourceRole: # rule: api===method # eg: /api/v1/source3===get means /api/v1/source3===get can be access by anyone, no need auth. excludedResource: + - /api/ai/**===* - /api/alerts/report/**===* - /api/account/auth/**===* - /api/i18n/**===get From a1112a4432c6325dd3dd57c61321b2851019a704 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Thu, 27 Jun 2024 22:05:45 +0800 Subject: [PATCH 21/29] [feature] :support alibaba Ai --- .../org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java index a27b44a9680..2654879aaf5 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/AliAiResponse.java @@ -73,6 +73,10 @@ public static class Choice { */ @JsonProperty("finish_reason") private String finishReason; + + /** + * response message + */ private AiMessage message; } From cc8c96cc11a76239acece1c37141a0bf3d9ea400 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Fri, 28 Jun 2024 20:05:08 +0800 Subject: [PATCH 22/29] [feature] :support alibaba Ai --- .../java/org/apache/hertzbeat/manager/service/AiService.java | 1 - 1 file changed, 1 deletion(-) 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 ea3d4a66d5a..ad2292ac774 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,7 +19,6 @@ import org.apache.hertzbeat.common.constants.AiTypeEnum; -import org.springframework.http.codec.ServerSentEvent; import reactor.core.publisher.Flux; From 615078c5ab14e66b2e67d5f53d263aa45cc1ddc3 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Sun, 30 Jun 2024 14:54:01 +0800 Subject: [PATCH 23/29] [feature] :support alibaba Ai --- .../common/constants/AiConstants.java | 31 ++++++ .../common/constants/AiTypeEnum.java | 7 +- .../pojo/dto/KimiAiRequestParamDTO.java | 55 +++++++++++ .../manager/pojo/dto/KimiAiResponse.java | 77 +++++++++++++++ .../manager/service/KimiAiServiceImpl.java | 95 +++++++++++++++++++ manager/src/main/resources/application.yml | 2 +- 6 files changed, 265 insertions(+), 2 deletions(-) create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiRequestParamDTO.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiResponse.java create mode 100644 manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.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 621085b063e..bf8f0893009 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 @@ -81,5 +81,36 @@ interface AliAiConstants { float TEMPERATURE = 0.9f; + } + + /** + * Kimi Ai constants + */ + interface KimiAiConstants { + + /** + * Kimi Ai URL + */ + String URL = "https://api.moonshot.cn/v1/chat/completions"; + + /** + * request role param + */ + String REQUEST_ROLE = "user"; + + /** + * The model outputs the maximum tokens, with a maximum output of 8192 and a default value of 1024 + */ + Integer MAX_TOKENS = 1024; + + /** + * 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 + */ + float TEMPERATURE = 0.9f; + + } } diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java b/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java index 87d7fc4d813..6225b229756 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/AiTypeEnum.java @@ -30,7 +30,12 @@ public enum AiTypeEnum { /** * alibabaAi */ - alibabaAi; + alibabaAi, + + /** + * Moonshot AI + */ + kimiAi; /** 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/KimiAiRequestParamDTO.java new file mode 100644 index 00000000000..e4129849910 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiRequestParamDTO.java @@ -0,0 +1,55 @@ +package org.apache.hertzbeat.manager.pojo.dto; + + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * zhiPu Request param + */ + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class KimiAiRequestParamDTO { + + /** + * 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/KimiAiResponse.java b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiResponse.java new file mode 100644 index 00000000000..a82331a3aa7 --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/KimiAiResponse.java @@ -0,0 +1,77 @@ +package org.apache.hertzbeat.manager.pojo.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * ZhiPuAiResponse + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class KimiAiResponse { + + /** + * 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/service/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java new file mode 100644 index 00000000000..4b9c513b20b --- /dev/null +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java @@ -0,0 +1,95 @@ +package org.apache.hertzbeat.manager.service; + +import com.alibaba.fastjson.JSON; +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.*; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.ExchangeStrategies; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Flux; +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.Objects; + +/** + * Kimi Ai + */ +@Service("KimiAiServiceImpl") +@Slf4j +public class KimiAiServiceImpl implements AiService { + + @Value("${aiConfig.model:moonshot-v1-8k}") + private String model; + @Value("${aiConfig.api-key}") + private String apiKey; + + + private WebClient webClient; + + @PostConstruct + private void init() { + this.webClient = WebClient.builder() + .baseUrl(AiConstants.KimiAiConstants.URL) + .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey) + .exchangeStrategies(ExchangeStrategies.builder() + .codecs(item -> item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) + .build()) + .build(); + System.out.println(model); + } + + @Override + public AiTypeEnum getType() { + return AiTypeEnum.kimiAi; + } + + @Override + public Flux requestAi(String text) { + + checkParam(text, apiKey); + KimiAiRequestParamDTO zhiPuRequestParamDTO = KimiAiRequestParamDTO.builder() + .model(model) + //sse + .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())); + + } + + 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()); + } + 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/resources/application.yml b/manager/src/main/resources/application.yml index c1089081d78..eade22030f4 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -206,6 +206,6 @@ aiConfig: # AI Type:zhiPu、alibabaAi type: # Model name:glm-4、qwen-turbo - model: + model: glm-4 # api key api-key: \ No newline at end of file From 4232fd94a74b275009fb1d957dbf61518fa62aa9 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Mon, 1 Jul 2024 21:45:41 +0800 Subject: [PATCH 24/29] [feature] : support Kimi Ai --- .../org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java index 4b9c513b20b..93eced7f479 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java @@ -57,7 +57,6 @@ public Flux requestAi(String text) { checkParam(text, apiKey); KimiAiRequestParamDTO zhiPuRequestParamDTO = KimiAiRequestParamDTO.builder() .model(model) - //sse .stream(Boolean.TRUE) .maxTokens(AiConstants.KimiAiConstants.MAX_TOKENS) .temperature(AiConstants.KimiAiConstants.TEMPERATURE) From eeda17fe72f5592fc78dc19f2412c21d8e67e70b Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Mon, 1 Jul 2024 22:08:14 +0800 Subject: [PATCH 25/29] [feature] : support Kimi Ai --- .../pojo/dto/KimiAiRequestParamDTO.java | 21 ++++++++++++--- .../manager/pojo/dto/KimiAiResponse.java | 20 ++++++++++++-- .../manager/service/KimiAiServiceImpl.java | 27 ++++++++++++++++--- 3 files changed, 59 insertions(+), 9 deletions(-) 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/KimiAiRequestParamDTO.java index e4129849910..29153ad4579 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/KimiAiRequestParamDTO.java @@ -1,14 +1,29 @@ -package org.apache.hertzbeat.manager.pojo.dto; +/* + * 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; -import java.util.List; - /** * zhiPu Request param */ 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/KimiAiResponse.java index a82331a3aa7..58458565d64 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/KimiAiResponse.java @@ -1,12 +1,28 @@ +/* + * 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; -import java.util.List; - /** * ZhiPuAiResponse */ diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java index 93eced7f479..db5ec67872f 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java @@ -1,10 +1,32 @@ +/* + * 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.service; 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.*; +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.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -14,9 +36,6 @@ import org.springframework.web.reactive.function.client.ExchangeStrategies; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Flux; -import javax.annotation.PostConstruct; -import java.util.List; -import java.util.Objects; /** * Kimi Ai From eb73a34f863cee91006e9c5955e60c862bf1fb0f Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Tue, 2 Jul 2024 20:53:40 +0800 Subject: [PATCH 26/29] [feature] : support Kimi Ai --- .../org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java index db5ec67872f..8e7b01b038b 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java @@ -46,10 +46,10 @@ public class KimiAiServiceImpl implements AiService { @Value("${aiConfig.model:moonshot-v1-8k}") private String model; + @Value("${aiConfig.api-key}") private String apiKey; - private WebClient webClient; @PostConstruct From 18fab8c8d760c5ec61fbc5383f6980972f0c1597 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Tue, 2 Jul 2024 21:21:57 +0800 Subject: [PATCH 27/29] [feature] : support kimi Ai --- .../org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java | 1 - manager/src/main/resources/application.yml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java index 8e7b01b038b..31820487230 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/KimiAiServiceImpl.java @@ -62,7 +62,6 @@ private void init() { .codecs(item -> item.defaultCodecs().maxInMemorySize(16 * 1024 * 1024)) .build()) .build(); - System.out.println(model); } @Override diff --git a/manager/src/main/resources/application.yml b/manager/src/main/resources/application.yml index e326fe490af..90d6e43e703 100644 --- a/manager/src/main/resources/application.yml +++ b/manager/src/main/resources/application.yml @@ -203,7 +203,7 @@ scheduler: # AI config # See the documentation for details : https://hertzbeat.apache.org/zh-cn/docs/help/aiConfig aiConfig: - # AI Type:zhiPu、alibabaAi、 + # AI Type:zhiPu、alibabaAi、kimiAi type: # Model name:glm-4、qwen-turbo、moonshot-v1-8k model: glm-4 From 23a83215acfcddfaf9a21792673d24bed395e63c Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Tue, 2 Jul 2024 21:23:26 +0800 Subject: [PATCH 28/29] [feature] : support kimi Ai --- manager/src/main/resources/sureness.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/manager/src/main/resources/sureness.yml b/manager/src/main/resources/sureness.yml index f967f632a8b..c7253ff88c8 100644 --- a/manager/src/main/resources/sureness.yml +++ b/manager/src/main/resources/sureness.yml @@ -63,7 +63,6 @@ resourceRole: # rule: api===method # eg: /api/v1/source3===get means /api/v1/source3===get can be access by anyone, no need auth. excludedResource: - - /api/ai/**===* - /api/alerts/report/**===* - /api/account/auth/**===* - /api/i18n/**===get From bfb0c37ed007e044378ac72c4618735ed8f188a5 Mon Sep 17 00:00:00 2001 From: linDong <56677297@qq.com> Date: Wed, 3 Jul 2024 20:56:43 +0800 Subject: [PATCH 29/29] [feature] : support spark Ai --- .../hertzbeat/manager/controller/MonitorsController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index 9ab7eac9719..46e1129b3d7 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -160,7 +160,7 @@ public ResponseEntity> deleteMonitors( @Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List ids ) { if (ids != null && !ids.isEmpty()) { - monitorService.cancelManageMonitors(new HashSet<>(ids)); + monitorService.deleteMonitors(new HashSet<>(ids)); } Message message = Message.success(); return ResponseEntity.ok(message); @@ -173,7 +173,7 @@ public ResponseEntity> cancelManageMonitors( @Parameter(description = "Monitoring ID List", example = "6565463543") @RequestParam(required = false) List ids ) { if (ids != null && !ids.isEmpty()) { - monitorService.deleteMonitors(new HashSet<>(ids)); + monitorService.cancelManageMonitors(new HashSet<>(ids)); } Message message = Message.success(); return ResponseEntity.ok(message);