From 0af4d79174c6914b56ac36dd1bb0313a7358ec73 Mon Sep 17 00:00:00 2001 From: luyie Date: Wed, 25 Sep 2024 20:59:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=A7=86=E9=A2=91=E5=B7=A1=E6=A3=80?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=A7=A3=E6=9E=90=E8=A7=86=E9=A2=91=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/hnac/hzims/common/utils/FileUtil.java | 39 +++++++++++++++++++++- .../zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java | 10 +++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java b/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java index 7b38cfc..f376ac3 100644 --- a/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java +++ b/hzims-biz-common/src/main/java/com/hnac/hzims/common/utils/FileUtil.java @@ -1,9 +1,19 @@ package com.hnac.hzims.common.utils; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NonNull; 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 javax.net.ssl.SSLContext; import java.io.*; import java.net.URL; import java.net.URLDecoder; @@ -147,6 +157,10 @@ public class FileUtil { return getBase64(getFileBytes(filePath)); } + public static String getBase64(URL url) throws IOException { + return getBase64(getUrlByte(url)); + } + @Data @AllArgsConstructor 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; + } } diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java index 8070aa2..5fd8578 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java +++ b/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 com.hnac.gglm.bigmodel.zhipuai.constants.ErrorCode; +import com.hnac.hzims.common.utils.FileUtil; import com.hnac.hzinfo.exception.HzServiceException; import com.zhipu.oapi.Constants; 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.extern.slf4j.Slf4j; import org.springblade.core.tool.jackson.JsonUtil; +import org.springblade.core.tool.utils.StringUtil; import org.springframework.stereotype.Service; import java.util.*; @@ -43,6 +45,12 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { @Override 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 messages = new ArrayList<>(); List> contentList = new ArrayList<>(); Map textMap = new HashMap<>(); @@ -51,7 +59,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { Map typeMap = new HashMap<>(); typeMap.put("type", "image_url"); Map urlMap = new HashMap<>(); - urlMap.put("url", url); + urlMap.put("url", StringUtil.isBlank(base64) ? url : base64); typeMap.put("image_url", urlMap); contentList.add(textMap); contentList.add(typeMap);