DiagnosisRobotController.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. package com.emoon.tongue.controller;
  2. import cn.hutool.crypto.SecureUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.emoon.common.core.domain.R;
  5. import com.emoon.common.core.utils.StringUtils;
  6. import com.emoon.tongue.domain.vo.TestDataVo;
  7. import com.emoon.tongue.domain.vo.TongueDiagnosisInfo;
  8. import com.emoon.tongue.domain.vo.TongueDiagnosisDetailVo;
  9. import com.emoon.tongue.domain.vo.TongueImageCheckVo;
  10. import com.emoon.tongue.service.*;
  11. import com.emoon.tongue.service.impl.LLMRobotServiceImpl;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Qualifier;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.scheduling.annotation.Async;
  17. import org.springframework.web.bind.annotation.*;
  18. import org.springframework.web.multipart.MultipartFile;
  19. import org.springframework.web.servlet.ModelAndView;
  20. import org.springframework.ui.Model;
  21. import jakarta.annotation.PostConstruct;
  22. import java.io.*;
  23. import java.nio.charset.StandardCharsets;
  24. import java.nio.file.*;
  25. import java.util.*;
  26. import java.util.Random;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.atomic.AtomicLong;
  29. /**
  30. * 舌诊控制器(机器人端)
  31. *
  32. * @author destiny
  33. * @date 2025-12-09
  34. */
  35. @Slf4j
  36. @RestController
  37. @RequestMapping("/api/v1/diagnosis/robot")
  38. @CrossOrigin(origins = "*", maxAge = 3600)
  39. public class DiagnosisRobotController {
  40. @Autowired
  41. private ITongueDiagnosisService tongueDiagnosisService;
  42. @Autowired
  43. private MinioService minioService;
  44. @Autowired
  45. private ITongueImageCheckService tongueImageCheckService;
  46. @Autowired
  47. private ITongueAiDiagnosisService tongueAiDiagnosisService;
  48. @Autowired
  49. @Qualifier("llmRobotService")
  50. private ILLMService llmService;
  51. @Autowired
  52. private ISaveFileService saveFileService;
  53. // 大模型配置
  54. @Value("${ai.llm.url}")
  55. private String llmUrl;
  56. @Value("${ai.llm.model}")
  57. private String llmModel;
  58. @Value("${ai.llm.max-tokens}")
  59. private Integer llmMaxTokens;
  60. private static final AtomicLong MOCK_ID_SEQ = new AtomicLong(1);
  61. /** 遮罩开关:true=显示遮罩 */
  62. private volatile boolean showMask = true;
  63. /** CSV 持久化文件路径(首次写入时确定) */
  64. private Path maskConfigPath;
  65. @PostConstruct
  66. public void loadMaskConfig() {
  67. try {
  68. // 优先从 jar 同级目录读取外部文件,便于部署后修改
  69. Path externalPath = Paths.get("data", "mask-config.csv");
  70. if (Files.exists(externalPath)) {
  71. maskConfigPath = externalPath;
  72. } else {
  73. // 从 classpath 拷贝到外部目录,后续读写都走外部文件
  74. Files.createDirectories(externalPath.getParent());
  75. try (InputStream is = getClass().getClassLoader().getResourceAsStream("data/mask-config.csv")) {
  76. if (is != null) {
  77. Files.copy(is, externalPath, StandardCopyOption.REPLACE_EXISTING);
  78. } else {
  79. Files.writeString(externalPath, "showMask,1\n", StandardCharsets.UTF_8);
  80. }
  81. }
  82. maskConfigPath = externalPath;
  83. }
  84. String content = Files.readString(maskConfigPath, StandardCharsets.UTF_8).trim();
  85. // 格式: showMask,1 或 showMask,0
  86. if (content.contains(",")) {
  87. String val = content.substring(content.indexOf(',') + 1).trim();
  88. showMask = !"0".equals(val);
  89. }
  90. log.info("遮罩开关配置加载完成: showMask={}, path={}", showMask, maskConfigPath.toAbsolutePath());
  91. } catch (Exception e) {
  92. log.warn("加载遮罩开关配置失败,使用默认值 showMask=true", e);
  93. }
  94. }
  95. private void persistMaskConfig() {
  96. try {
  97. if (maskConfigPath != null) {
  98. Files.writeString(maskConfigPath, "showMask," + (showMask ? "1" : "0") + "\n", StandardCharsets.UTF_8);
  99. }
  100. } catch (Exception e) {
  101. log.error("持久化遮罩开关配置失败", e);
  102. }
  103. }
  104. @GetMapping("/mask-config")
  105. public R<Map<String, Object>> getMaskConfig() {
  106. Map<String, Object> data = new HashMap<>();
  107. data.put("showMask", showMask ? 1 : 0);
  108. return R.ok(data);
  109. }
  110. @PostMapping("/mask-config")
  111. public R<Void> setMaskConfig(@RequestBody Map<String, Object> body) {
  112. Object val = body.get("showMask");
  113. if (val == null) {
  114. return R.fail("参数 showMask 不能为空");
  115. }
  116. showMask = !"0".equals(String.valueOf(val)) && !"false".equalsIgnoreCase(String.valueOf(val));
  117. persistMaskConfig();
  118. log.info("遮罩开关已更新: showMask={}", showMask);
  119. return R.ok();
  120. }
  121. /**
  122. * 获取舌诊任务详情(内网调用,无需签名验证)
  123. *
  124. * @param patientId 患者ID
  125. * @param projectId 医院编码
  126. * @return 舌诊任务详情
  127. */
  128. @GetMapping("/internal/detail")
  129. public R<TongueDiagnosisDetailVo> getDiagnosisDetailInternal(@RequestParam String patientId,
  130. @RequestParam String projectId) {
  131. try {
  132. TongueDiagnosisDetailVo detail = tongueDiagnosisService.getDiagnosisDetail(patientId, projectId);
  133. if (detail != null) {
  134. detail.setShowMask(showMask);
  135. }
  136. return R.ok(detail);
  137. } catch (Exception e) {
  138. log.error("获取舌诊详情异常", e);
  139. return R.fail("获取舌诊详情失败:" + e.getMessage());
  140. }
  141. }
  142. /**
  143. * 获取舌诊任务详情
  144. *
  145. * @param patientId 患者ID
  146. * @param projectId 医院编码
  147. * @param timestamp 时间戳
  148. * @param version 版本号
  149. * @param sign 签名
  150. * @return 舌诊任务详情
  151. */
  152. @GetMapping("/detail")
  153. public R<TongueDiagnosisDetailVo> getDiagnosisDetail(@RequestParam String patientId,
  154. @RequestParam String projectId,
  155. @RequestParam Long timestamp,
  156. @RequestParam String version,
  157. @RequestParam String sign) {
  158. // 验证时间戳(5分钟内有效)
  159. if (System.currentTimeMillis() - timestamp > 500 * 60 * 1000) {
  160. return R.fail("请求已过期");
  161. }
  162. // 验证签名
  163. Map<String, Object> params = new HashMap<>();
  164. params.put("patientId", patientId);
  165. params.put("projectId", projectId);
  166. params.put("timestamp", timestamp);
  167. params.put("version", version);
  168. if (!validateSign(params, sign)) {
  169. return R.fail("签名验证失败");
  170. }
  171. TongueDiagnosisDetailVo detail = tongueDiagnosisService.getDiagnosisDetail(patientId, projectId);
  172. if (detail != null) {
  173. detail.setShowMask(showMask);
  174. }
  175. return R.ok(detail);
  176. }
  177. /**
  178. * 舌诊AI诊断接口
  179. *
  180. * @param patientId 患者ID
  181. * @param projectId 医院编码
  182. * @param timestamp 时间戳
  183. * @param version 版本号
  184. * @param sign 签名
  185. * @return 任务ID
  186. */
  187. @PostMapping("/ai-diagnosis")
  188. public R<Long> performAiDiagnosis(@RequestParam String patientId,
  189. @RequestParam String projectId,
  190. @RequestParam Long timestamp,
  191. @RequestParam String version,
  192. @RequestParam String sign) {
  193. // 验证时间戳(5分钟内有效)
  194. if (System.currentTimeMillis() - timestamp > 500 * 60 * 1000) {
  195. return R.fail("请求已过期");
  196. }
  197. // 验证签名
  198. Map<String, Object> params = new HashMap<>();
  199. params.put("patientId", patientId);
  200. params.put("projectId", projectId);
  201. params.put("timestamp", timestamp);
  202. params.put("version", version);
  203. if (!validateSign(params, sign)) {
  204. return R.fail("签名验证失败");
  205. }
  206. try {
  207. // 获取记录主键ID作为任务ID
  208. Long taskId = tongueDiagnosisService.getDiagnosisId(patientId, projectId);
  209. if (taskId == null) {
  210. return R.fail("未找到舌诊记录,请先上传舌象图片并通过校验");
  211. }
  212. // 异步执行AI诊断
  213. performAiDiagnosisAsync(taskId, patientId, projectId);
  214. return R.ok("诊断任务已启动", taskId);
  215. } catch (Exception e) {
  216. log.error("舌诊AI诊断异常", e);
  217. return R.fail("诊断失败:" + e.getMessage());
  218. }
  219. }
  220. /**
  221. * 舌象图片校验接口
  222. *
  223. * @param file 舌象图片文件
  224. * @param patientId 患者ID
  225. * @param projectId 医院编码
  226. * @param timestamp 时间戳
  227. * @param sign 签名
  228. * @param version 版本号
  229. * @param bizParams 业务参数JSON字符串,包含患者信息
  230. * @return 校验结果
  231. */
  232. @PostMapping("/check-image")
  233. public R<TongueImageCheckVo> checkTongueImage(@RequestParam("file") MultipartFile file,
  234. @RequestParam String patientId,
  235. @RequestParam String projectId,
  236. @RequestParam Long timestamp,
  237. @RequestParam String sign,
  238. @RequestParam String version,
  239. @RequestParam String bizParams) {
  240. // 验证时间戳(5分钟内有效)
  241. if (System.currentTimeMillis() - timestamp > 500 * 60 * 1000) {
  242. return R.fail("请求已过期");
  243. }
  244. // 验证签名
  245. Map<String, Object> params = new HashMap<>();
  246. params.put("file", file.getOriginalFilename());
  247. params.put("patientId", patientId);
  248. params.put("projectId", projectId);
  249. params.put("timestamp", timestamp);
  250. params.put("version", version);
  251. params.put("bizParams", bizParams);
  252. if (!validateSign(params, sign)) {
  253. return R.fail("签名验证失败");
  254. }
  255. try {
  256. // 解析bizParams JSON
  257. Map<String, Object> patientInfo = JSONUtil.parseObj(bizParams);
  258. Integer patientAge = null;
  259. Integer patientGender = null;
  260. String patientChiefComplaint = null;
  261. try {
  262. patientAge = (Integer) patientInfo.get("patientAge");
  263. } catch (Exception e) {
  264. log.warn("解析patientAge失败,设置为null", e);
  265. }
  266. try {
  267. patientGender = (Integer) patientInfo.get("patientGender");
  268. } catch (Exception e) {
  269. log.warn("解析patientGender失败,设置为null", e);
  270. }
  271. try {
  272. patientChiefComplaint = (String) patientInfo.get("patientChiefComplaint");
  273. } catch (Exception e) {
  274. log.warn("解析patientChiefComplaint失败,设置为null", e);
  275. }
  276. // 上传文件到MinIO
  277. String fileUrl = minioService.uploadFile(file);
  278. if (fileUrl == null || fileUrl.isEmpty()) {
  279. return R.fail("文件上传失败");
  280. }
  281. String rawFilePath = saveFileService.uploadFilePath(file);
  282. String filePath = convertToLocalModelFileUri(rawFilePath);
  283. log.info("文件已保存至服务器并转换为本地模型路径: patientId={}, projectId={}, fileName={}, rawFilePath={}, filePath={}",
  284. patientId, projectId, file != null ? file.getOriginalFilename() : null, rawFilePath, filePath);
  285. if (filePath == null || filePath.isEmpty()) {
  286. return R.fail("文件保存至服务器失败");
  287. }
  288. // 调用大模型服务校验舌象图片
  289. TongueImageCheckVo checkResult = tongueImageCheckService.checkTongueImage(fileUrl);
  290. if (checkResult.getErrorCode() != null && checkResult.getErrorCode() == 0) {
  291. // 图片符合要求,保存上传记录
  292. boolean success = tongueDiagnosisService.handleTongueImageUpload(
  293. fileUrl, filePath, patientId, projectId, patientAge, patientGender, patientChiefComplaint);
  294. if (!success) {
  295. return R.fail("上传记录保存失败");
  296. }
  297. return R.ok("图片符合要求", checkResult);
  298. } else {
  299. // 图片不符合要求,返回对应的错误规则详情
  300. return R.fail(checkResult.getErrorType(), checkResult);
  301. }
  302. } catch (Exception e) {
  303. log.error("舌象图片校验异常", e);
  304. return R.fail("校验失败:" + e.getMessage());
  305. }
  306. }
  307. /**
  308. * 生成测试数据接口
  309. *
  310. * @param file 舌象图片文件(可选)
  311. * @return 测试数据
  312. */
  313. @PostMapping("/generate-test-data")
  314. public R<TestDataVo> generateTestData(@RequestParam(value = "file", required = false) MultipartFile file) {
  315. TestDataVo testData = new TestDataVo();
  316. try {
  317. // 随机生成患者ID
  318. String dateStr = String.valueOf(System.currentTimeMillis()).substring(0, 8);
  319. int randomNum = new Random().nextInt(999) + 1;
  320. testData.setPatientId("GH" + dateStr + String.format("%03d", randomNum));
  321. // 随机生成医院编码
  322. String[] hospitalCodes = {"HOSP001", "HOSP002", "HOSP003", "HOSP004", "HOSP005"};
  323. testData.setProjectId(hospitalCodes[new Random().nextInt(hospitalCodes.length)]);
  324. // 当前时间戳
  325. testData.setTimestamp(System.currentTimeMillis());
  326. // 固定版本号
  327. testData.setVersion("v1.0.0");
  328. // 随机生成患者信息
  329. testData.setPatientAge(new Random().nextInt(50) + 18); // 18-67岁
  330. testData.setPatientGender(new Random().nextInt(2) + 1); // 1或2
  331. String[] complaints = {
  332. "口干舌燥,睡眠不佳",
  333. "胃胀胃痛,食欲不振",
  334. "头晕乏力,精神不振",
  335. "咳嗽有痰,胸闷气短",
  336. "腰膝酸软,畏寒怕冷",
  337. "心烦易怒,失眠多梦",
  338. "消化不良,大便不成形",
  339. "面色苍白,手脚冰凉"
  340. };
  341. testData.setPatientChiefComplaint(complaints[new Random().nextInt(complaints.length)]);
  342. // 生成bizParams JSON字符串
  343. Map<String, Object> patientInfo = new HashMap<>();
  344. patientInfo.put("patientAge", testData.getPatientAge());
  345. patientInfo.put("patientGender", testData.getPatientGender());
  346. patientInfo.put("patientChiefComplaint", testData.getPatientChiefComplaint());
  347. testData.setBizParams(JSONUtil.toJsonStr(patientInfo));
  348. // 设置文件名:如果传入了文件则使用originalName,否则随机生成
  349. if (file != null && !file.isEmpty() && file.getOriginalFilename() != null) {
  350. testData.setFileName(file.getOriginalFilename());
  351. } else {
  352. String[] extensions = {".jpg", ".jpeg", ".png", ".bmp"};
  353. String extension = extensions[new Random().nextInt(extensions.length)];
  354. testData.setFileName("tongue_" + System.currentTimeMillis() + extension);
  355. }
  356. // 生成签名
  357. Map<String, Object> signParams = new HashMap<>();
  358. signParams.put("file", testData.getFileName());
  359. signParams.put("patientId", testData.getPatientId());
  360. signParams.put("projectId", testData.getProjectId());
  361. signParams.put("timestamp", testData.getTimestamp());
  362. signParams.put("bizParams", testData.getBizParams());
  363. signParams.put("version", testData.getVersion());
  364. String generatedSign = generateSign(signParams);
  365. testData.setSign(generatedSign);
  366. return R.ok("测试数据生成成功", testData);
  367. } catch (Exception e) {
  368. log.error("生成测试数据异常", e);
  369. return R.fail("生成测试数据失败:" + e.getMessage());
  370. }
  371. }
  372. /**
  373. * 生成签名
  374. *
  375. * @param params 参数Map
  376. * @return MD5签名
  377. */
  378. private String generateSign(Map<String, Object> params) {
  379. // 按照ASCII码排序参数
  380. List<String> sortedKeys = new ArrayList<>(params.keySet());
  381. Collections.sort(sortedKeys);
  382. // 拼接参数字符串
  383. StringBuilder sb = new StringBuilder();
  384. for (String key : sortedKeys) {
  385. Object value = params.get(key);
  386. if (value != null && StringUtils.isNotEmpty(value.toString())) {
  387. sb.append(key).append("=").append(value).append("&");
  388. }
  389. }
  390. // 移除最后一个&
  391. if (!sb.isEmpty()) {
  392. sb.deleteCharAt(sb.length() - 1);
  393. }
  394. // MD5加密并转为大写
  395. return SecureUtil.md5(sb.toString()).toUpperCase();
  396. }
  397. /**
  398. * 异步执行AI诊断
  399. *
  400. * @param taskId 任务ID(记录主键ID)
  401. * @param patientId 患者ID
  402. * @param projectId 医院编码
  403. */
  404. @Async("tongueDiagnosisExecutor")
  405. public void performAiDiagnosisAsync(Long taskId, String patientId, String projectId) {
  406. log.info("开始异步执行AI诊断,任务ID: {}, 患者ID: {}", taskId, patientId);
  407. try {
  408. // 模拟AI诊断处理时间,随机睡眠1-10秒
  409. Random random = new Random();
  410. int sleepSeconds = random.nextInt(10) + 1;
  411. log.info("AI诊断预计耗时: {} 秒", sleepSeconds);
  412. TimeUnit.SECONDS.sleep(sleepSeconds);
  413. // 生成AI诊断结果
  414. TongueDiagnosisInfo diagnosisResult = tongueAiDiagnosisService.performDiagnosis(patientId, projectId);
  415. if (diagnosisResult == null) {
  416. log.error("AI诊断结果为空,任务ID: {}", taskId);
  417. }
  418. // 将诊断结果转换为JSON字符串
  419. String diagnosisJson = JSONUtil.toJsonStr(diagnosisResult);
  420. // 更新数据库中的诊断结果
  421. boolean success = tongueDiagnosisService.updateAiDiagnosisResult(taskId, diagnosisJson);
  422. log.info("AI诊断结果: {}, 任务ID: {},是否保存成功: {}", diagnosisJson, taskId, success);
  423. } catch (InterruptedException e) {
  424. log.error("AI诊断任务被中断,任务ID: {}", taskId, e);
  425. Thread.currentThread().interrupt();
  426. } catch (Exception e) {
  427. log.error("AI诊断异步执行异常,任务ID: {}", taskId, e);
  428. }
  429. }
  430. /**
  431. * 调用大模型API接口
  432. *
  433. * @param request 请求体,包含prompt字段和type字段(validate: 判断主诉, extract: 字段提取)
  434. * @return 模型响应结果
  435. */
  436. @PostMapping("/llm-call")
  437. public R<String> callLlm(@RequestBody Map<String, String> request) {
  438. log.info("收到LLM调用请求,参数: {}", request);
  439. try {
  440. String prompt = request.get("prompt");
  441. if (StringUtils.isEmpty(prompt)) {
  442. return R.fail("提示词不能为空");
  443. }
  444. String type = request.get("type");
  445. log.info("请求类型: {}", type);
  446. String result;
  447. if ("validate".equals(type)) {
  448. // 判断主诉
  449. log.info("开始调用LLM服务(判断主诉),主诉原文: {}", prompt.substring(0, Math.min(100, prompt.length())) + "...");
  450. if (llmService instanceof LLMRobotServiceImpl) {
  451. result = ((LLMRobotServiceImpl) llmService).validateChiefComplaint(prompt, llmUrl, llmModel, llmMaxTokens);
  452. } else {
  453. log.error("llmService不是LLMRobotServiceImpl类型,无法调用validateChiefComplaint");
  454. return R.fail("服务类型错误");
  455. }
  456. log.info("【判断主诉】Controller返回的结果: {}", result);
  457. } else {
  458. // 字段提取(默认)
  459. log.info("开始调用LLM服务(字段提炼),prompt: {}", prompt.substring(0, Math.min(100, prompt.length())) + "...");
  460. result = llmService.callLLM(prompt, llmUrl, llmModel, llmMaxTokens);
  461. log.info("【字段提炼】Controller返回的JSON内容: {}", result);
  462. }
  463. log.info("LLM调用成功,返回结果长度: {}", result != null ? result.length() : 0);
  464. // 返回结果,放在msg字段(data为null)
  465. return R.ok(result);
  466. } catch (Exception e) {
  467. log.error("调用大模型API失败", e);
  468. return R.fail("调用失败: " + e.getMessage());
  469. }
  470. }
  471. /**
  472. * 生成签名接口(供H5页面调用)
  473. *
  474. * @param params 参数Map
  475. * @return 生成的签名
  476. */
  477. @PostMapping("/generate-sign")
  478. public R<String> generateSignForFrontend(@RequestBody Map<String, Object> params) {
  479. try {
  480. String sign = generateSign(params);
  481. return R.ok("签名生成成功", sign);
  482. } catch (Exception e) {
  483. log.error("生成签名异常", e);
  484. return R.fail("生成签名失败:" + e.getMessage());
  485. }
  486. }
  487. /**
  488. * 验证签名
  489. *
  490. * @param params 参数Map
  491. * @param sign 待验证签名
  492. * @return 是否验证通过
  493. */
  494. private boolean validateSign(Map<String, Object> params, String sign) {
  495. String generatedSign = generateSign(params);
  496. log.info("生成签名: {}, 待验证签名: {}", generatedSign, sign);
  497. return generatedSign.equals(sign);
  498. }
  499. /**
  500. * 将落盘服务返回的 filePath(例如:/ruoyi/file-downloads/xxx.jpg)
  501. * 转换为本地模型可直接读取的 file:// URI(例如:file:///home/gansu/gansu/shetou_picture/xxx.jpg)
  502. */
  503. private String convertToLocalModelFileUri(String rawFilePath) {
  504. if (rawFilePath == null) {
  505. return null;
  506. }
  507. String s = rawFilePath.trim();
  508. if (s.isEmpty()) {
  509. return null;
  510. }
  511. // 已经是 file://... 形式就直接返回
  512. if (s.startsWith("file://")) {
  513. return s;
  514. }
  515. // 取最后的文件名(兼容 / 和 \)
  516. int idx1 = s.lastIndexOf('/');
  517. int idx2 = s.lastIndexOf('\\');
  518. int idx = Math.max(idx1, idx2);
  519. String fileName = idx >= 0 ? s.substring(idx + 1) : s;
  520. if (fileName.isEmpty()) {
  521. return null;
  522. }
  523. // 固定替换为模型服务器本地目录
  524. return "file:///home/gansu/gansu/shetou_picture/" + fileName;
  525. }
  526. /**
  527. * 本地模型 Mock:返回固定的 ChatCompletions 结构(用于本地模型未训练完时联调整体流程)
  528. *
  529. * <p>注意:该接口返回的是"原始 chat.completion JSON",不会包一层 R,否则本地 provider 无法解析 choices。</p>
  530. */
  531. @PostMapping("/mock/local-chat-completions")
  532. public Map<String, Object> mockLocalChatCompletions(@RequestBody(required = false) Map<String, Object> request) {
  533. String model = request != null && request.get("model") != null ? String.valueOf(request.get("model")) : "/home/gansu/output_sft_qwen3_2B";
  534. long created = System.currentTimeMillis() / 1000;
  535. Map<String, Object> message = new HashMap<>();
  536. message.put("role", "assistant");
  537. message.put("content",
  538. "病人舌苔呈现的现象为:厚苔, 润苔, 腻苔, 裂纹舌, 青红舌, 黄, 齿痕舌。\n" +
  539. "诊断为:脾肾不固证。\n" +
  540. "处方:中药14付,每日两次,自煎 200ml,分2次中药口服:黑顺片 3g 先煎,红参 3g 另煎,干姜 3g,砂仁 9g 后下,酒大黄 3g,焦槟榔 9g,乌药 9g,麸炒枳壳 15g,醋香附 9g,沉香 3g 后下,檀香 5g,木香 6g,制吴茱萸 3g,丹参 15g,连翘 15g。"
  541. );
  542. message.put("refusal", null);
  543. message.put("annotations", null);
  544. message.put("audio", null);
  545. message.put("function_call", null);
  546. message.put("tool_calls", Collections.emptyList());
  547. message.put("reasoning_content", null);
  548. Map<String, Object> choice = new HashMap<>();
  549. choice.put("index", 0);
  550. choice.put("message", message);
  551. choice.put("logprobs", null);
  552. choice.put("finish_reason", "stop");
  553. choice.put("stop_reason", null);
  554. choice.put("token_ids", null);
  555. Map<String, Object> usage = new HashMap<>();
  556. usage.put("prompt_tokens", 86);
  557. usage.put("total_tokens", 274);
  558. usage.put("completion_tokens", 188);
  559. usage.put("prompt_tokens_details", null);
  560. Map<String, Object> resp = new HashMap<>();
  561. resp.put("id", "chatcmpl-mock-" + MOCK_ID_SEQ.getAndIncrement());
  562. resp.put("object", "chat.completion");
  563. resp.put("created", created);
  564. resp.put("model", model);
  565. resp.put("choices", List.of(choice));
  566. resp.put("service_tier", null);
  567. resp.put("system_fingerprint", null);
  568. resp.put("usage", usage);
  569. resp.put("prompt_logprobs", null);
  570. resp.put("prompt_token_ids", null);
  571. resp.put("kv_transfer_params", null);
  572. return resp;
  573. }
  574. }