调整 ai模块
This commit is contained in:
@@ -70,3 +70,34 @@ CREATE TABLE `tz_ai_sample_tag_group_relate`
|
||||
`sample_tag_group_id` bigint NOT NULL COMMENT '样本标签分组id',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB COMMENT = '样本标签-分组关联表';
|
||||
|
||||
|
||||
DROP TABLE IF EXISTS `tz_ai_dialog`;
|
||||
CREATE TABLE `tz_ai_dialog`
|
||||
(
|
||||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
|
||||
`title` varchar(128) NULL DEFAULT '' COMMENT '对话标题',
|
||||
`user_id` bigint NOT NULL COMMENT '用户id',
|
||||
`dialog_status` int(8) NULL COMMENT '对话状态(1active, 2archived, 3deleted)',
|
||||
`creator` varchar(64) NULL DEFAULT '' COMMENT '创建者',
|
||||
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`updater` varchar(64) NULL DEFAULT '' COMMENT '更新者',
|
||||
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
|
||||
`tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB COMMENT = 'ai-对话表';
|
||||
|
||||
DROP TABLE IF EXISTS `tz_ai_dialog_message`;
|
||||
CREATE TABLE `tz_ai_dialog_message`
|
||||
(
|
||||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
|
||||
`dialog_id` bigint NOT NULL COMMENT '对话id',
|
||||
`content_text` varchar(64) NULL DEFAULT '' COMMENT '内容',
|
||||
`content_type` int(8) NULL COMMENT '文本类型(1text,2image,3file,4audio)',
|
||||
`message_order` int(8) NULL COMMENT '对话中的顺序',
|
||||
`message_status` int(8) NULL COMMENT '消息状态 1正常 0删除',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB COMMENT = 'ai-对话消息表';
|
||||
|
||||
|
||||
|
||||
@@ -68,11 +68,21 @@ public class AiSampleController {
|
||||
@Parameter(name = "id", description = "编号", required = true)
|
||||
// @PreAuthorize("@ss.hasPermission('ai:sample:delete')")
|
||||
@PermitAll
|
||||
public CommonResult<Boolean> deleteAiSample(@RequestParam("id") Long id) {
|
||||
aiSampleService.deleteAiSample(id);
|
||||
public CommonResult<Boolean> deleteAiSample(@RequestParam("id") String ids) {
|
||||
aiSampleService.deleteAiSample(ids);
|
||||
return success(true);
|
||||
}
|
||||
|
||||
@GetMapping("/get")
|
||||
@Operation(summary = "获得样本库")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
// @PreAuthorize("@ss.hasPermission('ai:sample:query')")
|
||||
@PermitAll
|
||||
public CommonResult<AiSampleRespVO> getAiSample(@RequestParam("id") Long id) {
|
||||
AiSampleDO aiSample = aiSampleService.getAiSample(id);
|
||||
return success(BeanUtils.toBean(aiSample, AiSampleRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得样本库分页")
|
||||
// @PreAuthorize("@ss.hasPermission('ai:sample:query')")
|
||||
|
||||
@@ -5,7 +5,7 @@ import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Schema(description = "管理后台 - 样本库新增/修改 Request VO")
|
||||
@Schema(description = "管理后台 - 关联 Request VO")
|
||||
@Data
|
||||
public class AiSampleRelateTagVO {
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.tashow.cloud.ai.controller.app.dialog;
|
||||
|
||||
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.DialogResp;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.TranslateReqVo;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.TranslateRespVo;
|
||||
import com.tashow.cloud.ai.service.dialog.AiDialogMessageService;
|
||||
import com.tashow.cloud.ai.service.dialog.AiDialogService;
|
||||
import com.tashow.cloud.common.pojo.CommonResult;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.tashow.cloud.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/ai/dialog")
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class DialogController {
|
||||
|
||||
List<String> message = List.of("渴了", "饿了", "想睡觉", "想出去玩", "想溜达", "情绪低落", "很开心", "很伤心", "想哭");
|
||||
|
||||
@Resource
|
||||
private AiDialogService aiDialogService;
|
||||
@Resource
|
||||
private AiDialogMessageService messageService;
|
||||
|
||||
/**
|
||||
* 获取对话消息列表
|
||||
*/
|
||||
@PostMapping("/getDialog")
|
||||
@PermitAll
|
||||
public CommonResult<DialogResp> msList() {
|
||||
//获取当前登录用户
|
||||
Long userId = 1l;
|
||||
return success(aiDialogService.getDialog(userId));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*/
|
||||
@PostMapping("/translate")
|
||||
@PermitAll
|
||||
public CommonResult<TranslateRespVo> translate(@Validated TranslateReqVo fileReqVo) {
|
||||
return success(aiDialogService.translate(fileReqVo));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.tashow.cloud.ai.controller.app.dialog.vo;
|
||||
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogMessageDO;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Schema(description = "api - 对话 Response VO")
|
||||
@Data
|
||||
public class DialogResp {
|
||||
|
||||
@Schema(description = "对话id")
|
||||
private Long dialogId;
|
||||
@Schema(description = "对话标题")
|
||||
private String title;
|
||||
@Schema(description = "对话状态")
|
||||
private String dialogStatus;
|
||||
@Schema(description = "对话消息内容")
|
||||
private List<AiDialogMessageDO> messages;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.tashow.cloud.ai.controller.app.dialog.vo;
|
||||
|
||||
public class Pets {
|
||||
|
||||
private Long petId;
|
||||
private String petName;
|
||||
private String petAvatar;
|
||||
private String petType;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.tashow.cloud.ai.controller.app.dialog.vo;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 翻译接口请求vo
|
||||
*/
|
||||
@Data
|
||||
public class TranslateReqVo {
|
||||
|
||||
//对话id
|
||||
private Long dialogId;
|
||||
|
||||
/** 文件附件 */
|
||||
private MultipartFile file;
|
||||
|
||||
//文件时长
|
||||
private Integer filePlayTime;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.tashow.cloud.ai.controller.app.dialog.vo;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 翻译接口结果vo
|
||||
*/
|
||||
@Data
|
||||
public class TranslateRespVo {
|
||||
|
||||
//消息id
|
||||
private Long msgId;
|
||||
//对话id
|
||||
private Long dialogId;
|
||||
//消息顺序
|
||||
private Integer messageOrder;
|
||||
//消息类型(1text,2file)
|
||||
private Integer contentType;
|
||||
//消息内容 若消息类型是file 则为文件地址
|
||||
private String contentText;
|
||||
//文件时长
|
||||
private Integer filePlayTime;
|
||||
//宠物类型
|
||||
private String petType;
|
||||
//宠物名称
|
||||
private String petName;
|
||||
//宠物头像
|
||||
private String petAvatar;
|
||||
//宠物id
|
||||
private Long petId;
|
||||
//翻译状态
|
||||
private Integer translateStatus;
|
||||
//翻译结果
|
||||
private String translateResult;
|
||||
//发送时间
|
||||
private String createTime;
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.tashow.cloud.ai.controller.app.translate;
|
||||
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.tashow.cloud.common.pojo.CommonResult;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.tashow.cloud.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/ai/sample")
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class TranslateController {
|
||||
|
||||
List<String> message = List.of("渴了", "饿了", "想睡觉", "想出去玩", "想溜达", "情绪低落", "很开心", "很伤心", "想哭");
|
||||
|
||||
/**
|
||||
* 翻译
|
||||
*/
|
||||
@PostMapping("/translate")
|
||||
@PermitAll
|
||||
public CommonResult<String> translate(MultipartFile file) {
|
||||
// log.info("file:{}",file.getName());
|
||||
return success(message.get(RandomUtil.randomInt(0, message.size())));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.tashow.cloud.ai.dal.dataobject.dialog;
|
||||
|
||||
import lombok.*;
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.tashow.cloud.mybatis.mybatis.core.dataobject.BaseDO;
|
||||
|
||||
/**
|
||||
* ai-对话 DO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@TableName("tz_ai_dialog")
|
||||
@KeySequence("tz_ai_dialog_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AiDialogDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 对话标题
|
||||
*/
|
||||
private String title;
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
private Long userId;
|
||||
/**
|
||||
* 对话状态(1active, 2archived, 3deleted)
|
||||
*/
|
||||
private Integer dialogStatus;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.tashow.cloud.ai.dal.dataobject.dialog;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.KeySequence;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.tashow.cloud.mybatis.mybatis.core.dataobject.BaseDO;
|
||||
import lombok.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* ai-对话消息 DO
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@TableName("tz_ai_dialog_message")
|
||||
@KeySequence("tz_ai_dialog_message_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@ToString(callSuper = true)
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class AiDialogMessageDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 对话id
|
||||
*/
|
||||
private Long dialogId;
|
||||
/**
|
||||
* 内容
|
||||
*/
|
||||
private String contentText;
|
||||
/**
|
||||
* 文本类型(1text,2image,3file,4audio)
|
||||
*/
|
||||
private Integer contentType;
|
||||
/**
|
||||
* 对话中的顺序
|
||||
*/
|
||||
private Integer messageOrder;
|
||||
/**
|
||||
* 消息状态 1正常 0删除
|
||||
*/
|
||||
private Integer messageStatus;
|
||||
/**
|
||||
* 宠物id
|
||||
*/
|
||||
private Long petId;
|
||||
/**
|
||||
* 宠物名称
|
||||
*/
|
||||
private String petName;
|
||||
/**
|
||||
* 宠物头像
|
||||
*/
|
||||
private String petAvatar;
|
||||
/**
|
||||
* 翻译结果
|
||||
*/
|
||||
private String transResult;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.tashow.cloud.ai.dal.mysql.dialog;
|
||||
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogDO;
|
||||
import com.tashow.cloud.mybatis.mybatis.core.mapper.BaseMapperX;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* ai-对话 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface AiDialogMapper extends BaseMapperX<AiDialogDO> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.tashow.cloud.ai.dal.mysql.dialog;
|
||||
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogMessageDO;
|
||||
import com.tashow.cloud.mybatis.mybatis.core.mapper.BaseMapperX;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* ai-对话消息 Mapper
|
||||
*
|
||||
* @author 芋道源码
|
||||
*/
|
||||
@Mapper
|
||||
public interface AiDialogMessageMapper extends BaseMapperX<AiDialogMessageDO> {
|
||||
|
||||
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public interface AiSampleService {
|
||||
*
|
||||
* @param id 编号
|
||||
*/
|
||||
void deleteAiSample(Long id);
|
||||
void deleteAiSample(String id);
|
||||
|
||||
/**
|
||||
* 删除样本库
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.tashow.cloud.ai.service.aisample;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.tashow.cloud.ai.controller.admin.aisample.vo.*;
|
||||
import com.tashow.cloud.ai.dal.dataobject.aisample.AiSampleDO;
|
||||
@@ -15,11 +16,13 @@ import com.tashow.cloud.common.util.object.BeanUtils;
|
||||
import com.tashow.cloud.infraapi.api.file.FileApi;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -39,6 +42,8 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
private AiSampleTagRelateMapper aiSampleTagRelateMapper;
|
||||
@Resource
|
||||
private FileApi fileApi;
|
||||
@Value("file-server")
|
||||
private String fileServer;
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
@@ -48,7 +53,7 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
/* 调用文件上传服务*/
|
||||
for (MultipartFile file : uploadReqVO.getFiles()) {
|
||||
//返回上传结果
|
||||
String file1 ="http://192.168.1.231:48080"+ fileApi.createFile(file.getBytes());
|
||||
String file1 = fileServer + fileApi.createFile(file.getBytes());
|
||||
//保存样本信息
|
||||
AiSampleDO aiSampleDO = new AiSampleDO();
|
||||
aiSampleDO.setSampleFilePath(file1);
|
||||
@@ -73,7 +78,7 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
|
||||
@Override
|
||||
public void updateAiSamples(List<AiSampleSaveReqVO> updateReqVO) {
|
||||
aiSampleMapper.updateBatch( BeanUtils.toBean(updateReqVO, AiSampleDO.class));
|
||||
aiSampleMapper.updateBatch(BeanUtils.toBean(updateReqVO, AiSampleDO.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,37 +86,48 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
List<AiSampleTagRelateDO> tagRelateDOS = new ArrayList<>();
|
||||
for (Long sampleId : relateTagVO.getSampleIds()) {
|
||||
for (Long tagId : relateTagVO.getTagId()) {
|
||||
AiSampleTagRelateDO relateDO = new AiSampleTagRelateDO();
|
||||
relateDO.setSampleId(sampleId);
|
||||
relateDO.setSampleTagId(tagId);
|
||||
tagRelateDOS.add(relateDO);
|
||||
AiSampleTagRelateDO relateDO = aiSampleTagRelateMapper.selectOne(
|
||||
new LambdaQueryWrapper<AiSampleTagRelateDO>()
|
||||
.eq(AiSampleTagRelateDO::getSampleId, sampleId)
|
||||
.eq(AiSampleTagRelateDO::getSampleTagId, tagId) );
|
||||
if (relateDO== null){
|
||||
relateDO = new AiSampleTagRelateDO();
|
||||
relateDO.setSampleId(sampleId);
|
||||
relateDO.setSampleTagId(tagId);
|
||||
tagRelateDOS.add(relateDO);
|
||||
}
|
||||
}
|
||||
}
|
||||
aiSampleTagRelateMapper.insertBatch(tagRelateDOS);
|
||||
if (!tagRelateDOS.isEmpty()){
|
||||
aiSampleTagRelateMapper.insertBatch(tagRelateDOS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteRelate(AiSampleRelateTagVO relateTagVO) {
|
||||
List<Long> tagRelateIds = new ArrayList<>();
|
||||
for (Long sampleId : relateTagVO.getSampleIds()) {
|
||||
for (Long l : relateTagVO.getTagId()) {
|
||||
for (Long tagId : relateTagVO.getTagId()) {
|
||||
AiSampleTagRelateDO relateDO = aiSampleTagRelateMapper.selectOne(
|
||||
new LambdaQueryWrapper<AiSampleTagRelateDO>()
|
||||
.eq(AiSampleTagRelateDO::getSampleId, sampleId)
|
||||
.eq(AiSampleTagRelateDO::getSampleTagId, l)
|
||||
.eq(AiSampleTagRelateDO::getSampleTagId, tagId)
|
||||
);
|
||||
tagRelateIds.add(relateDO.getId());
|
||||
if (relateDO != null) {
|
||||
tagRelateIds.add(relateDO.getId());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (!tagRelateIds.isEmpty()) {
|
||||
aiSampleTagRelateMapper.deleteBatchIds(tagRelateIds);
|
||||
}
|
||||
aiSampleTagRelateMapper.deleteBatchIds(tagRelateIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAiSample(Long id) {
|
||||
// 校验存在
|
||||
validateAiSampleExists(id);
|
||||
public void deleteAiSample(String ids) {
|
||||
// 删除
|
||||
aiSampleMapper.deleteById(id);
|
||||
aiSampleMapper.deleteByIds(Arrays.asList(ids.split(StrUtil.COMMA)));
|
||||
}
|
||||
|
||||
private void validateAiSampleExists(Long id) {
|
||||
@@ -123,11 +139,11 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
@Override
|
||||
public AiSampleDO getAiSample(Long id) {
|
||||
AiSampleDO aiSampleDO = aiSampleMapper.selectById(id);
|
||||
aiSampleDO.setSampleFilePath("http://192.168.1.231:48080"+aiSampleDO.getSampleFilePath());
|
||||
aiSampleDO.setSampleFilePath(aiSampleDO.getSampleFilePath());
|
||||
//先获取关联的标签id
|
||||
List<AiSampleTagRelateDO> tagRelateDOS = aiSampleTagRelateMapper.selectList(new LambdaQueryWrapper<AiSampleTagRelateDO>().eq(AiSampleTagRelateDO::getSampleId, id));
|
||||
List<Long> tagIds = tagRelateDOS.stream().map(AiSampleTagRelateDO::getSampleTagId).toList();
|
||||
aiSampleDO.setTags(aiSampleTagMapper.selectList(new LambdaQueryWrapper<AiSampleTagDO>().in(AiSampleTagDO::getId,tagIds)));
|
||||
aiSampleDO.setTags(aiSampleTagMapper.selectList(new LambdaQueryWrapper<AiSampleTagDO>().in(AiSampleTagDO::getId, tagIds)));
|
||||
return aiSampleDO;
|
||||
}
|
||||
|
||||
@@ -137,13 +153,13 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
//根据样本id获取关联的标签id
|
||||
List<Long> sampleIds = aiSampleDOPageResult.getList().stream().map(AiSampleDO::getId).toList();
|
||||
List<AiSampleTagRelateDO> tagRelateDOS = aiSampleTagRelateMapper.selectList(
|
||||
new LambdaQueryWrapper<AiSampleTagRelateDO>()
|
||||
.in(!sampleIds.isEmpty(),AiSampleTagRelateDO::getSampleId, sampleIds));
|
||||
new LambdaQueryWrapper<AiSampleTagRelateDO>()
|
||||
.in(!sampleIds.isEmpty(), AiSampleTagRelateDO::getSampleId, sampleIds));
|
||||
List<Long> tagIds = tagRelateDOS.stream().map(AiSampleTagRelateDO::getSampleTagId).toList();
|
||||
//获取标签信息
|
||||
List<AiSampleTagDO> aiSampleTagDOS = aiSampleTagMapper.selectList(
|
||||
new LambdaQueryWrapper<AiSampleTagDO>()
|
||||
.in(!tagIds.isEmpty(),AiSampleTagDO::getId, tagIds));
|
||||
.in(!tagIds.isEmpty(), AiSampleTagDO::getId, tagIds));
|
||||
|
||||
//封装标签信息
|
||||
for (AiSampleDO aiSampleDO : aiSampleDOPageResult.getList()) {
|
||||
@@ -153,7 +169,7 @@ public class AiSampleServiceImpl implements AiSampleService {
|
||||
Object[] tagsId = list.stream().map(AiSampleTagRelateDO::getSampleTagId).toArray();
|
||||
List<AiSampleTagDO> list1 = aiSampleTagDOS.stream().filter(a -> ArrayUtil.containsAny(tagsId, a.getId())).toList();
|
||||
aiSampleDO.setTags(list1);
|
||||
aiSampleDO.setSampleFilePath("http://192.168.1.231:48080"+aiSampleDO.getSampleFilePath());
|
||||
aiSampleDO.setSampleFilePath(aiSampleDO.getSampleFilePath());
|
||||
}
|
||||
return aiSampleDOPageResult;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.tashow.cloud.ai.service.dialog;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogMessageDO;
|
||||
|
||||
/**
|
||||
* ai-对话消息 Service 接口
|
||||
*
|
||||
* @author lwq
|
||||
*/
|
||||
public interface AiDialogMessageService extends IService<AiDialogMessageDO> {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.tashow.cloud.ai.service.dialog;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogMessageDO;
|
||||
import com.tashow.cloud.ai.dal.mysql.dialog.AiDialogMessageMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
|
||||
/**
|
||||
* ai-对话消息 Service 实现类
|
||||
*
|
||||
* @author lwq
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
public class AiDialogMessageServiceImpl extends ServiceImpl<AiDialogMessageMapper, AiDialogMessageDO> implements AiDialogMessageService {
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.tashow.cloud.ai.service.dialog;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.DialogResp;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.TranslateReqVo;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.TranslateRespVo;
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogDO;
|
||||
|
||||
/**
|
||||
* ai-对话 Service 接口
|
||||
*
|
||||
* @author lwq
|
||||
*/
|
||||
public interface AiDialogService extends IService<AiDialogDO> {
|
||||
|
||||
|
||||
DialogResp getDialog(Long userId);
|
||||
|
||||
TranslateRespVo translate(TranslateReqVo fileReqVo);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.tashow.cloud.ai.service.dialog;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.DialogResp;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.TranslateReqVo;
|
||||
import com.tashow.cloud.ai.controller.app.dialog.vo.TranslateRespVo;
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogDO;
|
||||
import com.tashow.cloud.ai.dal.dataobject.dialog.AiDialogMessageDO;
|
||||
import com.tashow.cloud.ai.dal.mysql.dialog.AiDialogMapper;
|
||||
import com.tashow.cloud.ai.dal.mysql.dialog.AiDialogMessageMapper;
|
||||
import com.tashow.cloud.common.util.object.BeanUtils;
|
||||
import com.tashow.cloud.infraapi.api.file.FileApi;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ai-对话 Service 实现类
|
||||
*
|
||||
* @author lwq
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO> implements AiDialogService {
|
||||
|
||||
@Resource
|
||||
private AiDialogMessageMapper aiDialogMessageMapper;
|
||||
@Value("translate-server")
|
||||
private String translateServer;
|
||||
@Value("file-server")
|
||||
private String fileServer;
|
||||
@Resource
|
||||
private FileApi fileApi;
|
||||
|
||||
|
||||
@Override
|
||||
public DialogResp getDialog(Long userId) {
|
||||
AiDialogDO aiDialogDO = this.getOne(new LambdaQueryWrapper<AiDialogDO>().eq(AiDialogDO::getUserId, userId));
|
||||
if (aiDialogDO == null) {
|
||||
aiDialogDO = new AiDialogDO();
|
||||
aiDialogDO.setDialogStatus(1);
|
||||
aiDialogDO.setTitle("xxx");
|
||||
aiDialogDO.setUserId(userId);
|
||||
this.save(aiDialogDO);
|
||||
}
|
||||
List<AiDialogMessageDO> messageDOS = aiDialogMessageMapper.selectList(new LambdaQueryWrapper<AiDialogMessageDO>().eq(AiDialogMessageDO::getDialogId, aiDialogDO.getId()));
|
||||
DialogResp resp = new DialogResp();
|
||||
resp.setDialogId(aiDialogDO.getId());
|
||||
resp.setTitle(aiDialogDO.getTitle());
|
||||
resp.setMessages(messageDOS);
|
||||
return resp;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@SneakyThrows
|
||||
public TranslateRespVo translate(TranslateReqVo fileReqVo) {
|
||||
//翻译结果
|
||||
TranslateRespVo result = translate(fileReqVo.getFile());
|
||||
//创建消息 持久化消息
|
||||
result.setDialogId( fileReqVo.getDialogId());
|
||||
result.setFilePlayTime( fileReqVo.getFilePlayTime());
|
||||
//上传文件获取文件地址
|
||||
String fileUrl = fileServer + fileApi.createFile(fileReqVo.getFile().getBytes());
|
||||
result.setContentText(fileUrl);
|
||||
result.setContentType(2);
|
||||
//宠物档案 todo
|
||||
result.setPetId(1l);
|
||||
result.setPetName("猫猫翻译");
|
||||
result.setPetAvatar("https://img1.baidu.com/it/u=1224902049,3440357835&fm=253&app=138&f=JPEG?w=801&h=800");
|
||||
|
||||
//获取当前最后的排序
|
||||
AiDialogMessageDO aiDialogMessageDO = aiDialogMessageMapper.selectOne(new LambdaQueryWrapper<AiDialogMessageDO>()
|
||||
.eq(AiDialogMessageDO::getDialogId, fileReqVo.getDialogId())
|
||||
.orderByDesc(AiDialogMessageDO::getMessageOrder)
|
||||
.last("limit 1")
|
||||
);
|
||||
result.setMessageOrder(aiDialogMessageDO.getMessageOrder()+1);
|
||||
|
||||
AiDialogMessageDO dialogMessageDO = BeanUtils.toBean(result, AiDialogMessageDO.class);
|
||||
aiDialogMessageMapper.insert(dialogMessageDO);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
private TranslateRespVo translate(MultipartFile file) {
|
||||
//调用大模型接口
|
||||
String result = "";
|
||||
try {
|
||||
result = HttpRequest.post(translateServer).form("audio_data", file).timeout(20000) //20秒超时时间
|
||||
.execute().body();
|
||||
} catch (Exception e) {
|
||||
log.error("调用大模型翻译出错", e);
|
||||
}
|
||||
//
|
||||
// {
|
||||
// "species_labels": "cat",
|
||||
// "is_species_sound": true,
|
||||
// "confidence": 0.77200821734893,
|
||||
// "intent_result": {
|
||||
// "winner": "cat_舒服",
|
||||
// "confidence": 69.55630087055536,
|
||||
// "probabilities": {
|
||||
// "cat_等待喂食": 0.0,
|
||||
// "cat_舒服": 1.0
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//数据解析
|
||||
JSONObject translateResult = JSON.parseObject(result);
|
||||
//标签 如 cat dog
|
||||
String speciesLabels = translateResult.getString("species_labels");
|
||||
//解析翻译结果
|
||||
JSONObject probabilities = translateResult.getJSONObject("intent_result")
|
||||
.getJSONObject("probabilities");
|
||||
|
||||
String resultKey = probabilities.entrySet().stream()
|
||||
.filter(entry -> entry.getValue() instanceof Number)
|
||||
.max(Comparator.comparingDouble(entry -> ((Number) entry.getValue()).doubleValue()))
|
||||
.map(Map.Entry::getKey)
|
||||
.orElse(null);
|
||||
String sourceResult = probabilities.getString(resultKey);
|
||||
String lastResult = sourceResult.split(StrUtil.UNDERLINE)[1];
|
||||
|
||||
//返回结果
|
||||
TranslateRespVo translateRespVo = new TranslateRespVo();
|
||||
translateRespVo.setPetType(speciesLabels);
|
||||
translateRespVo.setTranslateStatus(1);
|
||||
translateRespVo.setTranslateResult(lastResult);
|
||||
return translateRespVo;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user