|
|
|
@ -7,9 +7,6 @@ import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.google.gson.Gson; |
|
|
|
import com.inspect.simulator.domain.algorithm.in.AnalyseRequest; |
|
|
|
import com.inspect.simulator.domain.algorithm.out.AnalyseResItem; |
|
|
|
import com.inspect.simulator.domain.algorithm.out.AnalyseResPoint; |
|
|
|
import com.inspect.simulator.domain.algorithm.out.AnalyseResult; |
|
|
|
import com.inspect.simulator.domain.assistant.*; |
|
|
|
import com.inspect.simulator.domain.bigmodelr.*; |
|
|
|
import com.inspect.simulator.domain.visual.*; |
|
|
|
@ -17,15 +14,14 @@ import com.inspect.simulator.mapper.PatrolResultMapper; |
|
|
|
import com.inspect.simulator.mapper.ResultAnalysisMapper; |
|
|
|
import com.inspect.simulator.service.ModelService; |
|
|
|
import com.inspect.simulator.service.remote.AnalysisRemoteService; |
|
|
|
import com.inspect.simulator.utils.HttpClientUtils; |
|
|
|
import com.inspect.simulator.utils.redis.RedisService; |
|
|
|
import lombok.Value; |
|
|
|
import okhttp3.*; |
|
|
|
import org.apache.commons.compress.utils.IOUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.net.ftp.FTPSClient; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.cloud.context.config.annotation.RefreshScope; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
@ -40,12 +36,13 @@ import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.*; |
|
|
|
import java.util.List; |
|
|
|
import java.util.*; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
@Service |
|
|
|
@RefreshScope |
|
|
|
public class ModelServiceImpl implements ModelService { |
|
|
|
|
|
|
|
|
|
|
|
@ -60,19 +57,24 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
|
|
|
|
private String hrFtpUrl = "ftp://ftpuser:atia2018@192.168.4.129:10012/"; |
|
|
|
|
|
|
|
// @Value("${file.ftpUrlAddress:test}") |
|
|
|
|
|
|
|
// private String ftpUrlAddress = "192.168.4.129"; |
|
|
|
private String ftpUrlAddress = "192.168.1.116"; |
|
|
|
@Value("${sftp.ip}") |
|
|
|
private String ftpUrlAddress; |
|
|
|
|
|
|
|
// @Value("${file.ftpUrlAccount:test}") |
|
|
|
private String ftpUrlAccount = "ftpuser"; |
|
|
|
@Value("${sftp.username}") |
|
|
|
private String ftpUrlAccount; |
|
|
|
|
|
|
|
// @Value("${file.ftpUrlPwd:test}") |
|
|
|
private String ftpUrlPwd = "atia2018"; |
|
|
|
@Value("${sftp.password}") |
|
|
|
private String ftpUrlPwd; |
|
|
|
|
|
|
|
// @Value("${file.ftpUrlPort:10000}") |
|
|
|
// private Integer ftpUrlPort = 10012; |
|
|
|
private Integer ftpUrlPort = 10990; |
|
|
|
@Value("${sftp.port}") |
|
|
|
private Integer ftpUrlPort; |
|
|
|
|
|
|
|
@Value("${scheduled.task.AIQuery:这是什么,输出字符串格式的回答。}") |
|
|
|
private String AIQuery; |
|
|
|
|
|
|
|
@Resource |
|
|
|
private AnalysisRemoteService analysisRemoteService; |
|
|
|
@ -1043,7 +1045,7 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
sessionId, |
|
|
|
escapeJsonString(textContent) |
|
|
|
); |
|
|
|
|
|
|
|
log.debug("智能体请求内容: {}", requestBody); |
|
|
|
URL url = new URL("http://25.90.210.236/lmp-cloud-ias-server/api/session/run"); |
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
|
|
|
|
|
|
|
@ -1061,6 +1063,7 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
} |
|
|
|
|
|
|
|
int responseCode = connection.getResponseCode(); |
|
|
|
log.debug("智能体响应状态码: {}", responseCode); |
|
|
|
if (responseCode == HttpURLConnection.HTTP_OK) { |
|
|
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) { |
|
|
|
StringBuilder response = new StringBuilder(); |
|
|
|
@ -1068,6 +1071,7 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
while ((responseLine = br.readLine()) != null) { |
|
|
|
response.append(responseLine.trim()); |
|
|
|
} |
|
|
|
log.info("智能体响应内容: {}", response); |
|
|
|
return parseAnswer(response.toString()); |
|
|
|
} |
|
|
|
} else { |
|
|
|
@ -1078,15 +1082,68 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private String parseAnswer(String responseBody) { |
|
|
|
if (responseBody.contains("\"answer\"")) { |
|
|
|
String answer = responseBody.split("\"answer\":\"")[1].split("\"")[0]; |
|
|
|
return answer.replace("\\\"", "\"").replace("\\\\", "\\"); |
|
|
|
} else { |
|
|
|
throw new RuntimeException("获取answer失败: " + responseBody); |
|
|
|
private static String parseAnswer(String responseBody) { |
|
|
|
try { |
|
|
|
ObjectMapper mapper = new ObjectMapper(); |
|
|
|
JsonNode rootNode = mapper.readTree(responseBody); |
|
|
|
|
|
|
|
JsonNode dataNode = rootNode.path("data"); |
|
|
|
if (dataNode.isMissingNode()) { |
|
|
|
throw new IllegalArgumentException("智能体 Response missing 'data' field"); |
|
|
|
} |
|
|
|
|
|
|
|
JsonNode answerNode = dataNode.path("answer"); |
|
|
|
if (answerNode.isMissingNode()) { |
|
|
|
throw new IllegalArgumentException("智能体 Data missing 'answer' field"); |
|
|
|
} |
|
|
|
|
|
|
|
// 处理answer可能是String或Array的情况 |
|
|
|
if (answerNode.isArray() && !answerNode.isEmpty()) { |
|
|
|
// 取第一个元素并尝试解析为字符串 |
|
|
|
JsonNode firstElement = answerNode.get(0); |
|
|
|
if (firstElement.isTextual()) { |
|
|
|
return firstElement.asText(); |
|
|
|
} else { |
|
|
|
// 如果数组元素不是文本,尝试转换为字符串表示 |
|
|
|
return firstElement.toString(); |
|
|
|
} |
|
|
|
} else if (answerNode.isTextual()) { |
|
|
|
// 尝试解析answer字符串,因为它可能是一个JSON数组的字符串表示 |
|
|
|
String answerText = answerNode.asText(); |
|
|
|
try { |
|
|
|
JsonNode parsedNode = mapper.readTree(answerText); |
|
|
|
if (parsedNode.isArray() && !parsedNode.isEmpty()) { |
|
|
|
JsonNode firstElement = parsedNode.get(0); |
|
|
|
if (firstElement.isTextual()) { |
|
|
|
return firstElement.asText(); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
// 如果不是有效的JSON,直接返回原文本 |
|
|
|
return answerText; |
|
|
|
} |
|
|
|
|
|
|
|
return answerText; |
|
|
|
} else if (answerNode.isObject()) { |
|
|
|
// 如果answer是对象,尝试转换为字符串表示 |
|
|
|
return answerNode.toString(); |
|
|
|
} |
|
|
|
|
|
|
|
throw new IllegalArgumentException("Invalid answer format in response"); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new IllegalArgumentException("Failed to parse answer: " + e.getMessage(), e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
// String responseBody = "{\"code\":\"000000\",\"data\":{\"traceId\":\"732727bd-f8a1-4872-b05d-7abf19dd4152\",\"answer\":\"[\\\"从这张图片中可以看到,这是一个电力设备的局部特写,主要展示的是变压器的瓦斯继电器部分。图片中可以看到两个粉色的阀门,分别连接着管道和电线。这些阀门和管道看起来是正常的,没有明显的破损或泄漏迹象。\\\\n\\\\n然而,从图片中无法判断是否存在其他隐患,例如内部的电气连接是否紧固、绝缘材料是否完好、是否有过热现象等这些都需要通过专业的检测工具和技术手段来进一步确认。\\\\n\\\\n此外,图片中显示的环境看起来比较干净,没有明显的灰尘或污垢积累,这也有助于减少潜在的安全隐患。但是,仍然建议定期进行设备的维护和检查,以确保设备的正常运行和安全。\\\\n\\\\n总的来说,从这张图片中无法直接判断出具体的隐患,但保持设备的清洁和定期维护是预防隐患的重要措施。\\\"]\",\"messageId\":\"88724767-fb52-414b-be15-d5d529290ce1\",\"isSensitiveWord\": false, \"isEnd\" : true, \"metadata\" :{}},\"success\" : true, \"message\":\"操作成功\"}"; |
|
|
|
String responseBody = "{\"code\":\"000000\",\"data\":{\"traceId\":\"9bc04bf4-f185-40da-9459-6c7e0253c3c8\",\"answer\":\"出于内容规范和平台规则的要求,我无法对敏感内容进行回应。建议你更换为其他合法合规、积极健康的话题,我们可以围绕内容展开愉快的交流。\",\"conversationId\":\"84ff30c6-79c0-41e2-a47e-1fbee858a5cb\",\"isSensitiveWord\" :true, \"isEnd\" :true},\"success\" : true, \"message\":\"操作成功\"}"; |
|
|
|
String s = parseAnswer(responseBody); |
|
|
|
System.out.println(s); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//转义 |
|
|
|
private String escapeJsonString(String input) { |
|
|
|
return input.replace("\\", "\\\\") |
|
|
|
@ -1108,9 +1165,8 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
log.info("会话创建成功,sessionId: " + sessionId); |
|
|
|
|
|
|
|
// 2. 运行会话 |
|
|
|
log.info("正在向大模型提问..."); |
|
|
|
log.info("正在向大模型提问..." + AIQuery); |
|
|
|
//String query = "#角色 你是电力缺陷识别专家,擅长识别并框选设备缺陷、环境异常等。#功能 根据我发送给你的图像,识别一下图像中的信息,给出缺陷或异常的描述。"; |
|
|
|
String query ="这张图片里有什么?"; |
|
|
|
// String base64String = convertToBase64(imgUrl); |
|
|
|
|
|
|
|
// 转换图片为base64 |
|
|
|
@ -1118,7 +1174,7 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
byte[] fileBytes = IOUtils.toByteArray(inputStream); |
|
|
|
String base64String = Base64.getEncoder().encodeToString(fileBytes); |
|
|
|
if (base64String != null) { |
|
|
|
answer = runSession(sessionId, query, base64String); |
|
|
|
answer = runSession(sessionId, AIQuery, base64String); |
|
|
|
log.info("大模型回答: " + answer); |
|
|
|
} else { |
|
|
|
log.info(imgUrl + "图片转base64未成功!"); |
|
|
|
@ -1132,8 +1188,6 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
|
|
|
|
@Override |
|
|
|
public int agentInquiry(String analyseRequestStr) { |
|
|
|
String answer=""; |
|
|
|
ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
List<VisualJson> visualJsonList = new ArrayList<>(); |
|
|
|
log.info(Color.MAGENTA + "[Agent Inquiry ShaoXing] agentInquiry: analyseRequestStr={}", analyseRequestStr); |
|
|
|
AnalyseRequest analyseRequest = new Gson().fromJson(analyseRequestStr, AnalyseRequest.class); |
|
|
|
@ -1143,16 +1197,6 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
//调用绍兴-智能体 |
|
|
|
String answerResult = this.sessionAnswer(imgUrl); |
|
|
|
log.info("调用绍兴智能体响应结果:" + answerResult); |
|
|
|
if (StringUtils.isNotEmpty(answerResult)){ |
|
|
|
JsonNode jsonNode = null; |
|
|
|
try { |
|
|
|
//获取answer-大模型回答 |
|
|
|
jsonNode = objectMapper.readTree(answerResult); |
|
|
|
answer = jsonNode.get("data").get("answer").asText(); |
|
|
|
} catch (JsonProcessingException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
//返回结构体 |
|
|
|
String[] typeList = analyseRequest.getObjectList().get(0).getTypeList(); |
|
|
|
for (int i = 0; i < typeList.length; i++) { |
|
|
|
@ -1165,7 +1209,7 @@ public class ModelServiceImpl implements ModelService { |
|
|
|
visualJson.setBusinessId(lineId); |
|
|
|
visualJson.setUseType(typeList[i]); |
|
|
|
//answer |
|
|
|
visualJson.setDescription(answer); |
|
|
|
visualJson.setDescription(answerResult); |
|
|
|
visualJsonList.add(visualJson); |
|
|
|
} |
|
|
|
return resultAnalysisMapper.addVisualModelInfo(visualJsonList); |
|
|
|
|