|
|
@@ -4,17 +4,28 @@ import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import cn.tr.module.smart.app.controller.vo.WxDoctorClinicRoomThumbnailVO;
|
|
|
+import cn.tr.module.smart.common.dto.BizQuestionDTO;
|
|
|
import cn.tr.module.smart.common.po.BizDeptPO;
|
|
|
import cn.tr.module.smart.common.properties.ALiYunProperties;
|
|
|
import cn.tr.module.smart.common.repository.BizDeptRepository;
|
|
|
import cn.tr.module.smart.common.service.ALiYunService;
|
|
|
import cn.tr.module.sys.storage.dto.SysStorageRecordDTO;
|
|
|
import cn.tr.module.sys.storage.service.IStorageFileService;
|
|
|
+import com.alibaba.dashscope.aigc.generation.Generation;
|
|
|
+import com.alibaba.dashscope.aigc.generation.GenerationParam;
|
|
|
+import com.alibaba.dashscope.aigc.generation.GenerationResult;
|
|
|
+import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
|
|
|
+import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
|
|
|
+import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
|
|
|
import com.alibaba.dashscope.app.Application;
|
|
|
import com.alibaba.dashscope.app.ApplicationParam;
|
|
|
import com.alibaba.dashscope.app.ApplicationResult;
|
|
|
+import com.alibaba.dashscope.common.Message;
|
|
|
+import com.alibaba.dashscope.common.MultiModalMessage;
|
|
|
+import com.alibaba.dashscope.common.Role;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
import com.aliyun.teaopenapi.models.OpenApiRequest;
|
|
|
import com.aliyun.teaopenapi.models.Params;
|
|
|
@@ -78,9 +89,77 @@ public class ALiYunServiceImpl implements ALiYunService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// @Override
|
|
|
+// public List<WxDoctorClinicRoomThumbnailVO> recognizeImage(MultipartFile file) throws Exception {
|
|
|
+// List<WxDoctorClinicRoomThumbnailVO> result = new ArrayList<>();
|
|
|
+// List<BizDeptPO> deptList = deptRepository.selectList(new LambdaQueryWrapper<>());
|
|
|
+//
|
|
|
+// //使用缓存模版
|
|
|
+// String prompt = promptTemplate;
|
|
|
+//
|
|
|
+// if (CollectionUtil.isNotEmpty(deptList)) {
|
|
|
+// prompt=String.format(prompt,
|
|
|
+// JSONUtil.toJsonStr( deptList.stream()
|
|
|
+// .map(dept->
|
|
|
+// MapUtil.builder()
|
|
|
+// .put("deptName",dept.getName())
|
|
|
+// .put("deptId",dept.getId())
|
|
|
+// .build()
|
|
|
+// )
|
|
|
+//
|
|
|
+// .collect(Collectors.toList())));
|
|
|
+// }
|
|
|
+// String imageUrl;
|
|
|
+// SysStorageRecordDTO uploadResult = null;
|
|
|
+// try {
|
|
|
+// uploadResult= fileService.upload(
|
|
|
+// null,
|
|
|
+// null,
|
|
|
+// file.getOriginalFilename(),
|
|
|
+// file.getBytes(),
|
|
|
+// null);
|
|
|
+// imageUrl = uploadResult.getAbsolutePath();
|
|
|
+//
|
|
|
+// ApplicationParam param = ApplicationParam.builder()
|
|
|
+// .appId(aliyunProperties.getImageAgent().getAppId())
|
|
|
+// .apiKey(aliyunProperties.getImageAgent().getApiKey())
|
|
|
+// .prompt(prompt)
|
|
|
+// .images(Collections.singletonList(imageUrl))
|
|
|
+// .build();
|
|
|
+//
|
|
|
+// Application app = new Application();
|
|
|
+// ApplicationResult response = app.call(param);
|
|
|
+// String jsonResponse = response.getOutput().getText();
|
|
|
+//
|
|
|
+// String cleanedJson = cleanMarkdownJson(jsonResponse);
|
|
|
+//
|
|
|
+// ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+//
|
|
|
+// if (cleanedJson.trim().startsWith("[")) {
|
|
|
+// List<Map<String, Object>> patientList = objectMapper.readValue(
|
|
|
+// cleanedJson,
|
|
|
+// new TypeReference<List<Map<String, Object>>>() {}
|
|
|
+// );
|
|
|
+// if(CollectionUtil.isNotEmpty(patientList)){
|
|
|
+// return patientList.stream()
|
|
|
+// .map(info ->
|
|
|
+// JSONUtil.toBean(JSONUtil.toJsonStr(info), WxDoctorClinicRoomThumbnailVO.class)
|
|
|
+// )
|
|
|
+// .filter(info-> StrUtil.isNotEmpty(info.getPatientCode()))
|
|
|
+// .collect(Collectors.toList());
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }finally {
|
|
|
+// if (ObjectUtil.isNotNull(uploadResult)) {
|
|
|
+// fileService.remove(Arrays.asList(uploadResult.getId()));
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return result;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public List<WxDoctorClinicRoomThumbnailVO> recognizeImage(MultipartFile file) throws Exception {
|
|
|
- List<WxDoctorClinicRoomThumbnailVO> result = new ArrayList<>();
|
|
|
List<BizDeptPO> deptList = deptRepository.selectList(new LambdaQueryWrapper<>());
|
|
|
|
|
|
//使用缓存模版
|
|
|
@@ -107,47 +186,30 @@ public class ALiYunServiceImpl implements ALiYunService {
|
|
|
file.getOriginalFilename(),
|
|
|
file.getBytes(),
|
|
|
null);
|
|
|
-
|
|
|
imageUrl = uploadResult.getAbsolutePath();
|
|
|
|
|
|
- ApplicationParam param = ApplicationParam.builder()
|
|
|
- .appId(aliyunProperties.getAppId())
|
|
|
- .apiKey(aliyunProperties.getApiKey())
|
|
|
- .prompt(prompt)
|
|
|
- .images(Collections.singletonList(imageUrl))
|
|
|
+ MultiModalConversation conv = new MultiModalConversation();
|
|
|
+ MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
|
|
|
+ .content(Arrays.asList(
|
|
|
+ Collections.singletonMap("text", "你是一位经验丰富的医院主任医师,擅长从医疗文档中提取关键信息,并将其转换为结构化的JSON格式"))).build();
|
|
|
+ MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
|
|
|
+ .content(Arrays.asList(
|
|
|
+ Collections.singletonMap("image", imageUrl),
|
|
|
+ Collections.singletonMap("text", prompt))).build();
|
|
|
+ MultiModalConversationParam param = MultiModalConversationParam.builder()
|
|
|
+ .apiKey(aliyunProperties.getImageAgent().getApiKey())
|
|
|
+ .model("qwen3-vl-plus-2025-09-23") // 此处以qwen-vl-max-latest为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/models
|
|
|
+ .messages(Arrays.asList(systemMessage, userMessage))
|
|
|
.build();
|
|
|
-
|
|
|
- Application app = new Application();
|
|
|
- ApplicationResult response = app.call(param);
|
|
|
- String jsonResponse = response.getOutput().getText();
|
|
|
-
|
|
|
- String cleanedJson = cleanMarkdownJson(jsonResponse);
|
|
|
-
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
-
|
|
|
- if (cleanedJson.trim().startsWith("[")) {
|
|
|
- List<Map<String, Object>> patientList = objectMapper.readValue(
|
|
|
- cleanedJson,
|
|
|
- new TypeReference<List<Map<String, Object>>>() {}
|
|
|
- );
|
|
|
- if(CollectionUtil.isNotEmpty(patientList)){
|
|
|
- return patientList.stream()
|
|
|
- .map(info ->
|
|
|
- JSONUtil.toBean(JSONUtil.toJsonStr(info), WxDoctorClinicRoomThumbnailVO.class)
|
|
|
- )
|
|
|
- .filter(info-> StrUtil.isNotEmpty(info.getPatientCode()))
|
|
|
- .collect(Collectors.toList());
|
|
|
- }
|
|
|
- }
|
|
|
+ MultiModalConversationResult result = conv.call(param);
|
|
|
+ return JSONUtil.toList(String.valueOf(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text")), WxDoctorClinicRoomThumbnailVO.class);
|
|
|
}finally {
|
|
|
if (ObjectUtil.isNotNull(uploadResult)) {
|
|
|
fileService.remove(Arrays.asList(uploadResult.getId()));
|
|
|
}
|
|
|
}
|
|
|
- return result;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 创建长期记忆体
|
|
|
*
|
|
|
@@ -200,11 +262,6 @@ public class ALiYunServiceImpl implements ALiYunService {
|
|
|
throw new Exception("无法从响应中提取memoryId: " + response);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void recognizeImageCommit(MultipartFile file) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
private String cleanMarkdownJson(String jsonResponse) {
|
|
|
return jsonResponse
|
|
|
.replaceFirst("^\\s*```json\\s*", "")
|