Commit ffea8fc4 authored by Tong Li's avatar Tong Li

AI模型

parent ca995cc5
...@@ -5,6 +5,7 @@ import com.aps.model.ChromosomeData; ...@@ -5,6 +5,7 @@ import com.aps.model.ChromosomeData;
import com.aps.model.MachineOption; import com.aps.model.MachineOption;
import com.aps.model.Operation; import com.aps.model.Operation;
import com.aps.parser.ChromosomeParser; import com.aps.parser.ChromosomeParser;
import com.aps.service.plan.SceneService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
...@@ -34,6 +35,10 @@ public class DiagnosisController { ...@@ -34,6 +35,10 @@ public class DiagnosisController {
private LLMClient llmClient; private LLMClient llmClient;
private static final String UPLOAD_DIR = "uploads"; private static final String UPLOAD_DIR = "uploads";
@Autowired
private SceneService sceneService;
// 移除成员变量,改用Session存储 // 移除成员变量,改用Session存储
// 注意:ChromosomeParser不再使用@Autowired注入,而是每次请求创建新实例 // 注意:ChromosomeParser不再使用@Autowired注入,而是每次请求创建新实例
...@@ -234,7 +239,114 @@ public class DiagnosisController { ...@@ -234,7 +239,114 @@ public class DiagnosisController {
return ResponseEntity.status(500).body(error); return ResponseEntity.status(500).body(error);
} }
} }
/**
* 加载并解析JSON文件
*/
private Map<String, Object> loadChromosomeFile(String sceneId,javax.servlet.http.HttpSession session) {
try {
if (sceneId == null || sceneId.isEmpty()) {
throw new IllegalArgumentException("场景ID不能为空");
}
File file =sceneService.getCurrentChromosomeFile(sceneId);
String filePath = file.getPath();
if (filePath == null || filePath.isEmpty()) {
throw new IllegalArgumentException("文件路径不能为空");
}
// 创建新的Parser实例(避免多用户数据共享)
ChromosomeParser parser = new ChromosomeParser();
// 解析数据
parser.loadFromFile(filePath);
// 获取基本信息和指标
Map<String, Object> basicInfo = parser.getBasicInfo();
Map<String, Object> metrics = parser.calculateMetrics();
String summary = parser.generateSummary();
// 将Parser实例存储到Session中,供后续操作使用
Map<String, Object> sessionData = getSessionData(session);
sessionData.put("parser", parser); // ← 存储Parser实例
sessionData.put("filePath", filePath);
sessionData.put("summary", summary);
sessionData.put("basicInfo", basicInfo);
sessionData.put("metrics", metrics);
return sessionData;
} catch (Exception e) {
Map<String, Object> error = new HashMap<>();
error.put("success", false);
error.put("message", "数据加载失败: " + e.getMessage());
return error;
}
}
/**
* 执行AI诊断
*/
@PostMapping("/diagnoseChromosome")
public ResponseEntity<Map<String, Object>> diagnoseChromosome(@RequestBody Map<String, Object> params,javax.servlet.http.HttpSession session) {
try {
// 从参数中获取sceneId
String sceneId = (String) params.get("sceneId");
// 校验sceneId是否存在
if (sceneId == null || sceneId.isEmpty()) {
throw new IllegalArgumentException("场景ID不能为空");
}
Map<String, Object> sessionData = loadChromosomeFile(sceneId,session);
if (!sessionData.containsKey("summary")) {
throw new IllegalStateException("请先加载数据");
}
// 从Session中获取Parser实例
ChromosomeParser parser = (ChromosomeParser) sessionData.get("parser");
if (parser == null) {
throw new IllegalStateException("解析器未初始化,请重新加载数据");
}
String summary = (String) sessionData.get("summary");
// 调用AI诊断
String rulesContext = "无特定规则约束。\n\n" +
"请作为APS(高级计划与排程)系统专家,基于您的专业知识对以下排产方案进行自由诊断分析。\n\n" +
"您可以从以下角度进行分析:\n" +
"- 整体评估\n" +
"- 瓶颈识别\n" +
"- 效率分析\n" +
"- 优化建议\n" +
"- 风险评估\n";
List<String> diagnosisFocus = Arrays.asList("全面分析");
String report = llmClient.diagnoseSchedule(summary, rulesContext, diagnosisFocus);
// 保存诊断报告(使用Session隔离)
sessionData.put("diagnosisReport", report);
Map<String, Object> response = new HashMap<>();
response.put("success", true);
response.put("report", report);
response.put("message", "诊断完成");
return ResponseEntity.ok(response);
} catch (Exception e) {
Map<String, Object> error = new HashMap<>();
error.put("success", false);
error.put("message", "诊断失败: " + e.getMessage());
e.printStackTrace();
return ResponseEntity.status(500).body(error);
}
}
/** /**
* 执行AI诊断 * 执行AI诊断
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment