调整 ai模块

This commit is contained in:
2025-10-14 15:19:49 +08:00
parent 077fb92772
commit 22fc268fcb
14 changed files with 188 additions and 92 deletions

View File

@@ -5,11 +5,6 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static com.tashow.cloud.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - 样本库分页 Request VO")
@Data
@@ -17,8 +12,8 @@ import static com.tashow.cloud.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_
@ToString(callSuper = true)
public class AiSamplePageReqVO extends PageParam {
@Schema(description = "标签id", example = "25839")
private Long tagId;
@Schema(description = "标签ids", example = "25839")
private String tagIds;
@Schema(description = "样本名称", example = "张三")
private String sampleName;

View File

@@ -12,7 +12,9 @@ import lombok.ToString;
@ToString(callSuper = true)
public class AiSampleTagPageReqVO extends PageParam {
@Schema(description = "分组id", example = "张三")
@Schema(description = "分组id")
private Long groupId;
@Schema(description = "标签名称", example = "张三")
private String tagName;
}

View File

@@ -4,13 +4,13 @@ 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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -32,17 +32,16 @@ public class DialogController {
@Resource
private AiDialogService aiDialogService;
@Resource
private AiDialogMessageService messageService;
/**
* 获取对话消息列表
*/
@PostMapping("/getDialog")
@GetMapping("/getDialog")
@PermitAll
public CommonResult<DialogResp> msList() {
//获取当前登录用户
Long userId = 1l;
Long userId = 1L;
return success(aiDialogService.getDialog(userId));
}

View File

@@ -0,0 +1,72 @@
package com.tashow.cloud.ai.controller.app.dialog.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.Date;
import static com.tashow.cloud.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
/**
* ai-对话消息 DO
*
* @author 芋道源码
*/
@Schema(description = "api - 对话 Response VO")
@Data
public class AiDialogMessageRespVo {
private Long id;
/**
* 对话id
*/
private Long dialogId;
/**
* 内容
*/
private String contentText;
/**
* 文件时长
*/
private Long contentDuration;
/**
* 文本类型1text2image3file4audio
*/
private Integer contentType;
/**
* 对话中的顺序
*/
private Integer messageOrder;
/**
* 消息状态 1正常 0删除
*/
private Integer messageStatus;
/**
* 宠物id
*/
private Long petId;
/**
* 宠物名称
*/
private String petName;
/**
* 宠物头像
*/
private String petAvatar;
/**
* 宠物类型
*/
private String petType;
/**
* 翻译结果
*/
private String transResult;
/**
* 创建时间
*/
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private Date createTime;
}

View File

@@ -1,6 +1,5 @@
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;
@@ -18,5 +17,5 @@ public class DialogResp {
@Schema(description = "对话状态")
private String dialogStatus;
@Schema(description = "对话消息内容")
private List<AiDialogMessageDO> messages;
private List<AiDialogMessageRespVo> messages;
}

View File

@@ -1,9 +0,0 @@
package com.tashow.cloud.ai.controller.app.dialog.vo;
public class Pets {
private Long petId;
private String petName;
private String petAvatar;
private String petType;
}

View File

@@ -11,12 +11,14 @@ public class TranslateReqVo {
//对话id
private Long dialogId;
//消息id
private Long msgId;
/** 文件附件 */
private MultipartFile file;
//文件时长
private Integer filePlayTime;
private Long contentDuration;
}

View File

@@ -1,7 +1,11 @@
package com.tashow.cloud.ai.controller.app.dialog.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import static com.tashow.cloud.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
/**
* 翻译接口结果vo
@@ -10,7 +14,7 @@ import lombok.Data;
public class TranslateRespVo {
//消息id
private Long msgId;
private Long id;
//对话id
private Long dialogId;
//消息顺序
@@ -19,8 +23,6 @@ public class TranslateRespVo {
private Integer contentType;
//消息内容 若消息类型是file 则为文件地址
private String contentText;
//文件时长
private Integer filePlayTime;
//宠物类型
private String petType;
//宠物名称
@@ -30,10 +32,14 @@ public class TranslateRespVo {
//宠物id
private Long petId;
//翻译状态
private Integer translateStatus;
private Integer transStatus;
//翻译结果
private String translateResult;
private String transResult;
//文件时长
private Long contentDuration;
//发送时间
@JsonFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private String createTime;
}

View File

@@ -1,9 +1,6 @@
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 com.baomidou.mybatisplus.annotation.*;
import lombok.*;
import java.time.LocalDateTime;
@@ -16,12 +13,11 @@ import java.time.LocalDateTime;
@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 {
public class AiDialogMessageDO {
/**
* 主键
@@ -36,6 +32,11 @@ public class AiDialogMessageDO extends BaseDO {
* 内容
*/
private String contentText;
/**
* 文件时长
*/
private Long contentDuration;
/**
* 文本类型1text2image3file4audio
*/
@@ -60,13 +61,26 @@ public class AiDialogMessageDO extends BaseDO {
* 宠物头像
*/
private String petAvatar;
/**
* 宠物类型
*/
private String petType;
/**
* 原始结果
*/
private String sourceResult;
/**
* 翻译结果
*/
private String transResult;
/**
* 翻译结果
*/
private Integer transStatus;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
}

View File

@@ -23,14 +23,19 @@ public interface AiSampleTagMapper extends BaseMapperX<AiSampleTagDO> {
.orderByDesc(AiSampleTagDO::getId));
}
@Select("<script>" +
"SELECT t.id,t.tag_name tagName FROM tz_ai_sample_tag t INNER JOIN tz_ai_sample_tag_group_relate r ON t.id = r.sample_tag_id " +
"<where>" +
"<if test='pageReqVO.groupId != null'>" +
"AND r.sample_tag_group_id = #{pageReqVO.groupId}" +
"</if>" +
"</where>" +
"order by id desc" +
@Select("<script>\n" +
"SELECT t.id, t.tag_name tagName \n" +
"FROM tz_ai_sample_tag t \n" +
"INNER JOIN tz_ai_sample_tag_group_relate r ON t.id = r.sample_tag_id \n" +
"<where>\n" +
" <if test=\"pageReqVO.groupId != null\">\n" +
" AND r.sample_tag_group_id = #{pageReqVO.groupId}\n" +
" </if>\n" +
" <if test=\"pageReqVO.tagName != null and pageReqVO.tagName != ''\">\n" +
" AND t.tag_name LIKE '%' || #{pageReqVO.tagName} || '%'\n" +
" </if>\n" +
"</where>\n" +
"ORDER BY t.id DESC\n" +
"</script>")
IPage<AiSampleTagDO> getAiSampleTagPage(Page<AiSampleTagDO> page, AiSampleTagPageReqVO pageReqVO);

View File

@@ -2,10 +2,12 @@ package com.tashow.cloud.ai.service.dialog;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
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.AiDialogMessageRespVo;
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;
@@ -21,8 +23,8 @@ 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.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
@@ -39,9 +41,9 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
@Resource
private AiDialogMessageMapper aiDialogMessageMapper;
@Value("translate-server")
@Value("${translate-server}")
private String translateServer;
@Value("file-server")
@Value("${file-server}")
private String fileServer;
@Resource
private FileApi fileApi;
@@ -61,7 +63,7 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
DialogResp resp = new DialogResp();
resp.setDialogId(aiDialogDO.getId());
resp.setTitle(aiDialogDO.getTitle());
resp.setMessages(messageDOS);
resp.setMessages(BeanUtils.toBean(messageDOS, AiDialogMessageRespVo.class));
return resp;
}
@@ -69,19 +71,26 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
@Override
@SneakyThrows
public TranslateRespVo translate(TranslateReqVo fileReqVo) {
//翻译结果
TranslateRespVo result = translate(fileReqVo.getFile());
//创建消息 持久化消息
result.setDialogId( fileReqVo.getDialogId());
result.setFilePlayTime( fileReqVo.getFilePlayTime());
AiDialogMessageDO messageDO = aiDialogMessageMapper.selectById(fileReqVo.getMsgId());
if (messageDO == null) {
messageDO = new AiDialogMessageDO();
}
//上传文件获取文件地址
String fileName = fileReqVo.getFile().getOriginalFilename();
String fileUrl = fileServer + fileApi.createFile(fileReqVo.getFile().getBytes());
result.setContentText(fileUrl);
result.setContentType(2);
//翻译结果
translate(messageDO,fileUrl, fileName);
//创建消息 持久化消息
messageDO.setDialogId(fileReqVo.getDialogId());
messageDO.setContentDuration(fileReqVo.getContentDuration());
messageDO.setContentText(fileUrl);
messageDO.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");
messageDO.setPetId(1l);
messageDO.setPetName("猫猫翻译");
messageDO.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>()
@@ -89,58 +98,59 @@ public class AiDialogServiceImpl extends ServiceImpl<AiDialogMapper, AiDialogDO>
.orderByDesc(AiDialogMessageDO::getMessageOrder)
.last("limit 1")
);
result.setMessageOrder(aiDialogMessageDO.getMessageOrder()+1);
AiDialogMessageDO dialogMessageDO = BeanUtils.toBean(result, AiDialogMessageDO.class);
aiDialogMessageMapper.insert(dialogMessageDO);
return result;
messageDO.setMessageOrder(aiDialogMessageDO == null ? 1 : aiDialogMessageDO.getMessageOrder() + 1);
messageDO.setCreateTime(LocalDateTime.now());
int i = messageDO.getId()==null? aiDialogMessageMapper.insert(messageDO):aiDialogMessageMapper.updateById(messageDO);
TranslateRespVo bean = BeanUtils.toBean(messageDO, TranslateRespVo.class);
bean.setId(messageDO.getId());
return bean;
}
private TranslateRespVo translate(MultipartFile file) {
private AiDialogMessageDO translate(AiDialogMessageDO messageDO,String file, String fileName) {
//调用大模型接口
String result = "";
try {
result = HttpRequest.post(translateServer).form("audio_data", file).timeout(20000) //20秒超时时间
result = HttpRequest.post(translateServer).form("audio_data", HttpUtil.downloadBytes(file), fileName).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
// }
// }
// }
messageDO.setSourceResult(result);
//数据解析
JSONObject translateResult = JSON.parseObject(result);
if (translateResult.isEmpty() || !translateResult.containsKey("intent_result")|| !translateResult.getBoolean("is_species_sound")) {
messageDO.setTransStatus(0);
messageDO.setTransResult("");
return messageDO;
}
//标签 如 cat dog
String speciesLabels = translateResult.getString("species_labels");
//解析翻译结果
JSONObject probabilities = translateResult.getJSONObject("intent_result")
.getJSONObject("probabilities");
.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;
messageDO.setPetType(speciesLabels);
messageDO.setTransStatus(1);
messageDO.setTransResult(StrUtil.isBlank(resultKey) ? "" : resultKey.split(StrUtil.UNDERLINE)[1]);
return messageDO;
}
public static void main(String[] args) {
byte[] bytes = HttpUtil.downloadBytes("http://192.168.1.231:48080/admin-api/infra/file/29/get/c7351abf780f18600c104ec5662d843783ed8c309c01fb427046565217f51102.wav");
// File file = new File("http://192.168.1.231:48080/admin-api/infra/file/29/get/c7351abf780f18600c104ec5662d843783ed8c309c01fb427046565217f51102.wav");
String result = HttpRequest.post("http://43.139.42.137:8000/analyze/audio").form("audio_data", bytes, "aaa").timeout(20000) //20秒超时时间
.execute().body();
System.out.println(result);
}
}

View File

@@ -72,5 +72,4 @@
<appender-ref ref="GRPC"/>
</root>
</springProfile>
</configuration>

View File

@@ -88,6 +88,7 @@ public class FileController {
response.setStatus(HttpStatus.NOT_FOUND.value());
return;
}
response.setContentLength(content.length);
writeAttachment(response, path, content);
}

View File

@@ -1,14 +1,15 @@
package com.tashow.cloud.system.controller.admin.sms.vo.log;
import static com.tashow.cloud.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
import com.tashow.cloud.common.pojo.PageParam;
import java.time.LocalDateTime;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static com.tashow.cloud.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
/** 管理后台 - 短信日志分页 Request VO */
@Data
@EqualsAndHashCode(callSuper = true)
@@ -27,8 +28,8 @@ public class SmsLogPageReqVO extends PageParam {
/** 发送状态,参见 SmsSendStatusEnum 枚举类 */
private Integer sendStatus;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
/** 发送时间 */
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] sendTime;
/** 接收状态,参见 SmsReceiveStatusEnum 枚举类 */