mirror of
https://gitcode.com/ageerle/ruoyi-ai.git
synced 2026-04-16 13:23:42 +00:00
Compare commits
15 Commits
v2.0.4
...
57b2f833f8
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
57b2f833f8 | ||
|
|
7907cbeb7f | ||
|
|
9cfcdd2b9b | ||
|
|
980df20752 | ||
|
|
aa92d232bb | ||
|
|
81c0bb5738 | ||
|
|
1a645c6e10 | ||
|
|
731f6ceb6e | ||
|
|
8e21245348 | ||
|
|
936e157e4a | ||
|
|
256f72c487 | ||
|
|
009aa5f1a5 | ||
|
|
d22d2cb708 | ||
|
|
59104028b6 | ||
|
|
c2f6a8321a |
2
pom.xml
2
pom.xml
@@ -20,7 +20,7 @@
|
||||
<java.version>17</java.version>
|
||||
<mysql.version>8.0.33</mysql.version>
|
||||
<mybatis.version>3.5.16</mybatis.version>
|
||||
<springdoc.version>2.1.0</springdoc.version>
|
||||
<springdoc.version>2.8.5</springdoc.version>
|
||||
<therapi-javadoc.version>0.15.0</therapi-javadoc.version>
|
||||
<poi.version>5.2.3</poi.version>
|
||||
<easyexcel.version>3.2.1</easyexcel.version>
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.ruoyi.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
@@ -215,11 +215,16 @@ mybatis-encryptor:
|
||||
publicKey:
|
||||
privateKey:
|
||||
|
||||
# Swagger配置
|
||||
swagger:
|
||||
springdoc:
|
||||
api-docs:
|
||||
# 是否开启接口文档
|
||||
enabled: true
|
||||
# swagger-ui:
|
||||
# # 持久化认证数据
|
||||
# persistAuthorization: true
|
||||
info:
|
||||
# 标题
|
||||
title: '标题:${ruoyi.name}多租户管理系统_接口文档'
|
||||
title: '标题:RuoYi-Vue-Plus多租户管理系统_接口文档'
|
||||
# 描述
|
||||
description: '描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...'
|
||||
# 版本
|
||||
@@ -236,24 +241,10 @@ swagger:
|
||||
type: APIKEY
|
||||
in: HEADER
|
||||
name: ${sa-token.token-name}
|
||||
|
||||
springdoc:
|
||||
api-docs:
|
||||
# 是否开启接口文档
|
||||
enabled: true
|
||||
swagger-ui:
|
||||
# 持久化认证数据
|
||||
persistAuthorization: true
|
||||
#这里定义了两个分组,可定义多个,也可以不定义
|
||||
group-configs:
|
||||
- group: 1.演示模块
|
||||
packages-to-scan: org.ruoyi.demo
|
||||
- group: 2.通用模块
|
||||
packages-to-scan: org.ruoyi.web
|
||||
- group: 3.系统模块
|
||||
- group: 1.系统模块
|
||||
packages-to-scan: org.ruoyi.system
|
||||
- group: 4.代码生成模块
|
||||
packages-to-scan: org.ruoyi.generator
|
||||
|
||||
# 防止XSS攻击
|
||||
xss:
|
||||
|
||||
BIN
ruoyi-admin/src/main/resources/static/favicon.ico
Normal file
BIN
ruoyi-admin/src/main/resources/static/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 78 KiB |
@@ -159,12 +159,20 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 微信模块 -->
|
||||
<dependency>
|
||||
<groupId>org.ruoyi</groupId>
|
||||
<artifactId>ruoyi-chat</artifactId>
|
||||
<artifactId>ruoyi-common-wechat</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- 支付模块 -->
|
||||
<dependency>
|
||||
<groupId>org.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common-pay</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -56,6 +56,12 @@ public class ChatRequest {
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
|
||||
/**
|
||||
* 会话id
|
||||
*/
|
||||
private Long sessionId;
|
||||
|
||||
/**
|
||||
* 应用ID
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,7 @@ import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.doc.config.properties.SwaggerProperties;
|
||||
import org.ruoyi.common.doc.config.properties.SpringDocProperties;
|
||||
import org.ruoyi.common.doc.handler.OpenApiHandler;
|
||||
import org.springdoc.core.configuration.SpringDocConfiguration;
|
||||
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
|
||||
@@ -36,26 +36,26 @@ import java.util.Set;
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@AutoConfiguration(before = SpringDocConfiguration.class)
|
||||
@EnableConfigurationProperties(SwaggerProperties.class)
|
||||
@EnableConfigurationProperties(SpringDocProperties.class)
|
||||
@ConditionalOnProperty(name = "springdoc.api-docs.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SwaggerConfig {
|
||||
public class SpringDocConfig {
|
||||
|
||||
private final ServerProperties serverProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(OpenAPI.class)
|
||||
public OpenAPI openApi(SwaggerProperties swaggerProperties) {
|
||||
public OpenAPI openApi(SpringDocProperties properties) {
|
||||
OpenAPI openApi = new OpenAPI();
|
||||
// 文档基本信息
|
||||
SwaggerProperties.InfoProperties infoProperties = swaggerProperties.getInfo();
|
||||
SpringDocProperties.InfoProperties infoProperties = properties.getInfo();
|
||||
Info info = convertInfo(infoProperties);
|
||||
openApi.info(info);
|
||||
// 扩展文档信息
|
||||
openApi.externalDocs(swaggerProperties.getExternalDocs());
|
||||
openApi.tags(swaggerProperties.getTags());
|
||||
openApi.paths(swaggerProperties.getPaths());
|
||||
openApi.components(swaggerProperties.getComponents());
|
||||
Set<String> keySet = swaggerProperties.getComponents().getSecuritySchemes().keySet();
|
||||
openApi.externalDocs(properties.getExternalDocs());
|
||||
openApi.tags(properties.getTags());
|
||||
openApi.paths(properties.getPaths());
|
||||
openApi.components(properties.getComponents());
|
||||
Set<String> keySet = properties.getComponents().getSecuritySchemes().keySet();
|
||||
List<SecurityRequirement> list = new ArrayList<>();
|
||||
SecurityRequirement securityRequirement = new SecurityRequirement();
|
||||
keySet.forEach(securityRequirement::addList);
|
||||
@@ -65,7 +65,7 @@ public class SwaggerConfig {
|
||||
return openApi;
|
||||
}
|
||||
|
||||
private Info convertInfo(SwaggerProperties.InfoProperties infoProperties) {
|
||||
private Info convertInfo(SpringDocProperties.InfoProperties infoProperties) {
|
||||
Info info = new Info();
|
||||
info.setTitle(infoProperties.getTitle());
|
||||
info.setDescription(infoProperties.getDescription());
|
||||
@@ -18,8 +18,8 @@ import java.util.List;
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "swagger")
|
||||
public class SwaggerProperties {
|
||||
@ConfigurationProperties(prefix = "springdoc")
|
||||
public class SpringDocProperties {
|
||||
|
||||
/**
|
||||
* 文档基本信息
|
||||
@@ -11,6 +11,7 @@ import io.swagger.v3.oas.models.Paths;
|
||||
import io.swagger.v3.oas.models.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.ruoyi.common.core.utils.StreamUtils;
|
||||
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
|
||||
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
|
||||
import org.springdoc.core.properties.SpringDocConfigProperties;
|
||||
@@ -154,11 +155,11 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
|
||||
if (!CollectionUtils.isEmpty(tagsStr))
|
||||
tagsStr = tagsStr.stream()
|
||||
.map(str -> propertyResolverUtils.resolve(str, locale))
|
||||
.collect(Collectors.toSet());
|
||||
.map(str -> propertyResolverUtils.resolve(str, locale))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
if (springdocTags.containsKey(handlerMethod)) {
|
||||
Tag tag = springdocTags.get(handlerMethod);
|
||||
io.swagger.v3.oas.models.tags.Tag tag = springdocTags.get(handlerMethod);
|
||||
tagsStr.add(tag.getName());
|
||||
if (openAPI.getTags() == null || !openAPI.getTags().contains(tag)) {
|
||||
openAPI.addTagsItem(tag);
|
||||
@@ -182,7 +183,7 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
if (javadocProvider.isPresent()) {
|
||||
String description = javadocProvider.get().getClassJavadoc(handlerMethod.getBeanType());
|
||||
if (StringUtils.isNotBlank(description)) {
|
||||
Tag tag = new Tag();
|
||||
io.swagger.v3.oas.models.tags.Tag tag = new io.swagger.v3.oas.models.tags.Tag();
|
||||
|
||||
// 自定义部分 修改使用java注释当tag名
|
||||
List<String> list = IoUtil.readLines(new StringReader(description), new ArrayList<>());
|
||||
@@ -203,7 +204,7 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
|
||||
if (!CollectionUtils.isEmpty(tags)) {
|
||||
// Existing tags
|
||||
List<Tag> openApiTags = openAPI.getTags();
|
||||
List<io.swagger.v3.oas.models.tags.Tag> openApiTags = openAPI.getTags();
|
||||
if (!CollectionUtils.isEmpty(openApiTags))
|
||||
tags.addAll(openApiTags);
|
||||
openAPI.setTags(new ArrayList<>(tags));
|
||||
@@ -211,7 +212,7 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
|
||||
// Handle SecurityRequirement at operation level
|
||||
io.swagger.v3.oas.annotations.security.SecurityRequirement[] securityRequirements = securityParser
|
||||
.getSecurityRequirements(handlerMethod);
|
||||
.getSecurityRequirements(handlerMethod);
|
||||
if (securityRequirements != null) {
|
||||
if (securityRequirements.length == 0)
|
||||
operation.setSecurity(Collections.emptyList());
|
||||
@@ -222,23 +223,23 @@ public class OpenApiHandler extends OpenAPIService {
|
||||
return operation;
|
||||
}
|
||||
|
||||
private void buildTagsFromMethod(Method method, Set<Tag> tags, Set<String> tagsStr, Locale locale) {
|
||||
private void buildTagsFromMethod(Method method, Set<io.swagger.v3.oas.models.tags.Tag> tags, Set<String> tagsStr, Locale locale) {
|
||||
// method tags
|
||||
Set<Tags> tagsSet = AnnotatedElementUtils
|
||||
.findAllMergedAnnotations(method, Tags.class);
|
||||
.findAllMergedAnnotations(method, Tags.class);
|
||||
Set<io.swagger.v3.oas.annotations.tags.Tag> methodTags = tagsSet.stream()
|
||||
.flatMap(x -> Stream.of(x.value())).collect(Collectors.toSet());
|
||||
.flatMap(x -> Stream.of(x.value())).collect(Collectors.toSet());
|
||||
methodTags.addAll(AnnotatedElementUtils.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.tags.Tag.class));
|
||||
if (!CollectionUtils.isEmpty(methodTags)) {
|
||||
tagsStr.addAll(methodTags.stream().map(tag -> propertyResolverUtils.resolve(tag.name(), locale)).collect(Collectors.toSet()));
|
||||
tagsStr.addAll(StreamUtils.toSet(methodTags, tag -> propertyResolverUtils.resolve(tag.name(), locale)));
|
||||
List<io.swagger.v3.oas.annotations.tags.Tag> allTags = new ArrayList<>(methodTags);
|
||||
addTags(allTags, tags, locale);
|
||||
}
|
||||
}
|
||||
|
||||
private void addTags(List<io.swagger.v3.oas.annotations.tags.Tag> sourceTags, Set<Tag> tags, Locale locale) {
|
||||
Optional<Set<Tag>> optionalTagSet = AnnotationsUtils
|
||||
.getTags(sourceTags.toArray(new io.swagger.v3.oas.annotations.tags.Tag[0]), true);
|
||||
private void addTags(List<io.swagger.v3.oas.annotations.tags.Tag> sourceTags, Set<io.swagger.v3.oas.models.tags.Tag> tags, Locale locale) {
|
||||
Optional<Set<io.swagger.v3.oas.models.tags.Tag>> optionalTagSet = AnnotationsUtils
|
||||
.getTags(sourceTags.toArray(new io.swagger.v3.oas.annotations.tags.Tag[0]), true);
|
||||
optionalTagSet.ifPresent(tagsSet -> {
|
||||
tagsSet.forEach(tag -> {
|
||||
tag.name(propertyResolverUtils.resolve(tag.getName(), locale));
|
||||
|
||||
@@ -1 +1 @@
|
||||
org.ruoyi.common.doc.config.SwaggerConfig
|
||||
org.ruoyi.common.doc.config.SpringDocConfig
|
||||
|
||||
@@ -34,6 +34,11 @@ public class ChatMessage extends BaseEntity {
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 会话id
|
||||
*/
|
||||
private Long sessionId;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.ruoyi.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.ruoyi.core.domain.BaseEntity;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
/**
|
||||
* 会话管理对象 chat_session
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@TableName("chat_session")
|
||||
public class ChatSession extends BaseEntity {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId(value = "id")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
private String sessionTitle;
|
||||
|
||||
/**
|
||||
* 会话内容
|
||||
*/
|
||||
private String sessionContent;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
|
||||
}
|
||||
@@ -40,6 +40,11 @@ public class ChatMessageBo extends BaseEntity {
|
||||
@NotBlank(message = "消息内容不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 会话id
|
||||
*/
|
||||
private Long sessionId;
|
||||
|
||||
/**
|
||||
* 对话角色
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.ruoyi.domain.bo;
|
||||
|
||||
import org.ruoyi.common.core.validate.AddGroup;
|
||||
import org.ruoyi.common.core.validate.EditGroup;
|
||||
import org.ruoyi.core.domain.BaseEntity;
|
||||
import io.github.linpeilie.annotations.AutoMapper;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import jakarta.validation.constraints.*;
|
||||
import org.ruoyi.domain.ChatSession;
|
||||
|
||||
/**
|
||||
* 会话管理业务对象 chat_session
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@AutoMapper(target = ChatSession.class, reverseConvertGenerate = false)
|
||||
public class ChatSessionBo extends BaseEntity {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@NotNull(message = "主键不能为空", groups = { EditGroup.class })
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@NotNull(message = "用户id不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
@NotBlank(message = "会话标题不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||
private String sessionTitle;
|
||||
|
||||
/**
|
||||
* 会话内容
|
||||
*/
|
||||
@NotBlank(message = "会话内容不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||
private String sessionContent;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
@NotBlank(message = "备注不能为空", groups = { AddGroup.class, EditGroup.class })
|
||||
private String remark;
|
||||
|
||||
|
||||
}
|
||||
@@ -41,6 +41,11 @@ public class ChatMessageVo implements Serializable {
|
||||
@ExcelProperty(value = "用户id")
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 会话id
|
||||
*/
|
||||
private Long sessionId;
|
||||
|
||||
/**
|
||||
* 消息内容
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.ruoyi.domain.vo;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import io.github.linpeilie.annotations.AutoMapper;
|
||||
import lombok.Data;
|
||||
import org.ruoyi.domain.ChatSession;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 会话管理视图对象 chat_session
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Data
|
||||
@ExcelIgnoreUnannotated
|
||||
@AutoMapper(target = ChatSession.class)
|
||||
public class ChatSessionVo implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@ExcelProperty(value = "主键")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@ExcelProperty(value = "用户id")
|
||||
private Long userId;
|
||||
|
||||
/**
|
||||
* 会话标题
|
||||
*/
|
||||
@ExcelProperty(value = "会话标题")
|
||||
private String sessionTitle;
|
||||
|
||||
/**
|
||||
* 会话内容
|
||||
*/
|
||||
@ExcelProperty(value = "会话内容")
|
||||
private String sessionContent;
|
||||
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
@ExcelProperty(value = "备注")
|
||||
private String remark;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.ruoyi.mapper;
|
||||
|
||||
import org.ruoyi.core.mapper.BaseMapperPlus;
|
||||
import org.ruoyi.domain.ChatSession;
|
||||
import org.ruoyi.domain.vo.ChatSessionVo;
|
||||
|
||||
/**
|
||||
* 会话管理Mapper接口
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
public interface ChatSessionMapper extends BaseMapperPlus<ChatSession, ChatSessionVo> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package org.ruoyi.service;
|
||||
|
||||
import org.ruoyi.core.page.PageQuery;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.domain.bo.ChatSessionBo;
|
||||
import org.ruoyi.domain.vo.ChatSessionVo;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 会话管理Service接口
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
public interface IChatSessionService {
|
||||
|
||||
/**
|
||||
* 查询会话管理
|
||||
*/
|
||||
ChatSessionVo queryById(Long id);
|
||||
|
||||
/**
|
||||
* 查询会话管理列表
|
||||
*/
|
||||
TableDataInfo<ChatSessionVo> queryPageList(ChatSessionBo bo, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 查询会话管理列表
|
||||
*/
|
||||
List<ChatSessionVo> queryList(ChatSessionBo bo);
|
||||
|
||||
/**
|
||||
* 新增会话管理
|
||||
*/
|
||||
Boolean insertByBo(ChatSessionBo bo);
|
||||
|
||||
/**
|
||||
* 修改会话管理
|
||||
*/
|
||||
Boolean updateByBo(ChatSessionBo bo);
|
||||
|
||||
/**
|
||||
* 校验并批量删除会话管理信息
|
||||
*/
|
||||
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package org.ruoyi.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.ruoyi.common.core.utils.MapstructUtils;
|
||||
import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.core.page.PageQuery;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.ruoyi.domain.ChatSession;
|
||||
import org.ruoyi.domain.bo.ChatSessionBo;
|
||||
import org.ruoyi.domain.vo.ChatSessionVo;
|
||||
import org.ruoyi.mapper.ChatSessionMapper;
|
||||
import org.ruoyi.service.IChatSessionService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 会话管理Service业务层处理
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class ChatSessionServiceImpl implements IChatSessionService {
|
||||
|
||||
private final ChatSessionMapper baseMapper;
|
||||
|
||||
/**
|
||||
* 查询会话管理
|
||||
*/
|
||||
@Override
|
||||
public ChatSessionVo queryById(Long id){
|
||||
return baseMapper.selectVoById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询会话管理列表
|
||||
*/
|
||||
@Override
|
||||
public TableDataInfo<ChatSessionVo> queryPageList(ChatSessionBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<ChatSession> lqw = buildQueryWrapper(bo);
|
||||
Page<ChatSessionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
return TableDataInfo.build(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询会话管理列表
|
||||
*/
|
||||
@Override
|
||||
public List<ChatSessionVo> queryList(ChatSessionBo bo) {
|
||||
LambdaQueryWrapper<ChatSession> lqw = buildQueryWrapper(bo);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<ChatSession> buildQueryWrapper(ChatSessionBo bo) {
|
||||
Map<String, Object> params = bo.getParams();
|
||||
LambdaQueryWrapper<ChatSession> lqw = Wrappers.lambdaQuery();
|
||||
lqw.eq(bo.getUserId() != null, ChatSession::getUserId, bo.getUserId());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getSessionTitle()), ChatSession::getSessionTitle, bo.getSessionTitle());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getSessionContent()), ChatSession::getSessionContent, bo.getSessionContent());
|
||||
return lqw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增会话管理
|
||||
*/
|
||||
@Override
|
||||
public Boolean insertByBo(ChatSessionBo bo) {
|
||||
ChatSession add = MapstructUtils.convert(bo, ChatSession.class);
|
||||
validEntityBeforeSave(add);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (flag) {
|
||||
bo.setId(add.getId());
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会话管理
|
||||
*/
|
||||
@Override
|
||||
public Boolean updateByBo(ChatSessionBo bo) {
|
||||
ChatSession update = MapstructUtils.convert(bo, ChatSession.class);
|
||||
validEntityBeforeSave(update);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存前的数据校验
|
||||
*/
|
||||
private void validEntityBeforeSave(ChatSession entity){
|
||||
//TODO 做一些数据校验,如唯一约束
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除会话管理
|
||||
*/
|
||||
@Override
|
||||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||||
if(isValid){
|
||||
//TODO 做一些业务上的校验,判断是否需要校验
|
||||
}
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,21 @@
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<langchain4j.version>1.0.0-beta4</langchain4j.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-bom</artifactId>
|
||||
<version>${langchain4j.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- pdf解析器 -->
|
||||
@@ -34,17 +47,60 @@
|
||||
<version>1.0.79</version>
|
||||
</dependency>
|
||||
|
||||
<!-- milvus java sdk -->
|
||||
<dependency>
|
||||
<groupId>io.milvus</groupId>
|
||||
<artifactId>milvus-sdk-java</artifactId>
|
||||
<version>2.3.2</version>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-weaviate</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.weaviate</groupId>
|
||||
<artifactId>client</artifactId>
|
||||
<version>4.0.0</version>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-embeddings-all-minilm-l6-v2</artifactId>
|
||||
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>weaviate</artifactId>
|
||||
<version>1.19.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-open-ai</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-ollama</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-milvus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>milvus</artifactId>
|
||||
<version>1.19.6</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-qdrant</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>qdrant</artifactId>
|
||||
<version>1.19.6</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.ruoyi.domain.bo;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 查询向量所需参数
|
||||
* @author ageer
|
||||
*/
|
||||
@Data
|
||||
public class QueryVectorBo {
|
||||
|
||||
/**
|
||||
* 查询内容
|
||||
*/
|
||||
private String query;
|
||||
|
||||
/**
|
||||
* 知识库kid
|
||||
*/
|
||||
private String kid;
|
||||
|
||||
/**
|
||||
* 查询向量返回条数
|
||||
*/
|
||||
private Integer maxResults;
|
||||
|
||||
/**
|
||||
* 模型名称
|
||||
*/
|
||||
private String modelName;
|
||||
|
||||
/**
|
||||
* 请求key
|
||||
*/
|
||||
private String apiKey;
|
||||
|
||||
/**
|
||||
* 请求地址
|
||||
*/
|
||||
private String baseUrl;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.ruoyi.domain.bo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 保存向量所需参数
|
||||
* @author ageer
|
||||
*/
|
||||
@Data
|
||||
public class StoreEmbeddingBo {
|
||||
|
||||
/**
|
||||
* 切分文本块列表
|
||||
*/
|
||||
private List<String> chunkList;
|
||||
|
||||
/**
|
||||
* 知识库kid
|
||||
*/
|
||||
private String kid;
|
||||
|
||||
/**
|
||||
* 文档id
|
||||
*/
|
||||
private String docId;
|
||||
|
||||
/**
|
||||
* 知识块id列表
|
||||
*/
|
||||
private List<String> fids;
|
||||
|
||||
/**
|
||||
* 模型名称
|
||||
*/
|
||||
private String modelName;
|
||||
|
||||
/**
|
||||
* 请求key
|
||||
*/
|
||||
private String apiKey;
|
||||
|
||||
/**
|
||||
* 请求地址
|
||||
*/
|
||||
private String baseUrl;
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package org.ruoyi.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface EmbeddingService {
|
||||
|
||||
void storeEmbeddings(List<String> chunkList, String kid, String docId,List<String> fidList);
|
||||
|
||||
void removeByDocId(String kid,String docId);
|
||||
|
||||
void removeByKid(String kid);
|
||||
|
||||
List<Double> getQueryVector(String query, String kid);
|
||||
|
||||
void createSchema(String kid);
|
||||
|
||||
void removeByKidAndFid(String kid, String fid);
|
||||
|
||||
void saveFragment(String kid, String docId, String fid, String content);
|
||||
}
|
||||
@@ -1,23 +1,26 @@
|
||||
package org.ruoyi.service;
|
||||
|
||||
import org.ruoyi.domain.bo.QueryVectorBo;
|
||||
import org.ruoyi.domain.bo.StoreEmbeddingBo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 向量存储
|
||||
* 向量库管理
|
||||
* @author ageer
|
||||
*/
|
||||
public interface VectorStoreService {
|
||||
|
||||
void storeEmbeddings(List<String> chunkList, List<List<Double>> vectorList, String kid, String docId, List<String> fidList);
|
||||
void storeEmbeddings(StoreEmbeddingBo storeEmbeddingBo);
|
||||
|
||||
void removeByDocId(String kid, String docId);
|
||||
void removeByDocId(String kid,String docId);
|
||||
|
||||
void removeByKid(String kid);
|
||||
|
||||
List<String> nearest(List<Double> queryVector, String kid);
|
||||
List<String> getQueryVector(QueryVectorBo queryVectorBo);
|
||||
|
||||
List<String> nearest(String query, String kid);
|
||||
|
||||
void newSchema(String kid);
|
||||
void createSchema(String kid,String modelName);
|
||||
|
||||
void removeByKidAndFid(String kid, String fid);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package org.ruoyi.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文本向量化
|
||||
*/
|
||||
public interface VectorizationService {
|
||||
|
||||
List<List<Double>> batchVectorization(List<String> chunkList, String kid);
|
||||
|
||||
List<Double> singleVectorization(String chunk, String kid);
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package org.ruoyi.service.impl;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.ruoyi.service.EmbeddingService;
|
||||
import org.ruoyi.service.VectorStoreService;
|
||||
import org.ruoyi.service.VectorizationService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
public class EmbeddingServiceImpl implements EmbeddingService {
|
||||
|
||||
private final VectorStoreService vectorStore;
|
||||
private final VectorizationService vectorization;
|
||||
|
||||
/**
|
||||
* 保存向量数据库
|
||||
* @param chunkList 文档按行切分的片段
|
||||
* @param kid 知识库ID
|
||||
* @param docId 文档ID
|
||||
*/
|
||||
@Override
|
||||
public void storeEmbeddings(List<String> chunkList, String kid, String docId,List<String> fidList) {
|
||||
List<List<Double>> vectorList = vectorization.batchVectorization(chunkList, kid);
|
||||
vectorStore.storeEmbeddings(chunkList,vectorList,kid,docId,fidList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByDocId(String kid,String docId) {
|
||||
vectorStore.removeByDocId(kid,docId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByKid(String kid) {
|
||||
vectorStore.removeByKid(kid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Double> getQueryVector(String query, String kid) {
|
||||
return vectorization.singleVectorization(query,kid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createSchema(String kid) {
|
||||
vectorStore.newSchema(kid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByKidAndFid(String kid, String fid) {
|
||||
vectorStore.removeByKidAndFid(kid,fid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveFragment(String kid, String docId, String fid, String content) {
|
||||
List<String> chunkList = new ArrayList<>();
|
||||
List<String> fidList = new ArrayList<>();
|
||||
chunkList.add(content);
|
||||
fidList.add(fid);
|
||||
storeEmbeddings(chunkList,kid,docId,fidList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package org.ruoyi.service.impl;
|
||||
|
||||
import dev.langchain4j.data.embedding.Embedding;
|
||||
import dev.langchain4j.data.segment.TextSegment;
|
||||
import dev.langchain4j.model.embedding.EmbeddingModel;
|
||||
import dev.langchain4j.model.ollama.OllamaEmbeddingModel;
|
||||
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
|
||||
import dev.langchain4j.model.output.Response;
|
||||
import dev.langchain4j.store.embedding.EmbeddingMatch;
|
||||
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
|
||||
import dev.langchain4j.store.embedding.EmbeddingStore;
|
||||
import dev.langchain4j.store.embedding.filter.Filter;
|
||||
import dev.langchain4j.store.embedding.filter.comparison.IsEqualTo;
|
||||
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
|
||||
import dev.langchain4j.store.embedding.qdrant.QdrantEmbeddingStore;
|
||||
import dev.langchain4j.store.embedding.weaviate.WeaviateEmbeddingStore;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.common.core.service.ConfigService;
|
||||
import org.ruoyi.domain.bo.QueryVectorBo;
|
||||
import org.ruoyi.domain.bo.StoreEmbeddingBo;
|
||||
import org.ruoyi.service.VectorStoreService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import static dev.langchain4j.model.openai.OpenAiEmbeddingModelName.TEXT_EMBEDDING_3_SMALL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 向量库管理
|
||||
* @author ageer
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class VectorStoreServiceImpl implements VectorStoreService {
|
||||
|
||||
private final ConfigService configService;
|
||||
|
||||
Map<String,EmbeddingStore<TextSegment>> storeMap;
|
||||
|
||||
@Override
|
||||
public void createSchema(String kid,String modelName) {
|
||||
EmbeddingStore<TextSegment> embeddingStore = WeaviateEmbeddingStore.builder().build();
|
||||
switch (modelName) {
|
||||
case "weaviate" -> {
|
||||
String protocol = configService.getConfigValue("weaviate", "protocol");
|
||||
String host = configService.getConfigValue("weaviate", "host");
|
||||
String className = configService.getConfigValue("weaviate", "classname");
|
||||
embeddingStore = WeaviateEmbeddingStore.builder()
|
||||
.scheme(protocol)
|
||||
.host(host)
|
||||
.objectClass(className + kid)
|
||||
.scheme(protocol)
|
||||
.avoidDups(true)
|
||||
.consistencyLevel("ALL")
|
||||
.build();
|
||||
}
|
||||
case "milvus" -> {
|
||||
String uri = configService.getConfigValue("milvus", "host");
|
||||
String collection = configService.getConfigValue("milvus", "collection");
|
||||
String dimension = configService.getConfigValue("milvus", "dimension");
|
||||
embeddingStore = MilvusEmbeddingStore.builder()
|
||||
.uri(uri)
|
||||
.collectionName(collection + kid)
|
||||
.dimension(Integer.parseInt(dimension))
|
||||
.build();
|
||||
}
|
||||
case "qdrant" -> {
|
||||
String host = configService.getConfigValue("qdrant", "host");
|
||||
String port = configService.getConfigValue("qdrant", "port");
|
||||
String collectionName = configService.getConfigValue("qdrant", "collectionName");
|
||||
embeddingStore = QdrantEmbeddingStore.builder()
|
||||
.host(host)
|
||||
.port(Integer.parseInt(port))
|
||||
.collectionName(collectionName)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
storeMap.put(kid,embeddingStore);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeEmbeddings(StoreEmbeddingBo storeEmbeddingBo) {
|
||||
EmbeddingStore<TextSegment> store = storeMap.get(storeEmbeddingBo.getKid());
|
||||
EmbeddingModel embeddingModel = getEmbeddingModel(storeEmbeddingBo.getModelName(),
|
||||
storeEmbeddingBo.getApiKey(), storeEmbeddingBo.getBaseUrl());
|
||||
for (int i = 0; i < storeEmbeddingBo.getChunkList().size(); i++) {
|
||||
Map<String, Object> dataSchema = new HashMap<>();
|
||||
dataSchema.put("kid", storeEmbeddingBo.getKid());
|
||||
dataSchema.put("docId", storeEmbeddingBo.getKid());
|
||||
dataSchema.put("fid", storeEmbeddingBo.getFids().get(i));
|
||||
Response<Embedding> response = embeddingModel.embed(storeEmbeddingBo.getChunkList().get(i));
|
||||
Embedding embedding = response.content();
|
||||
TextSegment segment = TextSegment.from(storeEmbeddingBo.getChunkList().get(i));
|
||||
segment.metadata().putAll(dataSchema);
|
||||
|
||||
store.add(embedding,segment);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getQueryVector(QueryVectorBo queryVectorBo) {
|
||||
EmbeddingStore<TextSegment> store = storeMap.get(queryVectorBo.getKid());
|
||||
|
||||
EmbeddingModel embeddingModel = getEmbeddingModel(queryVectorBo.getModelName(),
|
||||
queryVectorBo.getApiKey(), queryVectorBo.getBaseUrl());
|
||||
Filter simpleFilter = new IsEqualTo("kid", queryVectorBo.getKid());
|
||||
Embedding queryEmbedding = embeddingModel.embed(queryVectorBo.getQuery()).content();
|
||||
EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
|
||||
.queryEmbedding(queryEmbedding)
|
||||
.maxResults(queryVectorBo.getMaxResults())
|
||||
// 添加过滤条件
|
||||
.filter(simpleFilter)
|
||||
.build();
|
||||
List<EmbeddingMatch<TextSegment>> matches = store.search(embeddingSearchRequest).matches();
|
||||
|
||||
List<String> results = new ArrayList<>();
|
||||
|
||||
matches.forEach(embeddingMatch -> results.add(embeddingMatch.embedded().text()));
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeByKid(String kid) {
|
||||
EmbeddingStore<TextSegment> store = storeMap.get(kid);
|
||||
|
||||
// 根据条件删除向量数据
|
||||
Filter simpleFilter = new IsEqualTo("kid", kid);
|
||||
store.removeAll(simpleFilter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByDocId(String kid, String docId) {
|
||||
EmbeddingStore<TextSegment> store = storeMap.get(kid);
|
||||
// 根据条件删除向量数据
|
||||
Filter simpleFilterByDocId = new IsEqualTo("docId", docId);
|
||||
store.removeAll(simpleFilterByDocId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByKidAndFid(String kid, String fid) {
|
||||
EmbeddingStore<TextSegment> store = storeMap.get(kid);
|
||||
// 根据条件删除向量数据
|
||||
Filter simpleFilterByKid = new IsEqualTo("kid", kid);
|
||||
Filter simpleFilterFid = new IsEqualTo("fid", fid);
|
||||
Filter simpleFilterByAnd = Filter.and(simpleFilterFid, simpleFilterByKid);
|
||||
store.removeAll(simpleFilterByAnd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取向量模型
|
||||
*/
|
||||
public EmbeddingModel getEmbeddingModel(String modelName,String apiKey,String baseUrl) {
|
||||
EmbeddingModel embeddingModel = OpenAiEmbeddingModel.builder().build();
|
||||
if(TEXT_EMBEDDING_3_SMALL.toString().equals(modelName)) {
|
||||
embeddingModel = OpenAiEmbeddingModel.builder()
|
||||
.apiKey(apiKey)
|
||||
.baseUrl(baseUrl)
|
||||
.modelName(TEXT_EMBEDDING_3_SMALL)
|
||||
.build();
|
||||
// TODO 添加枚举
|
||||
}else if("quentinz/bge-large-zh-v1.5".equals(modelName)) {
|
||||
embeddingModel = OllamaEmbeddingModel.builder()
|
||||
.baseUrl(baseUrl)
|
||||
.modelName(modelName)
|
||||
.build();
|
||||
}
|
||||
return embeddingModel;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,402 +0,0 @@
|
||||
package org.ruoyi.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.UUID;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.google.gson.internal.LinkedTreeMap;
|
||||
import io.weaviate.client.Config;
|
||||
import io.weaviate.client.WeaviateClient;
|
||||
import io.weaviate.client.base.Result;
|
||||
import io.weaviate.client.v1.data.model.WeaviateObject;
|
||||
import io.weaviate.client.v1.data.replication.model.ConsistencyLevel;
|
||||
import io.weaviate.client.v1.filters.Operator;
|
||||
import io.weaviate.client.v1.filters.WhereFilter;
|
||||
import io.weaviate.client.v1.graphql.model.GraphQLResponse;
|
||||
import io.weaviate.client.v1.graphql.query.argument.NearTextArgument;
|
||||
import io.weaviate.client.v1.graphql.query.argument.NearVectorArgument;
|
||||
import io.weaviate.client.v1.graphql.query.fields.Field;
|
||||
import io.weaviate.client.v1.misc.model.Meta;
|
||||
import io.weaviate.client.v1.misc.model.ReplicationConfig;
|
||||
import io.weaviate.client.v1.misc.model.ShardingConfig;
|
||||
import io.weaviate.client.v1.misc.model.VectorIndexConfig;
|
||||
import io.weaviate.client.v1.schema.model.DataType;
|
||||
import io.weaviate.client.v1.schema.model.Property;
|
||||
import io.weaviate.client.v1.schema.model.Schema;
|
||||
import io.weaviate.client.v1.schema.model.WeaviateClass;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.ruoyi.common.core.service.ConfigService;
|
||||
import org.ruoyi.domain.vo.KnowledgeInfoVo;
|
||||
import org.ruoyi.service.IKnowledgeInfoService;
|
||||
import org.ruoyi.service.VectorStoreService;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class WeaviateVectorStoreImpl implements VectorStoreService {
|
||||
|
||||
private volatile String protocol;
|
||||
private volatile String host;
|
||||
private volatile String className;
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private IKnowledgeInfoService knowledgeInfoService;
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private ConfigService configService;
|
||||
|
||||
@PostConstruct
|
||||
public void loadConfig() {
|
||||
this.protocol = configService.getConfigValue("weaviate", "protocol");
|
||||
this.host = configService.getConfigValue("weaviate", "host");
|
||||
this.className = configService.getConfigValue("weaviate", "classname");
|
||||
}
|
||||
|
||||
public WeaviateClient getClient() {
|
||||
Config config = new Config(protocol, host);
|
||||
WeaviateClient client = new WeaviateClient(config);
|
||||
return client;
|
||||
}
|
||||
|
||||
public Result<Meta> getMeta() {
|
||||
WeaviateClient client = getClient();
|
||||
Result<Meta> meta = client.misc().metaGetter().run();
|
||||
if (meta.getError() == null) {
|
||||
System.out.printf("meta.hostname: %s\n", meta.getResult().getHostname());
|
||||
System.out.printf("meta.version: %s\n", meta.getResult().getVersion());
|
||||
System.out.printf("meta.modules: %s\n", meta.getResult().getModules());
|
||||
} else {
|
||||
System.out.printf("Error: %s\n", meta.getError().getMessages());
|
||||
}
|
||||
return meta;
|
||||
}
|
||||
|
||||
public Result<Schema> getSchemas() {
|
||||
WeaviateClient client = getClient();
|
||||
Result<Schema> result = client.schema().getter().run();
|
||||
if (result.hasErrors()) {
|
||||
System.out.println(result.getError());
|
||||
} else {
|
||||
System.out.println(result.getResult());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public Result<Boolean> createSchema(String kid) {
|
||||
WeaviateClient client = getClient();
|
||||
|
||||
VectorIndexConfig vectorIndexConfig = VectorIndexConfig.builder()
|
||||
.distance("cosine")
|
||||
.cleanupIntervalSeconds(300)
|
||||
.efConstruction(128)
|
||||
.maxConnections(64)
|
||||
.vectorCacheMaxObjects(500000L)
|
||||
.ef(-1)
|
||||
.skip(false)
|
||||
.dynamicEfFactor(8)
|
||||
.dynamicEfMax(500)
|
||||
.dynamicEfMin(100)
|
||||
.flatSearchCutoff(40000)
|
||||
.build();
|
||||
|
||||
ShardingConfig shardingConfig = ShardingConfig.builder()
|
||||
.desiredCount(3)
|
||||
.desiredVirtualCount(128)
|
||||
.function("murmur3")
|
||||
.key("_id")
|
||||
.strategy("hash")
|
||||
.virtualPerPhysical(128)
|
||||
.build();
|
||||
|
||||
ReplicationConfig replicationConfig = ReplicationConfig.builder()
|
||||
.factor(1)
|
||||
.build();
|
||||
|
||||
JSONObject classModuleConfigValue = new JSONObject();
|
||||
classModuleConfigValue.put("vectorizeClassName", false);
|
||||
JSONObject classModuleConfig = new JSONObject();
|
||||
classModuleConfig.put("text2vec-transformers", classModuleConfigValue);
|
||||
|
||||
JSONObject propertyModuleConfigValueSkipTrue = new JSONObject();
|
||||
propertyModuleConfigValueSkipTrue.put("vectorizePropertyName", false);
|
||||
propertyModuleConfigValueSkipTrue.put("skip", true);
|
||||
JSONObject propertyModuleConfigSkipTrue = new JSONObject();
|
||||
propertyModuleConfigSkipTrue.put("text2vec-transformers", propertyModuleConfigValueSkipTrue);
|
||||
|
||||
JSONObject propertyModuleConfigValueSkipFalse = new JSONObject();
|
||||
propertyModuleConfigValueSkipFalse.put("vectorizePropertyName", false);
|
||||
propertyModuleConfigValueSkipFalse.put("skip", false);
|
||||
JSONObject propertyModuleConfigSkipFalse = new JSONObject();
|
||||
propertyModuleConfigSkipFalse.put("text2vec-transformers", propertyModuleConfigValueSkipFalse);
|
||||
|
||||
WeaviateClass clazz = WeaviateClass.builder()
|
||||
.className(className + kid)
|
||||
.description("local knowledge")
|
||||
.vectorIndexType("hnsw")
|
||||
.vectorizer("text2vec-transformers")
|
||||
.shardingConfig(shardingConfig)
|
||||
.vectorIndexConfig(vectorIndexConfig)
|
||||
.replicationConfig(replicationConfig)
|
||||
.moduleConfig(classModuleConfig)
|
||||
.properties(new ArrayList() {
|
||||
{
|
||||
add(Property.builder()
|
||||
.dataType(new ArrayList() {
|
||||
{
|
||||
add(DataType.TEXT);
|
||||
}
|
||||
})
|
||||
.name("content")
|
||||
.description("The content of the local knowledge,for search")
|
||||
.moduleConfig(propertyModuleConfigSkipFalse)
|
||||
.build());
|
||||
add(Property.builder()
|
||||
.dataType(new ArrayList() {
|
||||
{
|
||||
add(DataType.TEXT);
|
||||
}
|
||||
})
|
||||
.name("kid")
|
||||
.description("The knowledge id of the local knowledge,for search")
|
||||
.moduleConfig(propertyModuleConfigSkipTrue)
|
||||
.build());
|
||||
add(Property.builder()
|
||||
.dataType(new ArrayList() {
|
||||
{
|
||||
add(DataType.TEXT);
|
||||
}
|
||||
})
|
||||
.name("docId")
|
||||
.description("The doc id of the local knowledge,for search")
|
||||
.moduleConfig(propertyModuleConfigSkipTrue)
|
||||
.build());
|
||||
add(Property.builder()
|
||||
.dataType(new ArrayList() {
|
||||
{
|
||||
add(DataType.TEXT);
|
||||
}
|
||||
})
|
||||
.name("fid")
|
||||
.description("The fragment id of the local knowledge,for search")
|
||||
.moduleConfig(propertyModuleConfigSkipTrue)
|
||||
.build());
|
||||
add(Property.builder()
|
||||
.dataType(new ArrayList() {
|
||||
{
|
||||
add(DataType.TEXT);
|
||||
}
|
||||
})
|
||||
.name("uuid")
|
||||
.description("The uuid id of the local knowledge fragment(same with id properties),for search")
|
||||
.moduleConfig(propertyModuleConfigSkipTrue)
|
||||
.build());
|
||||
} })
|
||||
.build();
|
||||
|
||||
Result<Boolean> result = client.schema().classCreator().withClass(clazz).run();
|
||||
if (result.hasErrors()) {
|
||||
System.out.println(result.getError());
|
||||
}
|
||||
System.out.println(result.getResult());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void newSchema(String kid) {
|
||||
createSchema(kid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByKidAndFid(String kid, String fid) {
|
||||
List<String> resultList = new ArrayList<>();
|
||||
WeaviateClient client = getClient();
|
||||
Field fieldId = Field.builder().name("uuid").build();
|
||||
WhereFilter where = WhereFilter.builder()
|
||||
.path(new String[]{"fid"})
|
||||
.operator(Operator.Equal)
|
||||
.valueString(fid)
|
||||
.build();
|
||||
Result<GraphQLResponse> result = client.graphQL().get()
|
||||
.withClassName(className + kid)
|
||||
.withFields(fieldId)
|
||||
.withWhere(where)
|
||||
.run();
|
||||
LinkedTreeMap<String, Object> t = (LinkedTreeMap<String, Object>) result.getResult().getData();
|
||||
LinkedTreeMap<String, ArrayList<LinkedTreeMap>> l = (LinkedTreeMap<String, ArrayList<LinkedTreeMap>>) t.get("Get");
|
||||
ArrayList<LinkedTreeMap> m = l.get(className + kid);
|
||||
for (LinkedTreeMap linkedTreeMap : m) {
|
||||
String uuid = linkedTreeMap.get("uuid").toString();
|
||||
resultList.add(uuid);
|
||||
}
|
||||
for (String uuid : resultList) {
|
||||
Result<Boolean> deleteResult = client.data().deleter()
|
||||
.withID(uuid)
|
||||
.withClassName(className + kid)
|
||||
.withConsistencyLevel(ConsistencyLevel.ALL) // default QUORUM
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void storeEmbeddings(List<String> chunkList, List<List<Double>> vectorList, String kid, String docId, List<String> fidList) {
|
||||
WeaviateClient client = getClient();
|
||||
|
||||
for (int i = 0; i < Math.min(chunkList.size(), vectorList.size()); i++) {
|
||||
List<Double> vector = vectorList.get(i);
|
||||
Float[] vf = vector.stream().map(Double::floatValue).toArray(Float[]::new);
|
||||
|
||||
Map<String, Object> dataSchema = new HashMap<>();
|
||||
dataSchema.put("content", chunkList.get(i));
|
||||
dataSchema.put("kid", kid);
|
||||
dataSchema.put("docId", docId);
|
||||
dataSchema.put("fid", fidList.get(i));
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
dataSchema.put("uuid", uuid);
|
||||
|
||||
Result<WeaviateObject> result = client.data().creator()
|
||||
.withClassName(className + kid)
|
||||
.withID(uuid)
|
||||
.withVector(vf)
|
||||
.withProperties(dataSchema)
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByDocId(String kid, String docId) {
|
||||
List<String> resultList = new ArrayList<>();
|
||||
WeaviateClient client = getClient();
|
||||
Field fieldId = Field.builder().name("uuid").build();
|
||||
WhereFilter where = WhereFilter.builder()
|
||||
.path(new String[]{"docId"})
|
||||
.operator(Operator.Equal)
|
||||
.valueString(docId)
|
||||
.build();
|
||||
Result<GraphQLResponse> result = client.graphQL().get()
|
||||
.withClassName(className + kid)
|
||||
.withFields(fieldId)
|
||||
.withWhere(where)
|
||||
.run();
|
||||
LinkedTreeMap<String, Object> t = (LinkedTreeMap<String, Object>) result.getResult().getData();
|
||||
LinkedTreeMap<String, ArrayList<LinkedTreeMap>> l = (LinkedTreeMap<String, ArrayList<LinkedTreeMap>>) t.get("Get");
|
||||
ArrayList<LinkedTreeMap> m = l.get(className + kid);
|
||||
for (LinkedTreeMap linkedTreeMap : m) {
|
||||
String uuid = linkedTreeMap.get("uuid").toString();
|
||||
resultList.add(uuid);
|
||||
}
|
||||
for (String uuid : resultList) {
|
||||
Result<Boolean> deleteResult = client.data().deleter()
|
||||
.withID(uuid)
|
||||
.withClassName(className + kid)
|
||||
.withConsistencyLevel(ConsistencyLevel.ALL) // default QUORUM
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeByKid(String kid) {
|
||||
WeaviateClient client = getClient();
|
||||
Result<Boolean> result = client.schema().classDeleter().withClassName(className + kid).run();
|
||||
if (result.hasErrors()) {
|
||||
System.out.println("删除schema失败" + result.getError());
|
||||
} else {
|
||||
System.out.println("删除schema成功" + result.getResult());
|
||||
}
|
||||
log.info("drop schema by kid, result = {}", result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> nearest(List<Double> queryVector, String kid) {
|
||||
if (StringUtils.isBlank(kid)) {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
List<String> resultList = new ArrayList<>();
|
||||
Float[] vf = new Float[queryVector.size()];
|
||||
for (int j = 0; j < queryVector.size(); j++) {
|
||||
Double value = queryVector.get(j);
|
||||
vf[j] = value.floatValue();
|
||||
}
|
||||
WeaviateClient client = getClient();
|
||||
Field contentField = Field.builder().name("content").build();
|
||||
Field _additional = Field.builder()
|
||||
.name("_additional")
|
||||
.fields(new Field[]{
|
||||
Field.builder().name("distance").build()
|
||||
}).build();
|
||||
NearVectorArgument nearVector = NearVectorArgument.builder()
|
||||
.vector(vf)
|
||||
.distance(1.6f) // certainty = 1f - distance /2f
|
||||
.build();
|
||||
KnowledgeInfoVo knowledgeInfoVo = knowledgeInfoService.queryById(Long.valueOf(kid));
|
||||
Result<GraphQLResponse> result = client.graphQL().get()
|
||||
.withClassName(className + kid)
|
||||
.withFields(contentField, _additional)
|
||||
.withNearVector(nearVector)
|
||||
.withLimit(knowledgeInfoVo.getRetrieveLimit())
|
||||
.run();
|
||||
LinkedTreeMap<String, Object> t = (LinkedTreeMap<String, Object>) result.getResult().getData();
|
||||
LinkedTreeMap<String, ArrayList<LinkedTreeMap>> l = (LinkedTreeMap<String, ArrayList<LinkedTreeMap>>) t.get("Get");
|
||||
ArrayList<LinkedTreeMap> m = l.get(className + kid);
|
||||
for (LinkedTreeMap linkedTreeMap : m) {
|
||||
String content = linkedTreeMap.get("content").toString();
|
||||
resultList.add(content);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> nearest(String query, String kid) {
|
||||
if (StringUtils.isBlank(kid)) {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
List<String> resultList = new ArrayList<>();
|
||||
WeaviateClient client = getClient();
|
||||
Field contentField = Field.builder().name("content").build();
|
||||
Field _additional = Field.builder()
|
||||
.name("_additional")
|
||||
.fields(new Field[]{
|
||||
Field.builder().name("distance").build()
|
||||
}).build();
|
||||
NearTextArgument nearText = client.graphQL().arguments().nearTextArgBuilder()
|
||||
.concepts(new String[]{query})
|
||||
.distance(1.6f) // certainty = 1f - distance /2f
|
||||
.build();
|
||||
KnowledgeInfoVo knowledgeInfoVo = knowledgeInfoService.queryById(Long.valueOf(kid));
|
||||
Result<GraphQLResponse> result = client.graphQL().get()
|
||||
.withClassName(className + kid)
|
||||
.withFields(contentField, _additional)
|
||||
.withNearText(nearText)
|
||||
.withLimit(knowledgeInfoVo.getRetrieveLimit())
|
||||
.run();
|
||||
LinkedTreeMap<String, Object> t = (LinkedTreeMap<String, Object>) result.getResult().getData();
|
||||
LinkedTreeMap<String, ArrayList<LinkedTreeMap>> l = (LinkedTreeMap<String, ArrayList<LinkedTreeMap>>) t.get("Get");
|
||||
ArrayList<LinkedTreeMap> m = l.get(className + kid);
|
||||
for (LinkedTreeMap linkedTreeMap : m) {
|
||||
String content = linkedTreeMap.get("content").toString();
|
||||
resultList.add(content);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
public Result<Boolean> deleteSchema(String kid) {
|
||||
WeaviateClient client = getClient();
|
||||
Result<Boolean> result = client.schema().classDeleter().withClassName(className + kid).run();
|
||||
if (result.hasErrors()) {
|
||||
System.out.println(result.getError());
|
||||
} else {
|
||||
System.out.println(result.getResult());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package org.ruoyi.chat.controller.chat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.constraints.*;
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import org.ruoyi.common.excel.utils.ExcelUtil;
|
||||
import org.ruoyi.common.idempotent.annotation.RepeatSubmit;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.domain.bo.ChatSessionBo;
|
||||
import org.ruoyi.domain.vo.ChatSessionVo;
|
||||
import org.ruoyi.service.IChatSessionService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.ruoyi.common.log.annotation.Log;
|
||||
import org.ruoyi.common.web.core.BaseController;
|
||||
import org.ruoyi.core.page.PageQuery;
|
||||
import org.ruoyi.common.core.domain.R;
|
||||
import org.ruoyi.common.core.validate.AddGroup;
|
||||
import org.ruoyi.common.core.validate.EditGroup;
|
||||
import org.ruoyi.common.log.enums.BusinessType;
|
||||
|
||||
/**
|
||||
* 会话管理
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/system/session")
|
||||
public class ChatSessionController extends BaseController {
|
||||
|
||||
private final IChatSessionService chatSessionService;
|
||||
|
||||
/**
|
||||
* 查询会话管理列表
|
||||
*/
|
||||
@SaCheckPermission("system:session:list")
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo<ChatSessionVo> list(ChatSessionBo bo, PageQuery pageQuery) {
|
||||
return chatSessionService.queryPageList(bo, pageQuery);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出会话管理列表
|
||||
*/
|
||||
@SaCheckPermission("system:session:export")
|
||||
@Log(title = "会话管理", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/export")
|
||||
public void export(ChatSessionBo bo, HttpServletResponse response) {
|
||||
List<ChatSessionVo> list = chatSessionService.queryList(bo);
|
||||
ExcelUtil.exportExcel(list, "会话管理", ChatSessionVo.class, response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会话管理详细信息
|
||||
*
|
||||
* @param id 主键
|
||||
*/
|
||||
@SaCheckPermission("system:session:query")
|
||||
@GetMapping("/{id}")
|
||||
public R<ChatSessionVo> getInfo(@NotNull(message = "主键不能为空")
|
||||
@PathVariable Long id) {
|
||||
return R.ok(chatSessionService.queryById(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增会话管理
|
||||
*/
|
||||
@SaCheckPermission("system:session:add")
|
||||
@Log(title = "会话管理", businessType = BusinessType.INSERT)
|
||||
@RepeatSubmit()
|
||||
@PostMapping()
|
||||
public R<Void> add(@Validated(AddGroup.class) @RequestBody ChatSessionBo bo) {
|
||||
return toAjax(chatSessionService.insertByBo(bo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改会话管理
|
||||
*/
|
||||
@SaCheckPermission("system:session:edit")
|
||||
@Log(title = "会话管理", businessType = BusinessType.UPDATE)
|
||||
@RepeatSubmit()
|
||||
@PutMapping()
|
||||
public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChatSessionBo bo) {
|
||||
return toAjax(chatSessionService.updateByBo(bo));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除会话管理
|
||||
*
|
||||
* @param ids 主键串
|
||||
*/
|
||||
@SaCheckPermission("system:session:remove")
|
||||
@Log(title = "会话管理", businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ids}")
|
||||
public R<Void> remove(@NotEmpty(message = "主键不能为空")
|
||||
@PathVariable Long[] ids) {
|
||||
return toAjax(chatSessionService.deleteWithValidByIds(List.of(ids), true));
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,10 @@ import org.springframework.web.multipart.MultipartFile;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ageer
|
||||
* 知识库管理
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Validated
|
||||
@RequiredArgsConstructor
|
||||
|
||||
@@ -16,6 +16,12 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
||||
/**
|
||||
* 绘画(换脸)任务查询
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Api(tags = "任务查询")
|
||||
@RestController
|
||||
@RequestMapping("/mj")
|
||||
|
||||
@@ -14,10 +14,10 @@ import org.ruoyi.common.core.utils.OkHttpUtil;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 描述:文生视频
|
||||
* 文生视频
|
||||
*
|
||||
* @author ageerle@163.com
|
||||
* date 2024/6/27
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/luma")
|
||||
|
||||
@@ -15,9 +15,14 @@ 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;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 绘画任务提交
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Api(tags = "任务提交")
|
||||
@RestController
|
||||
@RequestMapping("/mj/submit")
|
||||
|
||||
@@ -14,6 +14,13 @@ import org.ruoyi.chat.service.chat.IChatCostService;
|
||||
import org.ruoyi.common.core.utils.OkHttpUtil;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
/**
|
||||
* 文生歌曲任务提交
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/sunoapi")
|
||||
@RequiredArgsConstructor
|
||||
|
||||
@@ -11,6 +11,13 @@ import org.ruoyi.chat.domain.dto.TaskConditionDTO;
|
||||
import org.ruoyi.chat.util.MjOkHttpUtil;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
/**
|
||||
* 绘画任务查询
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Api(tags = "任务查询")
|
||||
@RestController
|
||||
@RequestMapping("/mj/task")
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
package org.ruoyi.chat.factory;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.ruoyi.chat.service.knowledge.BgeLargeVectorizationImpl;
|
||||
import org.ruoyi.chat.service.knowledge.OpenAiVectorizationImpl;
|
||||
import org.ruoyi.domain.vo.KnowledgeInfoVo;
|
||||
import org.ruoyi.service.IKnowledgeInfoService;
|
||||
import org.ruoyi.service.VectorizationService;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 文本向量化
|
||||
* @author huangkh
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class VectorizationFactory {
|
||||
|
||||
private final OpenAiVectorizationImpl openAiVectorization;
|
||||
|
||||
private final BgeLargeVectorizationImpl bgeLargeVectorization;
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private IKnowledgeInfoService knowledgeInfoService;
|
||||
|
||||
public VectorizationFactory(OpenAiVectorizationImpl openAiVectorization, BgeLargeVectorizationImpl bgeLargeVectorization) {
|
||||
this.openAiVectorization = openAiVectorization;
|
||||
this.bgeLargeVectorization = bgeLargeVectorization;
|
||||
}
|
||||
|
||||
public VectorizationService getEmbedding(String kid){
|
||||
String vectorModel = "text-embedding-3-small";
|
||||
if (StrUtil.isNotEmpty(kid)) {
|
||||
KnowledgeInfoVo knowledgeInfoVo = knowledgeInfoService.queryById(Long.valueOf(kid));
|
||||
if (knowledgeInfoVo != null && StrUtil.isNotEmpty(knowledgeInfoVo.getVectorModel())) {
|
||||
vectorModel = knowledgeInfoVo.getVectorModel();
|
||||
}
|
||||
}
|
||||
return switch (vectorModel) {
|
||||
case "quentinz/bge-large-zh-v1.5" -> bgeLargeVectorization;
|
||||
default -> openAiVectorization;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ public class ChatCostServiceImpl implements IChatCostService {
|
||||
/**
|
||||
* 扣除用户余额
|
||||
*/
|
||||
@Override
|
||||
public void deductToken(ChatRequest chatRequest) {
|
||||
|
||||
int tokens = TikTokensUtil.tokens(chatRequest.getModel(), chatRequest.getPrompt());
|
||||
@@ -53,6 +54,7 @@ public class ChatCostServiceImpl implements IChatCostService {
|
||||
String modelName = chatRequest.getModel();
|
||||
|
||||
ChatMessageBo chatMessageBo = new ChatMessageBo();
|
||||
chatMessageBo.setSessionId(chatRequest.getSessionId());
|
||||
|
||||
Object userId = LocalCache.CACHE.get("userId");
|
||||
if(userId!=null){
|
||||
|
||||
@@ -24,10 +24,12 @@ import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.core.utils.file.FileUtils;
|
||||
import org.ruoyi.common.core.utils.file.MimeTypeUtils;
|
||||
import org.ruoyi.common.redis.utils.RedisUtils;
|
||||
import org.ruoyi.domain.bo.ChatSessionBo;
|
||||
import org.ruoyi.domain.bo.QueryVectorBo;
|
||||
import org.ruoyi.domain.vo.ChatModelVo;
|
||||
import org.ruoyi.service.EmbeddingService;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.ruoyi.service.VectorStoreService;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.ruoyi.service.IChatSessionService;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
@@ -53,9 +55,7 @@ public class SseServiceImpl implements ISseService {
|
||||
|
||||
private final OpenAiStreamClient openAiStreamClient;
|
||||
|
||||
private final EmbeddingService embeddingService;
|
||||
|
||||
private final VectorStoreService vectorStore;
|
||||
private final VectorStoreService vectorStoreService;
|
||||
|
||||
private final IChatCostService chatCostService;
|
||||
|
||||
@@ -65,6 +65,8 @@ public class SseServiceImpl implements ISseService {
|
||||
|
||||
private final OllamaServiceImpl ollamaService;
|
||||
|
||||
private final IChatSessionService chatSessionService;
|
||||
|
||||
private ChatModelVo chatModelVo;
|
||||
|
||||
|
||||
@@ -80,6 +82,15 @@ public class SseServiceImpl implements ISseService {
|
||||
}else {
|
||||
LocalCache.CACHE.put("userId", chatCostService.getUserId());
|
||||
chatRequest.setUserId(chatCostService.getUserId());
|
||||
// 保存会话信息
|
||||
if(chatRequest.getSessionId()==null){
|
||||
ChatSessionBo chatSessionBo = new ChatSessionBo();
|
||||
chatSessionBo.setUserId(chatCostService.getUserId());
|
||||
chatSessionBo.setSessionTitle(getFirst10Characters(chatRequest.getPrompt()));
|
||||
chatSessionBo.setSessionContent(chatRequest.getPrompt());
|
||||
chatSessionService.insertByBo(chatSessionBo);
|
||||
chatRequest.setSessionId(chatSessionBo.getId());
|
||||
}
|
||||
// 保存消息记录 并扣除费用
|
||||
chatCostService.deductToken(chatRequest);
|
||||
}
|
||||
@@ -92,6 +103,23 @@ public class SseServiceImpl implements ISseService {
|
||||
return sseEmitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对话标题
|
||||
*
|
||||
* @param str 原字符
|
||||
* @return 截取后的字符
|
||||
*/
|
||||
public static String getFirst10Characters(String str) {
|
||||
// 判断字符串长度
|
||||
if (str.length() > 10) {
|
||||
// 如果长度大于10,截取前10个字符
|
||||
return str.substring(0, 10);
|
||||
} else {
|
||||
// 如果长度不足10,返回整个字符串
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查未登录用户是否超过当日对话次数限制
|
||||
*
|
||||
@@ -139,7 +167,10 @@ public class SseServiceImpl implements ISseService {
|
||||
// 获取对话消息列表
|
||||
List<Message> messages = chatRequest.getMessages();
|
||||
String sysPrompt = chatModelVo.getSystemPrompt();
|
||||
|
||||
|
||||
if(StringUtils.isEmpty(sysPrompt)){
|
||||
// TODO 系统默认提示词,后续会增加提示词管理
|
||||
sysPrompt ="你是一个由RuoYI-AI开发的人工智能助手,名字叫熊猫助手。你擅长中英文对话,能够理解并处理各种问题,提供安全、有帮助、准确的回答。" +
|
||||
"当前时间:"+ DateUtils.getDate()+
|
||||
"#注意:回复之前注意结合上下文和工具返回内容进行回复。";
|
||||
@@ -153,13 +184,20 @@ public class SseServiceImpl implements ISseService {
|
||||
if(StringUtils.isNotEmpty(chatRequest.getKid())){
|
||||
List<Message> knMessages = new ArrayList<>();
|
||||
String content = messages.get(messages.size() - 1).getContent().toString();
|
||||
List<String> nearestList;
|
||||
List<Double> queryVector = embeddingService.getQueryVector(content, chatRequest.getKid());
|
||||
nearestList = vectorStore.nearest(queryVector, chatRequest.getKid());
|
||||
QueryVectorBo queryVectorBo = new QueryVectorBo();
|
||||
queryVectorBo.setQuery(content);
|
||||
queryVectorBo.setKid(chatRequest.getKid());
|
||||
queryVectorBo.setApiKey(chatModelVo.getApiKey());
|
||||
queryVectorBo.setBaseUrl(chatModelVo.getApiHost());
|
||||
queryVectorBo.setModelName(chatModelVo.getModelName());
|
||||
// TODO 查询向量返回条数,这里应该查询知识库配置
|
||||
queryVectorBo.setMaxResults(3);
|
||||
List<String> nearestList = vectorStoreService.getQueryVector(queryVectorBo);
|
||||
for (String prompt : nearestList) {
|
||||
Message userMessage = Message.builder().content(prompt).role(Message.Role.USER).build();
|
||||
knMessages.add(userMessage);
|
||||
}
|
||||
// TODO 提示词,这里应该查询知识库配置
|
||||
Message userMessage = Message.builder().content(content + (!nearestList.isEmpty() ? "\n\n注意:回答问题时,须严格根据我给你的系统上下文内容原文进行回答,请不要自己发挥,回答时保持原来文本的段落层级" : "")).role(Message.Role.USER).build();
|
||||
knMessages.add(userMessage);
|
||||
messages.addAll(knMessages);
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
package org.ruoyi.chat.service.knowledge;
|
||||
|
||||
import io.github.ollama4j.OllamaAPI;
|
||||
import io.github.ollama4j.models.embeddings.OllamaEmbeddingsRequestModel;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.common.core.exception.ServiceException;
|
||||
import org.ruoyi.domain.vo.ChatModelVo;
|
||||
import org.ruoyi.domain.vo.KnowledgeInfoVo;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.ruoyi.service.IKnowledgeInfoService;
|
||||
import org.ruoyi.service.VectorizationService;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ageer
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class BgeLargeVectorizationImpl implements VectorizationService {
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private IKnowledgeInfoService knowledgeInfoService;
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private final IChatModelService chatModelService;
|
||||
|
||||
@Override
|
||||
public List<List<Double>> batchVectorization(List<String> chunkList, String kid) {
|
||||
|
||||
KnowledgeInfoVo knowledgeInfoVo = knowledgeInfoService.queryById(Long.valueOf(kid));
|
||||
|
||||
ChatModelVo chatModelVo = chatModelService.selectModelByName(knowledgeInfoVo.getVectorModel());
|
||||
|
||||
OllamaAPI api = new OllamaAPI(chatModelVo.getApiHost());
|
||||
|
||||
List<Double> doubleVector;
|
||||
List<List<Double>> vectorList = new ArrayList<>();
|
||||
try {
|
||||
for (String chunk : chunkList) {
|
||||
doubleVector = api.generateEmbeddings(new OllamaEmbeddingsRequestModel(knowledgeInfoVo.getVectorModel(), chunk));
|
||||
vectorList.add(doubleVector);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new ServiceException("文本向量化异常:"+e.getMessage());
|
||||
}
|
||||
return vectorList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Double> singleVectorization(String chunk, String kid) {
|
||||
List<String> chunkList = new ArrayList<>();
|
||||
chunkList.add(chunk);
|
||||
List<List<Double>> vectorList = batchVectorization(chunkList, kid);
|
||||
return vectorList.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package org.ruoyi.chat.service.knowledge;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -14,17 +15,23 @@ import org.ruoyi.common.core.utils.StringUtils;
|
||||
import org.ruoyi.common.satoken.utils.LoginHelper;
|
||||
import org.ruoyi.core.page.PageQuery;
|
||||
import org.ruoyi.core.page.TableDataInfo;
|
||||
import org.ruoyi.domain.ChatModel;
|
||||
import org.ruoyi.domain.KnowledgeAttach;
|
||||
import org.ruoyi.domain.KnowledgeFragment;
|
||||
import org.ruoyi.domain.KnowledgeInfo;
|
||||
import org.ruoyi.domain.bo.KnowledgeInfoBo;
|
||||
import org.ruoyi.domain.bo.KnowledgeInfoUploadBo;
|
||||
import org.ruoyi.domain.bo.StoreEmbeddingBo;
|
||||
import org.ruoyi.domain.vo.ChatModelVo;
|
||||
import org.ruoyi.domain.vo.KnowledgeInfoVo;
|
||||
import org.ruoyi.mapper.KnowledgeAttachMapper;
|
||||
import org.ruoyi.mapper.KnowledgeFragmentMapper;
|
||||
import org.ruoyi.mapper.KnowledgeInfoMapper;
|
||||
import org.ruoyi.service.EmbeddingService;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.ruoyi.service.VectorStoreService;
|
||||
import org.ruoyi.service.IKnowledgeInfoService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -42,9 +49,10 @@ import java.util.*;
|
||||
@Service
|
||||
public class KnowledgeInfoServiceImpl implements IKnowledgeInfoService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(KnowledgeInfoServiceImpl.class);
|
||||
private final KnowledgeInfoMapper baseMapper;
|
||||
|
||||
private final EmbeddingService embeddingService;
|
||||
private final VectorStoreService vectorStoreService;
|
||||
|
||||
private final ResourceLoaderFactory resourceLoaderFactory;
|
||||
|
||||
@@ -52,6 +60,8 @@ public class KnowledgeInfoServiceImpl implements IKnowledgeInfoService {
|
||||
|
||||
private final KnowledgeAttachMapper attachMapper;
|
||||
|
||||
private final IChatModelService chatModelService;
|
||||
|
||||
/**
|
||||
* 查询知识库
|
||||
*/
|
||||
@@ -150,7 +160,9 @@ public class KnowledgeInfoServiceImpl implements IKnowledgeInfoService {
|
||||
knowledgeInfo.setUid(LoginHelper.getLoginUser().getUserId());
|
||||
}
|
||||
baseMapper.insert(knowledgeInfo);
|
||||
embeddingService.createSchema(String.valueOf(knowledgeInfo.getId()));
|
||||
if (knowledgeInfo != null) {
|
||||
vectorStoreService.createSchema(String.valueOf(knowledgeInfo.getId()),bo.getVector());
|
||||
}
|
||||
}else {
|
||||
baseMapper.updateById(knowledgeInfo);
|
||||
}
|
||||
@@ -165,7 +177,7 @@ public class KnowledgeInfoServiceImpl implements IKnowledgeInfoService {
|
||||
check(knowledgeInfoList);
|
||||
// 删除向量库信息
|
||||
knowledgeInfoList.forEach(knowledgeInfoVo -> {
|
||||
embeddingService.removeByKid(String.valueOf(knowledgeInfoVo.getId()));
|
||||
vectorStoreService.removeByKid(String.valueOf(knowledgeInfoVo.getId()));
|
||||
});
|
||||
// 删除附件和知识片段
|
||||
fragmentMapper.deleteByMap(map);
|
||||
@@ -197,7 +209,7 @@ public class KnowledgeInfoServiceImpl implements IKnowledgeInfoService {
|
||||
List<KnowledgeFragment> knowledgeFragmentList = new ArrayList<>();
|
||||
if (CollUtil.isNotEmpty(chunkList)) {
|
||||
for (int i = 0; i < chunkList.size(); i++) {
|
||||
String fid = RandomUtil.randomString(16);
|
||||
String fid = RandomUtil.randomString(10);
|
||||
fids.add(fid);
|
||||
KnowledgeFragment knowledgeFragment = new KnowledgeFragment();
|
||||
knowledgeFragment.setKid(kid);
|
||||
@@ -211,15 +223,36 @@ public class KnowledgeInfoServiceImpl implements IKnowledgeInfoService {
|
||||
}
|
||||
fragmentMapper.insertBatch(knowledgeFragmentList);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.error("保存知识库信息失败!{}", e.getMessage());
|
||||
}
|
||||
knowledgeAttach.setContent(content);
|
||||
knowledgeAttach.setCreateTime(new Date());
|
||||
attachMapper.insert(knowledgeAttach);
|
||||
embeddingService.storeEmbeddings(chunkList,kid,docId,fids);
|
||||
|
||||
// 通过kid查询知识库信息
|
||||
KnowledgeInfoVo knowledgeInfoVo = baseMapper.selectVoOne(Wrappers.<KnowledgeInfo>lambdaQuery()
|
||||
.eq(KnowledgeInfo::getKid, kid));
|
||||
|
||||
// 通过向量模型查询模型信息
|
||||
ChatModelVo chatModelVo = chatModelService.selectModelByName(knowledgeInfoVo.getVectorModel());
|
||||
|
||||
StoreEmbeddingBo storeEmbeddingBo = new StoreEmbeddingBo();
|
||||
storeEmbeddingBo.setKid(kid);
|
||||
storeEmbeddingBo.setDocId(docId);
|
||||
storeEmbeddingBo.setFids(fids);
|
||||
storeEmbeddingBo.setChunkList(chunkList);
|
||||
storeEmbeddingBo.setModelName(knowledgeInfoVo.getVectorModel());
|
||||
storeEmbeddingBo.setApiKey(chatModelVo.getApiKey());
|
||||
storeEmbeddingBo.setBaseUrl(chatModelVo.getApiHost());
|
||||
vectorStoreService.storeEmbeddings(storeEmbeddingBo);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查用户是否有删除知识库权限
|
||||
*
|
||||
* @param knowledgeInfoList 知识库列表
|
||||
*/
|
||||
public void check(List<KnowledgeInfoVo> knowledgeInfoList){
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
for (KnowledgeInfoVo knowledgeInfoVo : knowledgeInfoList) {
|
||||
|
||||
@@ -1,107 +0,0 @@
|
||||
package org.ruoyi.chat.service.knowledge;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.chat.config.ChatConfig;
|
||||
import org.ruoyi.common.chat.entity.embeddings.Embedding;
|
||||
import org.ruoyi.common.chat.entity.embeddings.EmbeddingResponse;
|
||||
import org.ruoyi.common.chat.openai.OpenAiStreamClient;
|
||||
import org.ruoyi.domain.vo.ChatModelVo;
|
||||
import org.ruoyi.domain.vo.KnowledgeInfoVo;
|
||||
import org.ruoyi.service.IChatModelService;
|
||||
import org.ruoyi.service.IKnowledgeInfoService;
|
||||
import org.ruoyi.service.VectorizationService;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor
|
||||
public class OpenAiVectorizationImpl implements VectorizationService {
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private IKnowledgeInfoService knowledgeInfoService;
|
||||
|
||||
@Lazy
|
||||
@Resource
|
||||
private IChatModelService chatModelService;
|
||||
|
||||
@Getter
|
||||
private OpenAiStreamClient openAiStreamClient;
|
||||
|
||||
private final ChatConfig chatConfig;
|
||||
|
||||
@Override
|
||||
public List<List<Double>> batchVectorization(List<String> chunkList, String kid) {
|
||||
List<List<Double>> vectorList;
|
||||
// 获取知识库信息
|
||||
KnowledgeInfoVo knowledgeInfoVo = knowledgeInfoService.queryById(Long.valueOf(kid));
|
||||
if(knowledgeInfoVo == null){
|
||||
log.warn("知识库不存在:请查检ID {}",kid);
|
||||
vectorList=new ArrayList<>();
|
||||
vectorList.add(new ArrayList<>());
|
||||
return vectorList;
|
||||
}
|
||||
ChatModelVo chatModelVo = chatModelService.selectModelByName(knowledgeInfoVo.getVectorModel());
|
||||
String apiHost= chatModelVo.getApiHost();
|
||||
String apiKey= chatModelVo.getApiKey();
|
||||
openAiStreamClient = ChatConfig.createOpenAiStreamClient(apiHost,apiKey);
|
||||
Embedding embedding = buildEmbedding(chunkList, knowledgeInfoVo);
|
||||
EmbeddingResponse embeddings = openAiStreamClient.embeddings(embedding);
|
||||
// 处理 OpenAI 返回的嵌入数据
|
||||
vectorList = processOpenAiEmbeddings(embeddings);
|
||||
return vectorList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 Embedding 对象
|
||||
*/
|
||||
private Embedding buildEmbedding(List<String> chunkList, KnowledgeInfoVo knowledgeInfoVo) {
|
||||
return Embedding.builder()
|
||||
.input(chunkList)
|
||||
.model(knowledgeInfoVo.getVectorModel())
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 OpenAI 返回的嵌入数据
|
||||
*/
|
||||
private List<List<Double>> processOpenAiEmbeddings(EmbeddingResponse embeddings) {
|
||||
List<List<Double>> vectorList = new ArrayList<>();
|
||||
|
||||
embeddings.getData().forEach(data -> {
|
||||
List<BigDecimal> vector = data.getEmbedding();
|
||||
List<Double> doubleVector = convertToDoubleList(vector);
|
||||
vectorList.add(doubleVector);
|
||||
});
|
||||
|
||||
return vectorList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 BigDecimal 转换为 Double 列表
|
||||
*/
|
||||
private List<Double> convertToDoubleList(List<BigDecimal> vector) {
|
||||
return vector.stream()
|
||||
.map(BigDecimal::doubleValue)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Double> singleVectorization(String chunk, String kid) {
|
||||
List<String> chunkList = new ArrayList<>();
|
||||
chunkList.add(chunk);
|
||||
List<List<Double>> vectorList = batchVectorization(chunkList, kid);
|
||||
return vectorList.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package org.ruoyi.chat.service.knowledge;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ruoyi.chat.factory.VectorizationFactory;
|
||||
import org.ruoyi.service.VectorizationService;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@Primary
|
||||
@AllArgsConstructor
|
||||
public class VectorizationWrapper implements VectorizationService {
|
||||
|
||||
private final VectorizationFactory vectorizationFactory;
|
||||
@Override
|
||||
public List<List<Double>> batchVectorization(List<String> chunkList, String kid) {
|
||||
VectorizationService embedding = vectorizationFactory.getEmbedding(kid);
|
||||
return embedding.batchVectorization(chunkList, kid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Double> singleVectorization(String chunk, String kid) {
|
||||
VectorizationService embedding = vectorizationFactory.getEmbedding(kid);
|
||||
return embedding.singleVectorization(chunk, kid);
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author https://www.wdbyte.com
|
||||
* 企业微信应用
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
|
||||
@@ -13,7 +13,10 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author https://www.wdbyte.com
|
||||
* 微信公众号登录
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
|
||||
@@ -13,7 +13,10 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* 微信公众号登录校验
|
||||
*
|
||||
* @author ageerle
|
||||
* @date 2025-05-03
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/wx/cp")
|
||||
|
||||
42
script/sql/update/20250505.sql
Normal file
42
script/sql/update/20250505.sql
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
Navicat MySQL Data Transfer
|
||||
|
||||
Source Server : 129.211.24.7
|
||||
Source Server Type : MySQL
|
||||
Source Server Version : 50744
|
||||
Source Host : 129.211.24.7:3306
|
||||
Source Schema : ry-vue
|
||||
|
||||
Target Server Type : MySQL
|
||||
Target Server Version : 50744
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 05/05/2025 15:01:08
|
||||
*/
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for chat_session
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `chat_session`;
|
||||
CREATE TABLE `chat_session` (
|
||||
`id` bigint(20) NOT NULL COMMENT '主键',
|
||||
`user_id` bigint(20) NULL DEFAULT NULL COMMENT '用户id',
|
||||
`session_title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '会话标题',
|
||||
`session_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '会话内容',
|
||||
`create_dept` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '部门',
|
||||
`create_by` bigint(20) NULL DEFAULT NULL COMMENT '创建者',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` bigint(20) NULL DEFAULT NULL COMMENT '更新者',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
|
||||
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '会话管理' ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
|
||||
ALTER TABLE `chat_message`
|
||||
ADD COLUMN `session_id` bigint(20) NULL COMMENT '会话id' AFTER `id`;
|
||||
|
||||
Reference in New Issue
Block a user