|
|
@ -1,5 +1,6 @@ |
|
|
|
package com.hnac.hzims.bigmodel.zhipuai.handler; |
|
|
|
package com.hnac.hzims.bigmodel.zhipuai.handler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.lang.Assert; |
|
|
|
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; |
|
|
@ -35,7 +36,7 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public ModelApiResponse sendRequest(String text, String url) { |
|
|
|
public ModelApiResponse sendRequest(String text, String url) { |
|
|
|
ChatCompletionRequest request = getRequest(text, url); |
|
|
|
ChatCompletionRequest request = getRequest(text, url); |
|
|
|
log.info("send zhipu ai request :{}", Optional.ofNullable(request).map(req -> JsonUtil.toJson(req)).orElse("null")); |
|
|
|
log.info("send zhipu ai request :{}", Optional.ofNullable(request).map(JsonUtil::toJson).orElse("null")); |
|
|
|
return client.invokeModelApi(request); |
|
|
|
return client.invokeModelApi(request); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -70,19 +71,18 @@ public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public String getResult(String text, String url) { |
|
|
|
public String getResult(String text, String url) { |
|
|
|
ModelApiResponse response = sendRequest(text, url); |
|
|
|
ModelApiResponse response = sendRequest(text, url); |
|
|
|
log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(resp -> JsonUtil.toJson(resp)).orElse("null")); |
|
|
|
log.info("get zhi pu ai response : {}", Optional.ofNullable(response).map(JsonUtil::toJson).orElse("null")); |
|
|
|
if (response.isSuccess()) { |
|
|
|
Assert.<HzServiceException>isTrue(Objects.nonNull(response) && response.isSuccess(), () -> { |
|
|
|
|
|
|
|
throw new HzServiceException(response.getMsg()); |
|
|
|
|
|
|
|
}); |
|
|
|
try { |
|
|
|
try { |
|
|
|
String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); |
|
|
|
String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString(); |
|
|
|
resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.lastIndexOf("}") + 1).trim(); |
|
|
|
resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.lastIndexOf("}") + 1).trim(); |
|
|
|
return resultJson; |
|
|
|
return resultJson; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.info("get data from response error", e); |
|
|
|
log.info("get data from response error", e); |
|
|
|
|
|
|
|
throw new HzServiceException("获取数据失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
throw new HzServiceException(response.getMsg()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|