diff --git a/README.md b/README.md index 58e3553..e078ae5 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ vue3+typescript+pnpm * 创建数据库aideepin * 执行docs/create.sql -* 填充各模型的配置(至少设置一个) +* 填充各AI平台的配置(至少设置一个) openai的secretKey diff --git a/adi-common/src/main/java/com/moyz/adi/common/cosntant/RedisKeyConstant.java b/adi-common/src/main/java/com/moyz/adi/common/cosntant/RedisKeyConstant.java index 6f0cbaa..ca04bb5 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/cosntant/RedisKeyConstant.java +++ b/adi-common/src/main/java/com/moyz/adi/common/cosntant/RedisKeyConstant.java @@ -91,4 +91,10 @@ public class RedisKeyConstant { * 值: 用户id */ public static final String qa_item_create_limit = "aq:item:create:{0}"; + + /** + * 重新统计知识库信号 + * 值:知识库uuid + */ + public static final String KB_STATISTIC_RECALCULATE_SIGNAL = "kb:statistic:recalculate:signal"; } diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/KbInfoResp.java b/adi-common/src/main/java/com/moyz/adi/common/dto/KbInfoResp.java index ed3774e..1c4804e 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/dto/KbInfoResp.java +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/KbInfoResp.java @@ -11,4 +11,6 @@ public class KbInfoResp { private Integer starCount; private String ownerUuid; private String ownerName; + private Integer itemCount; + private Integer embeddingCount; } diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/KbItemDto.java b/adi-common/src/main/java/com/moyz/adi/common/dto/KbItemDto.java index ef13d64..837ee6a 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/dto/KbItemDto.java +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/KbItemDto.java @@ -1,6 +1,5 @@ package com.moyz.adi.common.dto; -import com.baomidou.mybatisplus.annotation.TableField; import lombok.Data; import java.time.LocalDateTime; diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/KbStatDto.java b/adi-common/src/main/java/com/moyz/adi/common/dto/KbStatDto.java new file mode 100644 index 0000000..5994227 --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/KbStatDto.java @@ -0,0 +1,9 @@ +package com.moyz.adi.common.dto; + +import lombok.Data; + +@Data +public class KbStatDto { + int itemCount; + int embeddingCount; +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/entity/KnowledgeBase.java b/adi-common/src/main/java/com/moyz/adi/common/entity/KnowledgeBase.java index 5d85942..9bce577 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/entity/KnowledgeBase.java +++ b/adi-common/src/main/java/com/moyz/adi/common/entity/KnowledgeBase.java @@ -30,6 +30,14 @@ public class KnowledgeBase extends BaseEntity { @TableField("star_count") private Integer starCount; + @Schema(title = "知识点数量") + @TableField("item_count") + private Integer itemCount; + + @Schema(title = "向量数") + @TableField("embedding_count") + private Integer embeddingCount; + @Schema(title = "所属人uuid") @TableField("owner_uuid") private String ownerUuid; diff --git a/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseEmbeddingMapper.java b/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseEmbeddingMapper.java index dbd8da3..6095b56 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseEmbeddingMapper.java +++ b/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseEmbeddingMapper.java @@ -14,4 +14,6 @@ public interface KnowledgeBaseEmbeddingMapper extends BaseMapper selectByItemUuid(Page page, @Param("kbItemUuid") String uuid); boolean deleteByItemUuid(@Param("kbItemUuid") String uuid); + + Integer countByKbUuid(@Param("kbUuid") String kbUuid); } diff --git a/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseItemMapper.java b/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseItemMapper.java index 24d066b..6c860c6 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseItemMapper.java +++ b/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseItemMapper.java @@ -13,4 +13,6 @@ public interface KnowledgeBaseItemMapper extends BaseMapper { @InterceptorIgnore(tenantLine = "true") Page searchByKb(Page page, @Param("kbUuid") String kbUuid, @Param("keyword") String keyword); + + KnowledgeBaseItem getByUuid(String uuid); } diff --git a/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseMapper.java b/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseMapper.java index d01b77f..fe9fe6a 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseMapper.java +++ b/adi-common/src/main/java/com/moyz/adi/common/mapper/KnowledgeBaseMapper.java @@ -25,4 +25,11 @@ public interface KnowledgeBaseMapper extends BaseMapper { * @return */ Page searchByUser(Page page, @Param("ownerId") long ownerId, @Param("keyword") String keyword, @Param("includeOthersPublic") Boolean includeOthersPublic); + + /** + * 更新统计数据 + * + * @param uuid + */ + void updateStatByUuid(@Param("uuid") String uuid); } diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/KnowledgeBaseEmbeddingService.java b/adi-common/src/main/java/com/moyz/adi/common/service/KnowledgeBaseEmbeddingService.java index ec11992..edc5129 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/KnowledgeBaseEmbeddingService.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/KnowledgeBaseEmbeddingService.java @@ -32,4 +32,8 @@ public class KnowledgeBaseEmbeddingService extends ServiceImpl new BaseException(A_DATA_NOT_FOUND)); } + /** + * Set update knowledge base stat signal + * + * @param kbUuid + */ + public void updateStatistic(String kbUuid) { + stringRedisTemplate.opsForSet().add(KB_STATISTIC_RECALCULATE_SIGNAL, kbUuid); + } + + /** + * Update knowledge base stat + */ + @Scheduled(fixedDelay = 60 * 1000) + public void asyncUpdateStatistic() { + Set kbUuidList = stringRedisTemplate.opsForSet().members(KB_STATISTIC_RECALCULATE_SIGNAL); + for (String kbUuid : kbUuidList) { + baseMapper.updateStatByUuid(kbUuid); + stringRedisTemplate.opsForSet().remove(KB_STATISTIC_RECALCULATE_SIGNAL, kbUuid); + } + } + private void checkPrivilege(Long kbId, String kbUuid) { if (null == kbId && StringUtils.isBlank(kbUuid)) { throw new BaseException(A_PARAMS_ERROR); diff --git a/adi-common/src/main/resources/mapper/KnowledgeBaseEmbeddingMapper.xml b/adi-common/src/main/resources/mapper/KnowledgeBaseEmbeddingMapper.xml new file mode 100644 index 0000000..991a585 --- /dev/null +++ b/adi-common/src/main/resources/mapper/KnowledgeBaseEmbeddingMapper.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/adi-common/src/main/resources/mapper/KnowledgeBaseItemMapper.xml b/adi-common/src/main/resources/mapper/KnowledgeBaseItemMapper.xml index 682215b..c8a11fb 100644 --- a/adi-common/src/main/resources/mapper/KnowledgeBaseItemMapper.xml +++ b/adi-common/src/main/resources/mapper/KnowledgeBaseItemMapper.xml @@ -14,4 +14,9 @@ order by create_time desc + diff --git a/adi-common/src/main/resources/mapper/KnowledgeBaseMapper.xml b/adi-common/src/main/resources/mapper/KnowledgeBaseMapper.xml index 90448f8..14487e5 100644 --- a/adi-common/src/main/resources/mapper/KnowledgeBaseMapper.xml +++ b/adi-common/src/main/resources/mapper/KnowledgeBaseMapper.xml @@ -29,4 +29,16 @@ order by star_count,update_time desc + + update adi_knowledge_base + set item_count = (select count(1) + from adi_knowledge_base_item + where kb_uuid = #{uuid} + and is_deleted = false), + embedding_count = (select count(1) + from adi_knowledge_base_embedding + where metadata ->> 'kb_uuid' = #{uuid}) + where uuid = #{uuid} + + diff --git a/docs/create.sql b/docs/create.sql index 3fe2d62..dbd3d7d 100644 --- a/docs/create.sql +++ b/docs/create.sql @@ -436,18 +436,20 @@ VALUES ('quota_by_qa_item_monthly', '100'); create table adi_knowledge_base ( - id bigserial primary key, - uuid varchar(32) default ''::character varying not null, - title varchar(250) default ''::character varying not null, - remark text default ''::character varying not null, - is_public boolean default false not null, - star_count int default 0 not null, - owner_id bigint default 0 not null, - owner_uuid varchar(32) default ''::character varying not null, - owner_name varchar(45) default ''::character varying not null, - create_time timestamp default CURRENT_TIMESTAMP not null, - update_time timestamp default CURRENT_TIMESTAMP not null, - is_deleted boolean default false not null + id bigserial primary key, + uuid varchar(32) default ''::character varying not null, + title varchar(250) default ''::character varying not null, + remark text default ''::character varying not null, + is_public boolean default false not null, + owner_id bigint default 0 not null, + owner_uuid varchar(32) default ''::character varying not null, + owner_name varchar(45) default ''::character varying not null, + star_count int default 0 not null, + item_count int default 0 not null, + embedding_count int default 0 not null, + create_time timestamp default CURRENT_TIMESTAMP not null, + update_time timestamp default CURRENT_TIMESTAMP not null, + is_deleted boolean default false not null ); comment on table adi_knowledge_base is '知识库'; @@ -460,6 +462,10 @@ comment on column adi_knowledge_base.is_public is '是否公开'; comment on column adi_knowledge_base.star_count is '点赞数'; +comment on column adi_knowledge_base.item_count is '知识点数量'; + +comment on column adi_knowledge_base.embedding_count is '向量数'; + comment on column adi_knowledge_base.owner_id is '所属人id'; comment on column adi_knowledge_base.owner_uuid is '所属人uuid';