Browse Source

fix:视频巡检无法解析视频的问题

zhongwei
luyie 2 months ago
parent
commit
0af4d79174
  1. 39
      hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java
  2. 10
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java

39
hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java

@ -1,9 +1,19 @@
package com.hnac.hzims.common.utils; package com.hnac.hzims.common.utils;
import lombok.*; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.net.ssl.SSLContext;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
@ -147,6 +157,10 @@ public class FileUtil {
return getBase64(getFileBytes(filePath)); return getBase64(getFileBytes(filePath));
} }
public static String getBase64(URL url) throws IOException {
return getBase64(getUrlByte(url));
}
@Data @Data
@AllArgsConstructor @AllArgsConstructor
public static class ByteArrayMultipartFile implements MultipartFile { public static class ByteArrayMultipartFile implements MultipartFile {
@ -187,4 +201,27 @@ public class FileUtil {
} }
} }
} }
public static byte[] getUrlByteByRequest(String urlString) {
try {
// 创建一个不验证主机名且接受任何证书的SSLContext
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (x509Certificates, s) -> true).build();
// 使用该SSLContext创建一个HttpClient,并禁用主机名验证
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build();
HttpGet request = new HttpGet(urlString);
CloseableHttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
org.apache.http.HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toByteArray(entity);
}
}
} catch (Exception e) {
log.error("获取文件内容失败", e);
}
return null;
}
} }

10
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java

@ -2,6 +2,7 @@ package com.hnac.gglm.bigmodel.zhipuai.handler;
import cn.hutool.core.lang.Assert; import cn.hutool.core.lang.Assert;
import com.hnac.gglm.bigmodel.zhipuai.constants.ErrorCode; import com.hnac.gglm.bigmodel.zhipuai.constants.ErrorCode;
import com.hnac.hzims.common.utils.FileUtil;
import com.hnac.hzinfo.exception.HzServiceException; import com.hnac.hzinfo.exception.HzServiceException;
import com.zhipu.oapi.Constants; import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest; import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
@ -11,6 +12,7 @@ import com.zhipu.oapi.service.v4.model.ModelApiResponse;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.jackson.JsonUtil; import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
@ -43,6 +45,12 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
@Override @Override
public ChatCompletionRequest getRequest(String text, String url) { public ChatCompletionRequest getRequest(String text, String url) {
String base64 = null;
try {
base64 = FileUtil.getBase64(FileUtil.getUrlByteByRequest(url));
} catch (Exception e) {
log.info("get base64 error", e);
}
List<ChatMessage> messages = new ArrayList<>(); List<ChatMessage> messages = new ArrayList<>();
List<Map<String, Object>> contentList = new ArrayList<>(); List<Map<String, Object>> contentList = new ArrayList<>();
Map<String, Object> textMap = new HashMap<>(); Map<String, Object> textMap = new HashMap<>();
@ -51,7 +59,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
Map<String, Object> typeMap = new HashMap<>(); Map<String, Object> typeMap = new HashMap<>();
typeMap.put("type", "image_url"); typeMap.put("type", "image_url");
Map<String, Object> urlMap = new HashMap<>(); Map<String, Object> urlMap = new HashMap<>();
urlMap.put("url", url); urlMap.put("url", StringUtil.isBlank(base64) ? url : base64);
typeMap.put("image_url", urlMap); typeMap.put("image_url", urlMap);
contentList.add(textMap); contentList.add(textMap);
contentList.add(typeMap); contentList.add(typeMap);

Loading…
Cancel
Save