From 5c38593cad55879a188dd88824a2cde415b68c7a Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Thu, 22 Aug 2024 10:51:02 +0800 Subject: [PATCH 1/9] =?UTF-8?q?fix:=20=E5=90=91=E9=87=8F=E5=BA=93=E7=BB=B4?= =?UTF-8?q?=E6=8A=A4=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bigmodel/api/wrapper/BaseSyncWrapper.java | 21 ++++++ .../bigmodel/maintenance/entity/ControlEntity.java | 2 +- hzims-service/hzims-big-model/pom.xml | 5 ++ .../control/DataSourceExecuteController.java | 33 ---------- .../business/control/RemoteController.java | 34 ---------- .../controller/DataSourceExecuteController.java | 33 ++++++++++ .../business/controller/RemoteController.java | 34 ++++++++++ .../business/wrapper/CanvasSyncWrapper.java | 21 ++++++ .../bigmodel/configuration/WeaviateConfig.java | 20 ++++++ .../maintenance/entity/SyncPropertyEntity.java | 40 ++++++++++++ .../maintenance/service/TableColumnService.java | 6 +- .../bigmodel/zhipuai/config/ZhipuAiConfig.java | 35 ++++++++++ .../operational/station/aspect/AddVideoAspect.java | 76 ---------------------- .../station/aspect/VideoSyncAspect.java | 58 +++++++++++++++++ .../station/wrapper/VideoSyncWrapper.java | 38 +++++++++++ 15 files changed, 309 insertions(+), 147 deletions(-) create mode 100644 hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/wrapper/BaseSyncWrapper.java delete mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/DataSourceExecuteController.java delete mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/RemoteController.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/DataSourceExecuteController.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/RemoteController.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/wrapper/CanvasSyncWrapper.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/SyncPropertyEntity.java delete mode 100644 hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/AddVideoAspect.java create mode 100644 hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/VideoSyncAspect.java create mode 100644 hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/wrapper/VideoSyncWrapper.java diff --git a/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/wrapper/BaseSyncWrapper.java b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/wrapper/BaseSyncWrapper.java new file mode 100644 index 0000000..4cef0d1 --- /dev/null +++ b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/api/wrapper/BaseSyncWrapper.java @@ -0,0 +1,21 @@ +package com.hnac.hzims.bigmodel.api.wrapper; + +import com.hnac.hzims.bigmodel.api.dto.SyncDTO; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * @Author: huangxing + * @Date: 2024/08/20 09:23 + * @Describe 对象转换为同步对象基础类 + */ +public abstract class BaseSyncWrapper { + + public abstract S convert(O obj); + + public List listVO(List list) { + return list.stream().map(this::convert).collect(Collectors.toList()); + } + +} diff --git a/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/ControlEntity.java b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/ControlEntity.java index 889b04a..d2176e8 100644 --- a/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/ControlEntity.java +++ b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/ControlEntity.java @@ -26,7 +26,7 @@ public class ControlEntity extends TenantEntity implements Serializable { @ApiModelProperty(value = "itemId",required = true) private String itemId; - @ApiModelProperty(value = "站点名称",required = true) + @ApiModelProperty(value = "站点名称") @QueryField(condition = SqlCondition.LIKE) private String stationName; diff --git a/hzims-service/hzims-big-model/pom.xml b/hzims-service/hzims-big-model/pom.xml index d3624b5..61973c4 100644 --- a/hzims-service/hzims-big-model/pom.xml +++ b/hzims-service/hzims-big-model/pom.xml @@ -83,6 +83,11 @@ oapi-java-sdk release-V4-2.0.2 + + io.weaviate + client + 4.7.0 + diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/DataSourceExecuteController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/DataSourceExecuteController.java deleted file mode 100644 index 4efcf3b..0000000 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/DataSourceExecuteController.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.hnac.hzims.bigmodel.business.control; - -import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; -import com.hnac.hzims.bigmodel.business.service.DataSourceService; -import com.hnac.hzims.bigmodel.business.vo.SqlVO; -import io.swagger.annotations.ApiOperation; -import lombok.AllArgsConstructor; -import org.springblade.core.tool.api.R; -import org.springframework.web.bind.annotation.*; - -import javax.validation.Valid; -import java.util.List; -import java.util.Map; - -/** - * @Author: huangxing - * @Date: 2024/06/28 14:07 - */ -@RestController -@RequestMapping("/dataSource/execute") -@AllArgsConstructor -public class DataSourceExecuteController { - - private final DataSourceService dataSourceService; - - @PostMapping("/executeQuery") - @ApiOperation("执行大模型sql") - @ApiOperationSupport(order = 1) - public R>> executeQuery(@RequestBody @Valid SqlVO sqlVO) { - return R.data(dataSourceService.queryListOnSpecificDataSource(sqlVO)); - } - -} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/RemoteController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/RemoteController.java deleted file mode 100644 index 3b4422c..0000000 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/control/RemoteController.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.hnac.hzims.bigmodel.business.control; - -import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; -import com.hnac.hzims.bigmodel.BigModelConstants; -import com.hnac.hzims.bigmodel.business.service.RemoteService; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import lombok.AllArgsConstructor; -import org.springblade.core.tool.api.R; -import org.springblade.system.dto.ControlDTO; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -/** - * @Author: huangxing - * @Date: 2024/06/24 14:42 - */ -@AllArgsConstructor -@RequestMapping("/remote") -@RestController -@Api(value = "数据平台遥控指令管理",tags = "数据平台遥控指令管理") -public class RemoteController { - - private final RemoteService remoteService; - - @ApiOperation("下发遥控指令") - @ApiOperationSupport(order = 1) - @PostMapping("/sendRemoteControl") - public R sendRemoteControl(ControlDTO controlDTO) { - return remoteService.sendRemoteControl(controlDTO); - } - -} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/DataSourceExecuteController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/DataSourceExecuteController.java new file mode 100644 index 0000000..47e7cc5 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/DataSourceExecuteController.java @@ -0,0 +1,33 @@ +package com.hnac.hzims.bigmodel.business.controller; + +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import com.hnac.hzims.bigmodel.business.service.DataSourceService; +import com.hnac.hzims.bigmodel.business.vo.SqlVO; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springblade.core.tool.api.R; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.List; +import java.util.Map; + +/** + * @Author: huangxing + * @Date: 2024/06/28 14:07 + */ +@RestController +@RequestMapping("/dataSource/execute") +@AllArgsConstructor +public class DataSourceExecuteController { + + private final DataSourceService dataSourceService; + + @PostMapping("/executeQuery") + @ApiOperation("执行大模型sql") + @ApiOperationSupport(order = 1) + public R>> executeQuery(@RequestBody @Valid SqlVO sqlVO) { + return R.data(dataSourceService.queryListOnSpecificDataSource(sqlVO)); + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/RemoteController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/RemoteController.java new file mode 100644 index 0000000..ac0c0b3 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/controller/RemoteController.java @@ -0,0 +1,34 @@ +package com.hnac.hzims.bigmodel.business.controller; + +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import com.hnac.hzims.bigmodel.business.service.RemoteService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springblade.core.tool.api.R; +import org.springblade.system.dto.ControlDTO; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @Author: huangxing + * @Date: 2024/06/24 14:42 + */ +@AllArgsConstructor +@RequestMapping("/remote") +@RestController +@Api(value = "数据平台遥控指令管理",tags = "数据平台遥控指令管理") +public class RemoteController { + + private final RemoteService remoteService; + + @ApiOperation("下发遥控指令") + @ApiOperationSupport(order = 1) + @PostMapping("/sendRemoteControl") + public R sendRemoteControl(@RequestBody ControlDTO controlDTO) { + return remoteService.sendRemoteControl(controlDTO); + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/wrapper/CanvasSyncWrapper.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/wrapper/CanvasSyncWrapper.java new file mode 100644 index 0000000..306e13d --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/wrapper/CanvasSyncWrapper.java @@ -0,0 +1,21 @@ +package com.hnac.hzims.bigmodel.business.wrapper; + +import com.hnac.hzims.bigmodel.api.dto.CanvasSyncDTO; +import com.hnac.hzims.bigmodel.api.wrapper.BaseSyncWrapper; +import com.hnac.hzinfo.sdk.v5.scada.vo.CanvasVO; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +/** + * @Author: huangxing + * @Date: 2024/08/20 16:49 + */ +@Component +@AllArgsConstructor +public class CanvasSyncWrapper extends BaseSyncWrapper { + + @Override + public CanvasSyncDTO convert(CanvasVO obj) { + return null; + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java new file mode 100644 index 0000000..022663a --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java @@ -0,0 +1,20 @@ +package com.hnac.hzims.bigmodel.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +/** + * @Author: huangxing + * @Date: 2024/08/21 15:22 + */ +@Configuration +public class WeaviateConfig { + + @Bean + public WebClient weaviateClient() { + return WebClient.create("http://192.168.60.16:9992"); + } + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/SyncPropertyEntity.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/SyncPropertyEntity.java new file mode 100644 index 0000000..c800041 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/entity/SyncPropertyEntity.java @@ -0,0 +1,40 @@ +package com.hnac.hzims.bigmodel.maintenance.entity; + +import com.baomidou.mybatisplus.annotation.TableName; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springblade.core.tenant.mp.TenantEntity; + +import java.io.Serializable; + +/** + * @Author: huangxing + * @Date: 2024/08/21 08:33 + */ +@Data +@EqualsAndHashCode +@ApiModel(value = "同步向量库基础配置",description = "同步向量库基础配置") +@TableName("") +public class SyncPropertyEntity extends TenantEntity implements Serializable { + + @ApiModelProperty("配置编号") + private String code; + + @ApiModelProperty("数据源") + private String datasource; + + @ApiModelProperty("数据源名称") + private String datasourceName; + + @ApiModelProperty("数据库表名") + private String tableName; + + @ApiModelProperty("向量数据库表名,由小写字母以及_组成") + private String vectorTableName; + + @ApiModelProperty("表描述") + private String tableDesc; + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableColumnService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableColumnService.java index 91f04fb..b8d4fc3 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableColumnService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/maintenance/service/TableColumnService.java @@ -20,17 +20,17 @@ public class TableColumnService { private final DataSourceService dataSourceService; public List> getDatabase(String datasource) { - String sql = "select distinct `table_schema` as tableName from `information_schema`.`tables` where `table_schema` <> 'information_schema';"; + String sql = "select distinct UPPER(`table_schema`) as tableName from `information_schema`.`tables` where `table_schema` <> 'information_schema';"; return dataSourceService.queryListOnSpecificDataSource(sql,datasource); } public List> getTables(String datasource,String database) { - String sql = "select distinct `table_name` as tableName from `information_schema`.`tables` where `table_schema` = '" + database + "';"; + String sql = "select distinct UPPER(`table_name`) as tableName from `information_schema`.`tables` where `table_schema` = '" + database + "';"; return dataSourceService.queryListOnSpecificDataSource(sql,datasource); } public List> getColumns(String datasource,String database,String tableName) { - String sql = "select distinct `column_name` as columnName,`column_comment` as columnComment from `information_schema`.`COLUMNS` where `TABLE_SCHEMA` = '" + database + "' and `TABLE_NAME` = '" + tableName + "';"; + String sql = "select distinct UPPER(`column_name`) as columnName,UPPER(`column_comment`) as columnComment from `information_schema`.`COLUMNS` where `TABLE_SCHEMA` = '" + database + "' and `TABLE_NAME` = '" + tableName + "';"; return dataSourceService.queryListOnSpecificDataSource(sql,datasource); } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java index 1c5a8b2..b28a3a5 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java @@ -1,11 +1,22 @@ package com.hnac.hzims.bigmodel.zhipuai.config; +import com.alibaba.fastjson.JSON; import com.zhipu.oapi.ClientV4; +import io.weaviate.client.Config; +import io.weaviate.client.WeaviateAuthClient; +import io.weaviate.client.WeaviateClient; +import io.weaviate.client.base.Result; +import io.weaviate.client.v1.auth.exception.AuthException; +import io.weaviate.client.v1.data.model.WeaviateObject; +import io.weaviate.client.v1.misc.model.Meta; import lombok.AllArgsConstructor; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.HashMap; +import java.util.List; + /** * @Author: ypj * @Date: 2024/8/9 17:01 @@ -21,4 +32,28 @@ public class ZhipuAiConfig { public ClientV4 clientV4() { return new ClientV4.Builder(properties.getApiSecret()).build(); } + +// public static void main(String[] args) throws AuthException { +// Config config = new Config("http", "192.168.60.16:9992"); +// WeaviateClient client = WeaviateAuthClient.apiKey(config,"123"); +// // 新增 HZIMS_TEST +//// Result craeteTableTest = client.data().creator().withClassName("HZIMS_TEST").run(); +//// System.out.println(JSON.toJSONString(craeteTableTest)); +//// Result> searchResult = client.data().objectsGetter().withClassName("HZIMS_TEST").run(); +//// System.out.println(JSON.toJSONString(searchResult)); +// // 新增数据 +//// HashMap hashMap = new HashMap() {{ +//// put("id_", "13412423423412"); +//// put("name", "test"); +//// }}; +//// Result hzimsTest = client.data().creator().withClassName("HZIMS_TEST").withProperties(hashMap).withVector(new Float[]{}).run(); +//// System.out.println(JSON.toJSONString(hzimsTest)); +//// Result> searchResult = client.data().objectsGetter().withClassName("HZIMS_TEST").run(); +//// System.out.println(JSON.toJSONString(searchResult)); +// // 删除数据 +// Result hzimsTest = client.data().deleter().withClassName("HZIMS_TEST").withID("14c6847d-b758-475f-968d-64b0e193e43e").run(); +// System.out.println(JSON.toJSONString(hzimsTest)); +// Result> searchResult = client.data().objectsGetter().withClassName("HZIMS_TEST").run(); +// System.out.println(JSON.toJSONString(searchResult)); +// } } diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/AddVideoAspect.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/AddVideoAspect.java deleted file mode 100644 index 272eda4..0000000 --- a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/AddVideoAspect.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.hnac.hzims.operational.station.aspect; - -import com.alibaba.fastjson.JSON; -import com.google.common.collect.Lists; -import com.hnac.hzims.bigmodel.api.constants.MqttTopicConstants; -import com.hnac.hzims.bigmodel.api.dto.VideoSyncDTO; -import com.hnac.hzims.operational.station.entity.StationVideoTypeEntity; -import com.hnac.hzims.operational.station.feign.IStationClient; -import com.hnac.hzims.operational.station.service.IStationService; -import com.hnac.hzims.operational.station.vo.StationVO; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.annotation.After; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.springblade.core.tool.utils.BeanUtil; -import org.springblade.core.tool.utils.Func; -import org.springblade.mqtt.producer.IMqttSender; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; - -/** - * @Author: huangxing - * @Date: 2024/08/09 14:02 - */ -@Aspect -@Component -@AllArgsConstructor -@Slf4j -public class AddVideoAspect { - - private final IStationService stationService; - private final IMqttSender mqttSender; - - @Pointcut("execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.saveBatch(..)) " + - "|| execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.save(..)) " + - "|| execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.updateById(..)) " + - "|| execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.updateBatchById(..))") - private void pointCut() { - - } - - @After("pointCut()") - public void after(JoinPoint point) { - Object[] args = point.getArgs(); - if(args.length == 1) { - Object arg = args[0]; - if(arg instanceof StationVideoTypeEntity) { - List videoSyncDTOS = Lists.newArrayList(this.convert((StationVideoTypeEntity) arg)); - mqttSender.sendToMqtt(MqttTopicConstants.TOPIC_VIDEO_INSERT, JSON.toJSONString(videoSyncDTOS)); - } - else if(arg instanceof List) { - List videoList = (List) args[0]; - List videoSyncDTOS = videoList.stream().map(this::convert).collect(Collectors.toList()); - mqttSender.sendToMqtt(MqttTopicConstants.TOPIC_VIDEO_INSERT, JSON.toJSONString(videoSyncDTOS)); - } - } - } - - private VideoSyncDTO convert(StationVideoTypeEntity entity) { - VideoSyncDTO videoSyncDTO = BeanUtil.copy(entity,VideoSyncDTO.class); - StationVO station = stationService.getStationByCode(entity.getStationId()); - String stationName = Optional.ofNullable(station).map(StationVO::getName).orElse(""); - videoSyncDTO.setId(String.valueOf(entity.getId())); - videoSyncDTO.setItemId(String.valueOf(entity.getId())); - videoSyncDTO.setStationName(stationName); - videoSyncDTO.setItemName(stationName + " " + videoSyncDTO.getName()); - return videoSyncDTO; - } - -} diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/VideoSyncAspect.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/VideoSyncAspect.java new file mode 100644 index 0000000..b5b2e7b --- /dev/null +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/aspect/VideoSyncAspect.java @@ -0,0 +1,58 @@ +package com.hnac.hzims.operational.station.aspect; + +import com.alibaba.fastjson.JSON; +import com.google.common.collect.Lists; +import com.hnac.hzims.bigmodel.api.constants.MqttTopicConstants; +import com.hnac.hzims.bigmodel.api.dto.VideoSyncDTO; +import com.hnac.hzims.operational.station.entity.StationVideoTypeEntity; +import com.hnac.hzims.operational.station.wrapper.VideoSyncWrapper; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springblade.mqtt.producer.IMqttSender; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * @Author: huangxing + * @Date: 2024/08/09 14:02 + */ +@Aspect +@Component +@AllArgsConstructor +@Slf4j +public class VideoSyncAspect { + + private final VideoSyncWrapper wrapper; + private final IMqttSender mqttSender; + + @Pointcut("execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.saveBatch(..)) " + + "|| execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.save(..)) " + + "|| execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.updateById(..)) " + + "|| execution(* com.hnac.hzims.operational.station.service.IStationVideoTypeService.updateBatchById(..))") + private void pointCut() { + + } + + @After("pointCut()") + public void after(JoinPoint point) { + Object[] args = point.getArgs(); + if(args.length == 1) { + Object arg = args[0]; + if(arg instanceof StationVideoTypeEntity) { + List videoSyncDTOS = Lists.newArrayList(wrapper.convert((StationVideoTypeEntity) arg)); + mqttSender.sendToMqtt(MqttTopicConstants.TOPIC_VIDEO_INSERT, JSON.toJSONString(videoSyncDTOS)); + } + else if(arg instanceof List) { + List videoList = (List) args[0]; + List videoSyncDTOS = wrapper.listVO(videoList); + mqttSender.sendToMqtt(MqttTopicConstants.TOPIC_VIDEO_INSERT, JSON.toJSONString(videoSyncDTOS)); + } + } + } + +} diff --git a/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/wrapper/VideoSyncWrapper.java b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/wrapper/VideoSyncWrapper.java new file mode 100644 index 0000000..72ce88c --- /dev/null +++ b/hzims-service/operational/src/main/java/com/hnac/hzims/operational/station/wrapper/VideoSyncWrapper.java @@ -0,0 +1,38 @@ +package com.hnac.hzims.operational.station.wrapper; + +import com.hnac.hzims.bigmodel.api.dto.VideoSyncDTO; +import com.hnac.hzims.bigmodel.api.wrapper.BaseSyncWrapper; +import com.hnac.hzims.operational.station.entity.StationVideoTypeEntity; +import com.hnac.hzims.operational.station.service.IStationService; +import com.hnac.hzims.operational.station.vo.StationVO; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.SpringUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +/** + * @Author: huangxing + * @Date: 2024/08/20 11:21 + */ +@Component +@AllArgsConstructor +public class VideoSyncWrapper extends BaseSyncWrapper { + + private final IStationService stationService; + + @Override + public VideoSyncDTO convert(StationVideoTypeEntity entity) { + VideoSyncDTO videoSyncDTO = BeanUtil.copy(entity,VideoSyncDTO.class); + StationVO station = stationService.getStationByCode(entity.getStationId()); + String stationName = Optional.ofNullable(station).map(StationVO::getName).orElse(""); + videoSyncDTO.setId(String.valueOf(entity.getId())); + videoSyncDTO.setItemId(String.valueOf(entity.getId())); + videoSyncDTO.setStationName(stationName); + videoSyncDTO.setItemName(stationName + " " + videoSyncDTO.getName()); + return videoSyncDTO; + } +} From 986536d417052adb51d1c1a111c5cf03766ec878 Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Thu, 22 Aug 2024 16:44:58 +0800 Subject: [PATCH 2/9] =?UTF-8?q?fix:=20sql=E6=89=A7=E8=A1=8C=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E8=AF=AD=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/hnac/hzims/common/service/UserAuthDataService.java | 5 +++++ .../com/hnac/hzims/bigmodel/business/service/DataSourceService.java | 1 + 2 files changed, 6 insertions(+) diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/service/UserAuthDataService.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/service/UserAuthDataService.java index 7d7de77..d793fd8 100644 --- a/hzims-biz-common/src/main/java/com/hnac/hzims/common/service/UserAuthDataService.java +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/service/UserAuthDataService.java @@ -1,9 +1,11 @@ package com.hnac.hzims.common.service; +import com.hnac.hzims.common.constant.CommonConstant; import com.hnac.hzims.common.logs.utils.StringUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.api.R; +import org.springblade.core.tool.utils.Func; import org.springblade.system.user.entity.User; import org.springblade.system.user.feign.IUserClient; import org.springframework.http.HttpStatus; @@ -22,6 +24,9 @@ public class UserAuthDataService { * @return */ public String getUserAuthDataSQL(Long userId) { + if(CommonConstant.SYSTEM_USER.equals(userId)) { + return ""; + } R r = userClient.userInfoById(userId); String sqlScript = null; if(r.getCode() != HttpStatus.OK.value() || r.getData() == null) { diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java index 23613a2..c616d0a 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java @@ -63,6 +63,7 @@ public class DataSourceService { } } } + sql = sql.replaceAll(">", ">").replaceAll("<", "<"); log.info("执行sql:{}",sql); return this.queryListOnSpecificDataSource(sql, sqlVO.getTableAuthVOList().get(0).getDatasourceName()); } From ea653a473f54926ef16c03affef632add024e59a Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Mon, 26 Aug 2024 14:58:49 +0800 Subject: [PATCH 3/9] =?UTF-8?q?fix:=20=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bigmodel/database/entity/WeaviateEntity.java | 23 +++ .../hzinfo/inspect/obj/entity/ObjectEntity.java | 2 +- .../bigmodel/configuration/BigModelInvokeApi.java | 5 + .../bigmodel/configuration/WeaviateConfig.java | 20 --- .../bigmodel/configuration/WeaviateConfigure.java | 56 +++++++ .../bigmodel/configuration/WeaviateProperties.java | 32 ++++ .../bigmodel/database/service/WeaviateService.java | 183 +++++++++++++++++++++ .../hzims/bigmodel/database/util/WeaviateUtil.java | 11 ++ .../bigmodel/zhipuai/config/ZhipuAiConfig.java | 35 ---- .../src/main/resources/template/template.yml | 2 + 10 files changed, 313 insertions(+), 56 deletions(-) create mode 100644 hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/database/entity/WeaviateEntity.java delete mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfigure.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateProperties.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java create mode 100644 hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/util/WeaviateUtil.java diff --git a/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/database/entity/WeaviateEntity.java b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/database/entity/WeaviateEntity.java new file mode 100644 index 0000000..6c2b3ba --- /dev/null +++ b/hzims-service-api/big-model-api/src/main/java/com/hnac/hzims/bigmodel/database/entity/WeaviateEntity.java @@ -0,0 +1,23 @@ +package com.hnac.hzims.bigmodel.database.entity; + +import com.alibaba.fastjson.annotation.JSONField; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.io.Serializable; + +/** + * @Author: huangxing + * @Date: 2024/08/22 19:35 + */ +@Data +@EqualsAndHashCode +public class WeaviateEntity implements Serializable { + + @JSONField(name = "item_id") + private String itemId; + + @JSONField(name = "item_name") + private String itemName; + +} diff --git a/hzims-service-api/inspect-api/src/main/java/com/hnac/hzinfo/inspect/obj/entity/ObjectEntity.java b/hzims-service-api/inspect-api/src/main/java/com/hnac/hzinfo/inspect/obj/entity/ObjectEntity.java index 4a7efdb..7ef2305 100644 --- a/hzims-service-api/inspect-api/src/main/java/com/hnac/hzinfo/inspect/obj/entity/ObjectEntity.java +++ b/hzims-service-api/inspect-api/src/main/java/com/hnac/hzinfo/inspect/obj/entity/ObjectEntity.java @@ -49,7 +49,7 @@ public class ObjectEntity extends TenantEntity { * 地理位置 */ @ApiModelProperty(value = "地理位置,长度必须为1到128") - @Size(min=1,max = 128,message ="地理位置长度必须为1到128") + // @Size(min=1,max = 128,message ="地理位置长度必须为1到128") private String address; /** * 经度(东经) diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/BigModelInvokeApi.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/BigModelInvokeApi.java index c11b4b6..b6b842a 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/BigModelInvokeApi.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/BigModelInvokeApi.java @@ -62,4 +62,9 @@ public class BigModelInvokeApi { */ private String updateKnowledge; + /** + * 计算向量 + */ + private String compute; + } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java deleted file mode 100644 index 022663a..0000000 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.hnac.hzims.bigmodel.configuration; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.reactive.function.client.WebClient; -import reactor.core.publisher.Mono; - -/** - * @Author: huangxing - * @Date: 2024/08/21 15:22 - */ -@Configuration -public class WeaviateConfig { - - @Bean - public WebClient weaviateClient() { - return WebClient.create("http://192.168.60.16:9992"); - } - -} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfigure.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfigure.java new file mode 100644 index 0000000..6bd2f14 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateConfigure.java @@ -0,0 +1,56 @@ +package com.hnac.hzims.bigmodel.configuration; + +import io.weaviate.client.Config; +import io.weaviate.client.WeaviateAuthClient; +import io.weaviate.client.WeaviateClient; +import io.weaviate.client.v1.auth.exception.AuthException; +import io.weaviate.client.v1.data.api.ObjectCreator; +import io.weaviate.client.v1.data.api.ObjectDeleter; +import io.weaviate.client.v1.data.api.ObjectUpdater; +import io.weaviate.client.v1.data.api.ObjectsGetter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @Author: huangxing + * @Date: 2024/08/22 18:38 + */ +@Configuration +public class WeaviateConfigure { + + private final WeaviateProperties weaviateProperties; + + public WeaviateConfigure(WeaviateProperties weaviateProperties) { + this.weaviateProperties = weaviateProperties; + } + + @Bean + public WeaviateClient weaviateClient() throws AuthException { + Config config = new Config(this.weaviateProperties.getSchema(), this.weaviateProperties.getHost() + ":" + this.weaviateProperties.getPort()); + return WeaviateAuthClient.apiKey(config,this.weaviateProperties.getApiKey()); + } + + @Bean + public ObjectsGetter objectsGetter() throws AuthException { + WeaviateClient weaviateClient = weaviateClient(); + return weaviateClient.data().objectsGetter(); + } + + @Bean + public ObjectCreator objectCreator() throws AuthException { + WeaviateClient weaviateClient = weaviateClient(); + return weaviateClient.data().creator(); + } + + @Bean + public ObjectDeleter deleter() throws AuthException { + WeaviateClient weaviateClient = weaviateClient(); + return weaviateClient.data().deleter(); + } + + @Bean + public ObjectUpdater updater() throws AuthException { + WeaviateClient weaviateClient = weaviateClient(); + return weaviateClient.data().updater(); + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateProperties.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateProperties.java new file mode 100644 index 0000000..468bae9 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/WeaviateProperties.java @@ -0,0 +1,32 @@ +package com.hnac.hzims.bigmodel.configuration; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * @Author: huangxing + * @Date: 2024/08/21 15:22 + */ +@Data +@Component +@ConfigurationProperties(prefix = "weaviate.datasource") +public class WeaviateProperties { + + private String schema; + + private String host; + + private String port; + + /** + * 登录认证KEY + */ + private String apiKey; + + /** + * 数据库表名前缀 + */ + private String classNamePrefix; + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java new file mode 100644 index 0000000..931a6a8 --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java @@ -0,0 +1,183 @@ +package com.hnac.hzims.bigmodel.database.service; + +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.google.common.collect.Lists; +import com.google.protobuf.ServiceException; +import com.hnac.hzims.bigmodel.configuration.BigModelInvokeApi; +import com.hnac.hzinfo.exception.HzServiceException; +import io.weaviate.client.base.Result; +import io.weaviate.client.v1.data.api.ObjectCreator; +import io.weaviate.client.v1.data.api.ObjectDeleter; +import io.weaviate.client.v1.data.api.ObjectUpdater; +import io.weaviate.client.v1.data.model.WeaviateObject; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.Func; +import org.springblade.core.tool.utils.StringUtil; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.*; +import java.util.stream.Collector; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** + * @Author: huangxing + * @Date: 2024/08/22 19:17 + */ +@RequiredArgsConstructor +@Service +@Slf4j +public class WeaviateService { + + private final ObjectCreator objectCreator; + private final ObjectUpdater objectUpdater; + private final ObjectDeleter objectDeleter; + private final BigModelInvokeApi invokeApi; + @Value("${gglm.vectorUrl}") + private final String vectorUrl; + + /** + * 对象保存向量数据库 + * @param entity 保存对象 + * @param className 保存表名 + * @param attrs 待计算的列信息 + * @return 保存操作结果 + */ + public Boolean save(Object entity, String className, List attrs) { + ObjectCreator creator = objectCreator.withClassName(className); + if(Func.isNotEmpty(attrs)) { + JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entity)); + List vectors = attrs.stream().map(attr -> jsonObject.getString(attr)).collect(Collectors.toList()); + Float[] compute = this.compute(vectors); + creator.withVector(compute); + } + Result result = creator.withProperties(BeanUtil.toMap(entity)).run(); + return !result.hasErrors(); + } + + /** + * 对象批量保存向量数据库 + * @param entities 保存对象列表 + * @param className 保存表名 + * @param attrsMap 待计算的列信息 key-向量名 value-实体类对象属性,多个按逗号分隔 + * @return 保存操作结果 + */ + public Boolean saveBatch(List entities,String className, Map attrsMap) { + ObjectCreator creator = objectCreator.withClassName(className); + List vectorStrs = Lists.newArrayList(); + List attrs = Lists.newArrayList(); + if(Func.isNotEmpty(attrsMap)) { + // 格式化数据 + attrsMap.forEach((k,v) -> attrs.add(v)); + // 解析待计算的向量字段 + entities.forEach(entity -> { + List vectorStr = attrs.stream().map(fields -> this.getFieldValue(fields, entity)).filter(Func::isNotEmpty).collect(Collectors.toList()); + vectorStrs.addAll(vectorStr); + }); + } + if(Func.isNotEmpty(vectorStrs)) { + // 若解析出来的向量存在值 + Float[] vectors = this.compute(vectorStrs); + + } else { + entities.forEach(entity -> creator.withProperties(BeanUtil.toMap(entity)).run()); + return true; + } + return false; + } + + /** + * 拆解计算出来的向量Float[] + * @param entitySize 对象列表size + * @param attrsMap 待计算的列信息 key-向量名 value-实体类对象属性,多个按逗号分隔 + * @param vectorTotal 计算出的向量总量 + * @return 拆解结果 + */ + private List> splitVector(Integer entitySize,Map attrsMap,Float[] vectorTotal) { + List> result = Lists.newArrayList(); + + // 获取待切割的下标 + List indexes = this.getSplitIndex(vectorTotal.length, entitySize); + indexes.forEach(index -> { + + }); + return null; + } + + /** + * 获取将list等量分隔成若干份的列表下标 + * @param size 总数 + * @param splitNum 分隔数量 + * @return 下标集合 + */ + private List getSplitIndex(int size,int splitNum) { + if(size % splitNum != 0) { + throw new HzServiceException("向量计算失败,无法根据同步对象进行等量分隔!"); + } + return IntStream.iterate(0, index -> index + 1) + .limit(splitNum) + .mapToObj(index -> index * (size / splitNum)) + .collect(Collectors.toList()); + } + + + private String getFieldValue(String fields,Object object) { + Class clazz = object.getClass(); + return Func.toStrList(",", fields).stream().map(field -> { + try { + Field declaredField = clazz.getDeclaredField(field); + declaredField.setAccessible(true); + return declaredField.get(object).toString(); + } catch (NoSuchFieldException | IllegalAccessException e) { + return null; + } + }).collect(Collectors.joining(" ")); + } + + /** + * 计算向量值 + * @param vectors 待计算的向量 + * @return 向量值Float[] + */ + private Float[] compute(List vectors) { + // 向量计算 + String url = vectorUrl + invokeApi.getCompute(); + String jsonData = JSONUtil.toJsonStr(vectors); + HttpResponse response = HttpRequest.post(url) + .header("Content-Type", "application/json; charset=utf-8") + .body(jsonData) + .execute(); + byte[] bytes = response.bodyBytes(); + if (bytes.length % 4 != 0) { + throw new HzServiceException("向量计算失败!响应数据长度不是4的倍数"); + } + List chunks = new ArrayList<>(); + int range = bytes.length / 4; + IntStream.range(0, range) + .forEach(index -> { + byte[] chunk = new byte[4]; + int page = index * 4; + chunk[0] = bytes[page]; + chunk[1] = bytes[page + 1]; + chunk[2] = bytes[page + 2]; + chunk[3] = bytes[page + 3]; + chunks.add(chunk); + }); + List floats = chunks.stream().map(b -> { + ByteBuffer buffer = ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN); + return buffer.getFloat(); + }).collect(Collectors.toList()); + return floats.toArray(new Float[floats.size()]); + } +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/util/WeaviateUtil.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/util/WeaviateUtil.java new file mode 100644 index 0000000..1c056ce --- /dev/null +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/util/WeaviateUtil.java @@ -0,0 +1,11 @@ +package com.hnac.hzims.bigmodel.database.util; + +/** + * @Author: huangxing + * @Date: 2024/08/23 10:09 + */ +public class WeaviateUtil { + + + +} diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java index b28a3a5..1c5a8b2 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java @@ -1,22 +1,11 @@ package com.hnac.hzims.bigmodel.zhipuai.config; -import com.alibaba.fastjson.JSON; import com.zhipu.oapi.ClientV4; -import io.weaviate.client.Config; -import io.weaviate.client.WeaviateAuthClient; -import io.weaviate.client.WeaviateClient; -import io.weaviate.client.base.Result; -import io.weaviate.client.v1.auth.exception.AuthException; -import io.weaviate.client.v1.data.model.WeaviateObject; -import io.weaviate.client.v1.misc.model.Meta; import lombok.AllArgsConstructor; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import java.util.HashMap; -import java.util.List; - /** * @Author: ypj * @Date: 2024/8/9 17:01 @@ -32,28 +21,4 @@ public class ZhipuAiConfig { public ClientV4 clientV4() { return new ClientV4.Builder(properties.getApiSecret()).build(); } - -// public static void main(String[] args) throws AuthException { -// Config config = new Config("http", "192.168.60.16:9992"); -// WeaviateClient client = WeaviateAuthClient.apiKey(config,"123"); -// // 新增 HZIMS_TEST -//// Result craeteTableTest = client.data().creator().withClassName("HZIMS_TEST").run(); -//// System.out.println(JSON.toJSONString(craeteTableTest)); -//// Result> searchResult = client.data().objectsGetter().withClassName("HZIMS_TEST").run(); -//// System.out.println(JSON.toJSONString(searchResult)); -// // 新增数据 -//// HashMap hashMap = new HashMap() {{ -//// put("id_", "13412423423412"); -//// put("name", "test"); -//// }}; -//// Result hzimsTest = client.data().creator().withClassName("HZIMS_TEST").withProperties(hashMap).withVector(new Float[]{}).run(); -//// System.out.println(JSON.toJSONString(hzimsTest)); -//// Result> searchResult = client.data().objectsGetter().withClassName("HZIMS_TEST").run(); -//// System.out.println(JSON.toJSONString(searchResult)); -// // 删除数据 -// Result hzimsTest = client.data().deleter().withClassName("HZIMS_TEST").withID("14c6847d-b758-475f-968d-64b0e193e43e").run(); -// System.out.println(JSON.toJSONString(hzimsTest)); -// Result> searchResult = client.data().objectsGetter().withClassName("HZIMS_TEST").run(); -// System.out.println(JSON.toJSONString(searchResult)); -// } } diff --git a/hzims-service/hzims-big-model/src/main/resources/template/template.yml b/hzims-service/hzims-big-model/src/main/resources/template/template.yml index 77287f6..ab25e27 100644 --- a/hzims-service/hzims-big-model/src/main/resources/template/template.yml +++ b/hzims-service/hzims-big-model/src/main/resources/template/template.yml @@ -49,6 +49,7 @@ spring: gglm: url: "http://${gglm.host}:${gglm.port}" + vectorUrl: "http://${vector.host}:${vector.port}" api: assistantAsk: "/qa/assistant_ask" assistantStatus: "/qa/assistant_status" @@ -62,6 +63,7 @@ gglm: smartReportGeneratePower: "/custom/smart_report_generate_power" assistantAnalyseAsk: "/qa/assistant_analyse_ask" updateKnowledge: "/kn/update_knowledge" + compute: "compute" swagger: base-packages: com.hnac.hzims.bigmodel From 7092f66b538f38b789f74d272ff355527e0153df Mon Sep 17 00:00:00 2001 From: haungxing <1203316822@qq.com> Date: Wed, 28 Aug 2024 08:33:34 +0800 Subject: [PATCH 4/9] =?UTF-8?q?fix:=20=E4=B8=B4=E6=97=B6=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/service/DataSourceService.java | 5 +- .../bigmodel/database/service/WeaviateService.java | 87 +++++++++++++++++++--- .../controller/FontEndInteractiveController.java | 2 +- .../interactive/service/IHznlmInvokeService.java | 2 + .../service/impl/HznlmInvokeServiceImpl.java | 28 +++++++ .../inspect/obj/mapper/ObjectTemplateMapper.xml | 2 +- 6 files changed, 114 insertions(+), 12 deletions(-) diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java index c616d0a..ac1c3c8 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/business/service/DataSourceService.java @@ -58,7 +58,10 @@ public class DataSourceService { throw new HzServiceException(ResultCode.FAILURE,"查询语句中存在未进行鉴权的表,查询失败!"); }); if("1".equals(propertise.get(0).getAuthType())) { - String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where" + userAuthDataSQL +") temp"; + String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where is_deleted = 0 and " + userAuthDataSQL +") temp"; + sql = sql.replace(tableAuthVO.getTableName(),tableSubStr); + } else { + String tableSubStr = "(SELECT * FROM " + tableAuthVO.getTableName() + " where is_deleted = 0) temp"; sql = sql.replace(tableAuthVO.getTableName(),tableSubStr); } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java index 931a6a8..cbc926b 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/database/service/WeaviateService.java @@ -6,28 +6,26 @@ import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; -import com.google.protobuf.ServiceException; import com.hnac.hzims.bigmodel.configuration.BigModelInvokeApi; import com.hnac.hzinfo.exception.HzServiceException; import io.weaviate.client.base.Result; import io.weaviate.client.v1.data.api.ObjectCreator; import io.weaviate.client.v1.data.api.ObjectDeleter; import io.weaviate.client.v1.data.api.ObjectUpdater; +import io.weaviate.client.v1.data.api.ObjectsGetter; import io.weaviate.client.v1.data.model.WeaviateObject; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.utils.BeanUtil; import org.springblade.core.tool.utils.Func; -import org.springblade.core.tool.utils.StringUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import org.springframework.util.Assert; import java.lang.reflect.Field; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.*; -import java.util.stream.Collector; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -43,9 +41,11 @@ public class WeaviateService { private final ObjectCreator objectCreator; private final ObjectUpdater objectUpdater; private final ObjectDeleter objectDeleter; + private final ObjectsGetter objectsGetter; private final BigModelInvokeApi invokeApi; + @Value("${gglm.vectorUrl}") - private final String vectorUrl; + private String vectorUrl; /** * 对象保存向量数据库 @@ -89,7 +89,10 @@ public class WeaviateService { if(Func.isNotEmpty(vectorStrs)) { // 若解析出来的向量存在值 Float[] vectors = this.compute(vectorStrs); - + List> vector = this.splitVector(entities.size(), attrsMap, vectors); + for(int i = 0; i < entities.size(); i++) { + creator.withProperties(BeanUtil.toMap(entities.get(i))).withVectors(vector.get(i)).run(); + } } else { entities.forEach(entity -> creator.withProperties(BeanUtil.toMap(entity)).run()); return true; @@ -98,6 +101,63 @@ public class WeaviateService { } /** + * 删除向量数据库(表名) + * @param className 表名 + * @return 删除结果 + */ + public Boolean deleteByClassName(String className) { + Result result = objectDeleter.withClassName(className).run(); + return !result.hasErrors(); + } + + /** + * 删除向量数据库(ID) + * @param id 向量数据库ID + * @return 删除结果 + */ + public Boolean deleteById(String id) { + Result result = objectDeleter.withID(id).run(); + return !result.hasErrors(); + } + + /** + * 更新数据库(通过ID) + * @param id 向量数据库ID + * @return 更新结果 + */ + public Boolean updateById(String id, Object entity, String className, Map attrMap) { + ObjectUpdater updater = objectUpdater.withClassName(className).withID(id).withProperties(BeanUtil.toMap(entity)); + // 计算向量 + Map vector = new HashMap<>(); + if(Func.isNotEmpty(attrMap)) { + attrMap.forEach((k,v) -> { + String fieldValue = this.getFieldValue(v, entity); + Float[] compute = this.compute(Lists.newArrayList(fieldValue)); + vector.put(k,compute); + }); + } + if(Func.isNotEmpty(vector)) { + updater.withVectors(vector); + } + Result result = updater.run(); + return !result.hasErrors(); + } + + public List list(String id,String className) { + if(Func.isNotEmpty(id)) { + objectsGetter.withID(id); + } + if(Func.isNotEmpty(className)) { + objectsGetter.withClassName(className); + } + Result> result = objectsGetter.run(); + if(result.hasErrors()) { + throw new HzServiceException("查询失败!"); + } + return result.getResult().stream().map(WeaviateObject::getProperties).collect(Collectors.toList()); + } + + /** * 拆解计算出来的向量Float[] * @param entitySize 对象列表size * @param attrsMap 待计算的列信息 key-向量名 value-实体类对象属性,多个按逗号分隔 @@ -106,13 +166,22 @@ public class WeaviateService { */ private List> splitVector(Integer entitySize,Map attrsMap,Float[] vectorTotal) { List> result = Lists.newArrayList(); - + List vectorTotalList = Lists.newArrayList(vectorTotal); // 获取待切割的下标 List indexes = this.getSplitIndex(vectorTotal.length, entitySize); + int step = vectorTotal.length / entitySize; indexes.forEach(index -> { - + List vectors = vectorTotalList.subList(index, index + step); + Map vectorMap = new HashMap<>(); + List splitIndex = this.getSplitIndex(vectors.size(), attrsMap.size()); + AtomicInteger i = new AtomicInteger(); + attrsMap.forEach((k,v) -> { + List vector = vectors.subList(splitIndex.get(i.get()), splitIndex.get(i.get() + (vectors.size() / attrsMap.size()))); + vectorMap.put(k, vector.toArray(new Float[vector.size()])); + i.getAndIncrement(); + }); }); - return null; + return result; } /** diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java index 21b05f4..b7cab25 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/FontEndInteractiveController.java @@ -64,7 +64,7 @@ public class FontEndInteractiveController { @ApiOperationSupport(order = 5) @GetMapping("/interruptSession") public R interruptSession(@RequestParam(value = "id") String sessionId) { - hznlmInvokeService.askAbort(sessionId); + hznlmInvokeService.interruptSession(sessionId); return R.success("操作成功!"); } } diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java index 51ed3f3..2ef2107 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IHznlmInvokeService.java @@ -81,6 +81,8 @@ public interface IHznlmInvokeService { */ void askAbort(String sessionId); + void interruptSession(String sessionId); + /** * 发起机组发电量智能报表分析问答 * @param req 待分析的数据 diff --git a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java index b8bdb15..48e9881 100644 --- a/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java +++ b/hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/HznlmInvokeServiceImpl.java @@ -17,6 +17,7 @@ import com.hnac.hzims.bigmodel.utils.RequestClientUtil; import com.hnac.hzims.bigmodel.websocket.constants.RedisKeyConstants; import com.hnac.hzims.bigmodel.websocket.sessionManager.InteractiveSessionManager; import com.hnac.hzims.common.constant.CommonConstant; +import com.hnac.hzinfo.exception.HzServiceException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.tool.api.R; @@ -126,6 +127,33 @@ public class HznlmInvokeServiceImpl implements IHznlmInvokeService { sessionRedisManager.removeSessionId(sessionId); } + public void interruptSession(String sessionId) { + this.askAbort(sessionId); + // 循环获取该会话ID中断状态,当状态等于-2或重连超6次则中断返回结果 + int status = 999; + int attempts = 0; + while (status > 0) { + // 若重连超过10次 则抛出错误 + if(attempts >= 8) { + throw new HzServiceException("中断失败!长时间未获取到中断状态"); + } + List answers = this.getAnswerBySessionIds(sessionId); + if(Func.isNotEmpty(answers)) { + log.info(answers.get(0).getStatus()+""); + status = answers.get(0).getStatus(); + } + // 若获取到的状态不等于2 则延时0.5秒 + if(status > 0) { + try { + Thread.sleep(500); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + attempts ++; + } + } + @Override public void smartReportGeneratePower(RunReportAnalyseRequest req) { Map params = new HashMap<>(); diff --git a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml index 6136502..8220245 100644 --- a/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml +++ b/hzims-service/inspect/src/main/java/com/hnac/hzinfo/inspect/obj/mapper/ObjectTemplateMapper.xml @@ -3,7 +3,7 @@