Browse Source

add:智谱大模型对接

zhongwei
luyie 3 months ago
parent
commit
dc0bd08cb8
  1. 5
      hzims-service-api/target/classes/META-INF/build-info.properties
  2. 5
      hzims-service/hzims-big-model/pom.xml
  3. 26
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java
  4. 20
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java
  5. 27
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisModel.java
  6. 32
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java
  7. 21
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/BaseEnum.java
  8. 36
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuController.java
  9. 25
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java
  10. 23
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java
  11. 28
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java
  12. 37
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java
  13. 95
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerGlm4v.java
  14. 57
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerTest.java
  15. 16
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/ZhipuAnalysisService.java
  16. 44
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java
  17. 37
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisRequest.java
  18. 17
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResponse.java

5
hzims-service-api/target/classes/META-INF/build-info.properties

@ -0,0 +1,5 @@
build.artifact=hzims-service-api
build.group=com.hnac.hzims
build.name=hzims-service-api
build.time=2024-08-12T06\:09\:01.772Z
build.version=4.0.0-SNAPSHOT

5
hzims-service/hzims-big-model/pom.xml

@ -78,6 +78,11 @@
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.5.0</version> <version>3.5.0</version>
</dependency> </dependency>
<dependency>
<groupId>cn.bigmodel.openapi</groupId>
<artifactId>oapi-java-sdk</artifactId>
<version>release-V4-2.0.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

26
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfig.java

@ -0,0 +1,26 @@
package com.hnac.hzims.bigmodel.zhipuai.config;
import com.zhipu.oapi.ClientV4;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* @Author: ypj
* @Date: 2024/8/9 17:01
*/
@Configuration
@EnableConfigurationProperties(ZhipuAiConfigProperties.class)
@AllArgsConstructor
public class ZhipuAiConfig {
private ZhipuAiConfigProperties properties;
@Bean("clientV4")
public ClientV4 clientV4() {
return new ClientV4.Builder(properties.getApiSecret()).build();
}
}

20
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/config/ZhipuAiConfigProperties.java

@ -0,0 +1,20 @@
package com.hnac.hzims.bigmodel.zhipuai.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
/**
* @Author: ypj
* @Date: 2024/8/8 14:03
*/
@Data
@ConfigurationProperties(prefix = "zhipuai")
public class ZhipuAiConfigProperties {
private String apiSecret;
private String url;
}

27
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisModel.java

@ -0,0 +1,27 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
import lombok.Getter;
/**
* @Author: ypj
* @Date: 2024/8/9 9:50
*/
@Getter
public enum AnalysisModel implements BaseEnum {
GLM_4("glm-4", "GLM-4模型"),
GLM_4V("glm-4v", "GLM-4V模型"),
COG_VIDEO_X("cogvideox", "CogVideoX模型"),
COG_VIEW("cogvideox", "CogVideoX模型"),
GLM_4_ALL_TOOLS("glm-4-alltool", "GLM-4-AllTools模型");
private final String code;
private final String msg;
AnalysisModel(String code, String msg) {
this.code = code;
this.msg = msg;
}
}

32
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/AnalysisResultType.java

@ -0,0 +1,32 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
import lombok.Getter;
import org.apache.poi.ss.formula.functions.T;
import java.math.BigDecimal;
/**
* @Author: ypj
* @Date: 2024/8/8 15:27
*/
@Getter
public enum AnalysisResultType implements BaseEnum {
BOOLEAN("BOOLEAN", "是非判断", "是", Boolean.class),
NUMBER("NUMBER", "确定数量", null, Integer.class),
DECIMAL("DECIMAL", "获取数值", null, BigDecimal.class);
private final String code;
private final String msg;
private final String condition;
private final Class<T> clazz;
AnalysisResultType(String code, String msg, String condition, Class clazz) {
this.code = code;
this.msg = msg;
this.condition = condition;
this.clazz = clazz;
}
}

21
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/constants/BaseEnum.java

@ -0,0 +1,21 @@
package com.hnac.hzims.bigmodel.zhipuai.constants;
/**
* @Author: ypj
* @Date: 2024/8/8 15:42
*/
public interface BaseEnum {
<U extends Object> U getCode();
String getMsg();
static <T extends BaseEnum, U extends Object> T getInstance(U code, Class<T> clazz) {
T[] baseEnums = clazz.getEnumConstants();
for (T baseEnum : baseEnums) {
if (baseEnum.getCode().equals(code)) {
return baseEnum;
}
}
return null;
}
}

36
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/controller/ZhipuController.java

@ -0,0 +1,36 @@
package com.hnac.hzims.bigmodel.zhipuai.controller;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hnac.hzims.bigmodel.zhipuai.service.ZhipuAnalysisService;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisRequest;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* @Author: ypj
* @Date: 2024/8/8 16:36
*/
@RestController
@RequestMapping("zhipu")
@Api(value = "智谱", tags = "智谱")
@AllArgsConstructor
public class ZhipuController {
private final ZhipuAnalysisService analysisService;
@PostMapping("/analysis")
@ApiOperation("分析")
@ApiOperationSupport(order = 1)
public R<ZhipuAnalysisResponse> analysis(@RequestBody @Valid ZhipuAnalysisRequest request) {
return R.data(analysisService.analysis(request));
}
}

