TASK: 1、LLM对话新增两个工具,查找sketch和修改项目参数 2、新增接口获取最新的minio图片预览地址

This commit is contained in:
2025-06-19 14:56:18 +08:00
parent 161bba896c
commit 0fd1ddcde1
3 changed files with 102 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
package com.ai.da.service.impl;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.common.constant.CommonConstant;
import com.ai.da.common.context.UserContext;
import com.ai.da.common.enums.CollectionLevel1TypeEnum;
@@ -20,11 +21,16 @@ import com.ai.da.python.PythonService;
import com.ai.da.service.DesignService;
import com.ai.da.service.LLMService;
import com.ai.da.service.SysFileService;
import com.ai.da.service.WorkspaceService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.text.StringEscapeUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
@@ -71,6 +77,8 @@ public class LLMServiceImpl implements LLMService {
private CollectionElementMapper collectionElementMapper;
@Resource
private CollectionElementRelModelMapper collectionElementRelModelMapper;
@Value("${minio.bucketName.sysImage}")
private String sysImage;
@Override
public SseEmitter stream(String prompt, Long projectParamId, String fileUrl, List<String> imageUrlList, String token, Boolean enableThinking, String process) {
@@ -78,12 +86,12 @@ public class LLMServiceImpl implements LLMService {
executor.submit(() -> {
try {
boolean validate = jwtTokenHelper.validateToken(token);
boolean validate = jwtTokenHelper.validateToken(token); //
if (validate) {
AuthPrincipalVo principal = jwtTokenHelper.parserToUser(token);
Long accountId = principal.getId();
String url = "http://18.167.251.121:10002/chat-stream";
// String url = "http://10.1.1.240:1013/chat-stream";
// String url = "http://18.167.251.121:10002/chat-stream";
String url = "http://10.1.1.240:1013/chat-stream";
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
@@ -222,10 +230,12 @@ public class LLMServiceImpl implements LLMService {
@Override
public SseEmitter streamNew(String prompt, Long projectParamId, String fileUrl, List<String> imageUrlList, String token, Boolean enableThinking, String process) {
SseEmitter emitter = new SseEmitter(0L); // 永不超时
List<String> designTools = Arrays.asList("moodboard", "printboard", "generate_color_code", "sketchboard");
executor.submit(() -> {
try {
boolean validate = jwtTokenHelper.validateToken(token);
// boolean validate = jwtTokenHelper.validateToken(token);
boolean validate = true;
if (validate) {
AuthPrincipalVo principal = jwtTokenHelper.parserToUser(token);
Long accountId = principal.getId();
@@ -327,7 +337,9 @@ public class LLMServiceImpl implements LLMService {
}
String toolsName = json.getString("tools_name");
JSONObject toolsData = json.getJSONObject("content");
if (Objects.nonNull(toolsName) && !toolsName.equals("design_control_signal") && !toolsName.equals("generate_project_info")) {
/*&& !toolsName.equals("design_control_signal")
&& !toolsName.equals("generate_project_info")*/
if (Objects.nonNull(toolsName) && designTools.contains(toolsName)) {
boolean color = true;
ReceiveDesignParam receiveDesignParam = JSONObject.parseObject(JSONObject.toJSONString(toolsData), ReceiveDesignParam.class);
receiveDesignParam.setProjectId(projectId);
@@ -353,6 +365,11 @@ public class LLMServiceImpl implements LLMService {
systemImage.setContent(JSONObject.toJSONString(receiveDesignParam.getReceiveCollectionElementList()));
systemImage.setAccountId(accountId);
chatMessageMapper.insert(systemImage);
} else if (Objects.nonNull(toolsName) && toolsName.equals("search_sketch_img")) {
json.put("content", processSearchSketchToolCon(toolsData));
} else if (Objects.nonNull(toolsName)
&& toolsName.equals("update_project_info")) {
updateProjectParams(projectId, toolsData);
}
emitter.send(json.toJSONString());
}
@@ -367,7 +384,6 @@ public class LLMServiceImpl implements LLMService {
}
}
}
emitter.complete();
} catch (Exception e) {
System.out.println("走进异常");
@@ -378,6 +394,72 @@ public class LLMServiceImpl implements LLMService {
return emitter;
}
private String processSearchSketchToolCon(JSONObject content){
// content内容 "{\"sketch_list\": [\"\\\\female\\\\blouse\\\\blouse_645.jpg\", \"\\\\female\\\\dress\\\\0902004968.jpg\"]}"
JSONArray sketchList = content.getJSONArray("sketch_list");
if (sketchList.isEmpty()){
return null;
}
ArrayList<ReceiveCollectionElement> dataList = new ArrayList<>();
sketchList.forEach(sketch -> {
ReceiveCollectionElement receiveCollectionElement = new ReceiveCollectionElement();
receiveCollectionElement.setUrl((String) sketch);
receiveCollectionElement.setMinioUrl(minioUtil.getPreSignedUrl(sysImage + "/images" + sketch, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
dataList.add(receiveCollectionElement);
});
ReceiveDesignParam receiveDesignParam = new ReceiveDesignParam();
receiveDesignParam.setReceiveCollectionElementList(dataList);
return JSONArray.toJSONString(receiveDesignParam);
}
@Resource
private WorkspaceService workspaceService;
private String updateProjectParams(Long projectId, JSONObject jsonObject){
// 1. 获取 project_info 并转换为 List<Map<String, String>>
List<Map<String, String>> projectInfoList = (List<Map<String, String>>) jsonObject.get("project_info");
Long workspaceId = workspaceService.getByProjectId(projectId);
if (Objects.isNull(workspaceId)){
throw new BusinessException("未知projectId");
}
Workspace workspace = workspaceService.getBaseMapper().selectOne(new QueryWrapper<Workspace>().eq("project_id", projectId));
if (!projectInfoList.isEmpty()){
projectInfoList.forEach(info ->
info.forEach((key, value) -> {
switch (key){
case "project_name":
Project project = projectMapper.selectById(projectId);
project.setName(value);
project.setUpdateTime(LocalDateTime.now());
projectMapper.updateById(project);
break;
case "ageGroup":
workspace.setAgeGroup(value);
break;
case "gender":
workspace.setSex(value);
break;
case "position":
workspace.setPosition(value);
break;
case "style":
QueryWrapper<Style> qw = new QueryWrapper<>();
qw.lambda().eq(Style::getName, value);
Style style = styleMapper.selectOne(qw);
if (Objects.nonNull(style)) {
WorkspaceRelStyle workspaceRelStyle = workspaceRelStyleMapper.selectOne(new QueryWrapper<WorkspaceRelStyle>().eq("workspace_id", workspaceId));
workspaceRelStyle.setStyleId(style.getId());
workspaceRelStyleMapper.updateById(workspaceRelStyle);
}
break;
}
})
);
workspaceMapper.updateById(workspace);
}
return null;
}
@Override
public Long chatCreateProject(String prompt, String process, String fileUrl, List<String> imageUrlList) {
AuthPrincipalVo userHolder = UserContext.getUserHolder();