25
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/entity/ZhipuAnalysisInfoEntity.java

@ -0,0 +1,25 @@
package com.hnac.hzims.bigmodel.zhipuai.entity;
import lombok.Data;
import org.springblade.core.mp.base.BaseEntity;
/**
* @Author: ypj
* @Date: 2024/8/8 15:17
*/
@Data
public class ZhipuAnalysisInfoEntity extends BaseEntity {
private String resultType;
private String name;
private String code;
private String model;
private String requestContent;
private String resultStrategy;
}

23
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/AbstractZhipuAnalysisHandler.java

@ -0,0 +1,23 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.zhipu.oapi.ClientV4;
import javax.annotation.Resource;
import java.util.Base64;
/**
* @Author: ypj
* @Date: 2024/8/12 8:44
*/
public abstract class AbstractZhipuAnalysisHandler implements ZhipuAnalyser {
protected final String requestIdTemplate = "hzims-%d";
protected final String resultKey = "结果";
@Resource
protected ClientV4 client;
@Override
public String getBase64(String url) {
return Base64.getEncoder().encodeToString(getFileBytes(url));
}
}

28
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalyser.java

@ -0,0 +1,28 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
/**
* @Author: ypj
* @Date: 2024/8/8 14:45
*/
public interface ZhipuAnalyser<Req, Resp> {
String getBase64(String url);
byte[] getFileBytes(String url);
String getAnalysisStrategy();
String getAnalysisModel();
default boolean isSupport(String modelName) {
return getAnalysisStrategy().equals(modelName);
}
Resp sendRequest(ZhipuAnalysisInfoEntity info, String url);
Req getRequest(ZhipuAnalysisInfoEntity info, String url);
String getResult(ZhipuAnalysisInfoEntity info, String url);
}

37
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisFactory.java

@ -0,0 +1,37 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author: ypj
* @Date: 2024/8/9 10:47
*/
@Component
@AllArgsConstructor
@Slf4j
public class ZhipuAnalysisFactory {
private final List<ZhipuAnalyser> analysisStrategyList;
private static final Map<String, ZhipuAnalyser> analysisStrategyMap = new ConcurrentHashMap<>();
public ZhipuAnalyser getAnalysisStrategy(final String modelName) {
ZhipuAnalyser result = ZhipuAnalysisFactory.analysisStrategyMap.get(modelName);
if (Objects.nonNull(result)) {
return result;
}
Optional<ZhipuAnalyser> strategyOptional = analysisStrategyList.stream().filter(strategy -> strategy.isSupport(modelName)).findFirst();
if (strategyOptional.isPresent()) {
result = strategyOptional.get();
ZhipuAnalysisFactory.analysisStrategyMap.put(modelName, result);
}
return result;
}
}

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

@ -0,0 +1,95 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.constants.AnalysisModel;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
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.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: ypj
* @Date: 2024/8/12 9:20
*/
@Service
@AllArgsConstructor
@Slf4j
public class ZhipuAnalysisHandlerGlm4v extends AbstractZhipuAnalysisHandler {
@Override
public String getAnalysisStrategy() {
return AnalysisModel.GLM_4V.getCode();
}
@Override
public String getAnalysisModel() {
return AnalysisModel.GLM_4V.getCode();
}
@Override
public byte[] getFileBytes(String url) {
return new byte[0];
}
@Override
public ModelApiResponse sendRequest(ZhipuAnalysisInfoEntity info, String url) {
ChatCompletionRequest request = getRequest(info, url);
log.info("send zhipu ai request :{}", request);
return client.invokeModelApi(request);
}
@Override
public ChatCompletionRequest getRequest(ZhipuAnalysisInfoEntity info, String url) {
List<ChatMessage> messages = new ArrayList<>();
List<Map<String, Object>> contentList = new ArrayList<>();
Map<String, Object> textMap = new HashMap<>();
textMap.put("type", "text");
textMap.put("text", info.getRequestContent());
Map<String, Object> typeMap = new HashMap<>();
typeMap.put("type", "image_url");
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("url", getBase64(url));
typeMap.put("image_url", urlMap);
contentList.add(textMap);
contentList.add(typeMap);
ChatMessage chatMessage = new ChatMessage(ChatMessageRole.USER.value(), contentList);
messages.add(chatMessage);
String requestId = String.format(requestIdTemplate, System.currentTimeMillis());
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model(getAnalysisModel())
.stream(Boolean.FALSE)
.invokeMethod(Constants.invokeMethod)
.messages(messages)
.requestId(requestId)
.build();
return request;
}
@Override
public String getResult(ZhipuAnalysisInfoEntity info, String url) {
ModelApiResponse response = sendRequest(info, url);
log.info("get zhi pu ai response : {}", JsonUtil.toJson(response));
if (response.isSuccess()) {
String resultJson = response.getData().getChoices().get(0).getMessage().getContent().toString();
resultJson = resultJson.substring(resultJson.indexOf("{"), resultJson.indexOf("}") + 1).trim();
Map<String, Object> resultMap = JsonUtil.toMap(resultJson);
return resultMap.get(resultKey).toString();
} else {
return "访问失败";
}
}
}

57
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/handler/ZhipuAnalysisHandlerTest.java

@ -0,0 +1,57 @@
package com.hnac.hzims.bigmodel.zhipuai.handler;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.ChatCompletionRequest;
import com.zhipu.oapi.service.v4.model.ChatMessage;
import com.zhipu.oapi.service.v4.model.ChatMessageRole;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: ypj
* @Date: 2024/8/12 10:11
*/
@Service
@AllArgsConstructor
@Slf4j
public class ZhipuAnalysisHandlerTest extends ZhipuAnalysisHandlerGlm4v {
private final String model = "test";
@Override
public String getAnalysisStrategy() {
return model;
}
@Override
public byte[] getFileBytes(String url) {
File file = new File(url);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
fileInputStream.read(bytes);
return bytes;
} catch (Exception e) {
log.error("读取文件出错", e);
} finally {
if (null != fileInputStream) {
try {
fileInputStream.close();
} catch (IOException ignore) {
}
}
}
return null;
}
}

16
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/ZhipuAnalysisService.java

@ -0,0 +1,16 @@
package com.hnac.hzims.bigmodel.zhipuai.service;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisRequest;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisResponse;
/**
* @Author: ypj
* @Date: 2024/8/12 10:25
*/
public interface ZhipuAnalysisService {
ZhipuAnalysisResponse analysis(ZhipuAnalysisRequest request);
ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisRequest request);
}

44
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/service/impl/ZhipuAnalysisServiceImpl.java

@ -0,0 +1,44 @@
package com.hnac.hzims.bigmodel.zhipuai.service.impl;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalyser;
import com.hnac.hzims.bigmodel.zhipuai.handler.ZhipuAnalysisFactory;
import com.hnac.hzims.bigmodel.zhipuai.service.ZhipuAnalysisService;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisRequest;
import com.hnac.hzims.bigmodel.zhipuai.vo.ZhipuAnalysisResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Objects;
/**
* @Author: ypj
* @Date: 2024/8/12 10:26
*/
@Service
@AllArgsConstructor
@Slf4j
public class ZhipuAnalysisServiceImpl implements ZhipuAnalysisService {
private final ZhipuAnalysisFactory analysisFactory;
@Override
public ZhipuAnalysisResponse analysis(ZhipuAnalysisRequest request) {
ZhipuAnalysisInfoEntity info = getAnalysisInfo(request);
ZhipuAnalyser analyser = analysisFactory.getAnalysisStrategy(info.getModel());
String resultStr = analyser.getResult(info, request.getUrl());
ZhipuAnalysisResponse response = new ZhipuAnalysisResponse();
response.setResultStr(resultStr);
return response;
}
@Override
public ZhipuAnalysisInfoEntity getAnalysisInfo(ZhipuAnalysisRequest request) {
if (Objects.isNull(request.getCode())) {
return request.toAnalysisInfo();
}
return null;
}
}

37
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisRequest.java

@ -0,0 +1,37 @@
package com.hnac.hzims.bigmodel.zhipuai.vo;
import com.hnac.hzims.bigmodel.zhipuai.entity.ZhipuAnalysisInfoEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @Author: ypj
* @Date: 2024/8/12 10:23
*/
@Data
@ApiModel(value = "智谱分析请求", description = "智谱分析请求")
public class ZhipuAnalysisRequest {
@ApiModelProperty("模型编码")
private String code;
@ApiModelProperty("模型名称")
private String model;
@ApiModelProperty("文件地址")
private String url;
@ApiModelProperty("发送内容")
private String content;
@ApiModelProperty("额外信息")
private String message;
public ZhipuAnalysisInfoEntity toAnalysisInfo() {
ZhipuAnalysisInfoEntity info = new ZhipuAnalysisInfoEntity();
info.setModel(this.getModel());
info.setRequestContent(this.content);
info.setModel(this.getModel());
return info;
}
}

17
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/zhipuai/vo/ZhipuAnalysisResponse.java

@ -0,0 +1,17 @@
package com.hnac.hzims.bigmodel.zhipuai.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @Author: ypj
* @Date: 2024/8/12 10:23
*/
@Data
@ApiModel(value = "智谱分析返回",description = "智谱分析返回")
public class ZhipuAnalysisResponse {
@ApiModelProperty("返回结果")
String resultStr;
}
Loading…
Cancel
Save