|
|
|
@ -7,21 +7,22 @@ import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
|
import com.hnac.hzims.bigmodel.configuration.BigModelInvokeUrl; |
|
|
|
|
import com.hnac.hzims.bigmodel.entity.FunctionEntity; |
|
|
|
|
import com.hnac.hzims.bigmodel.function.service.IFunctionService; |
|
|
|
|
import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq; |
|
|
|
|
import com.hnac.hzims.bigmodel.interactive.service.IInteractiveService; |
|
|
|
|
import com.hnac.hzims.bigmodel.interactive.service.IJumpPageService; |
|
|
|
|
import com.hnac.hzims.bigmodel.function.service.IFunctionService; |
|
|
|
|
import com.hnac.hzims.bigmodel.interactive.service.IParamsService; |
|
|
|
|
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.springblade.core.log.exception.ServiceException; |
|
|
|
|
import org.springblade.core.log.logger.BladeLogger; |
|
|
|
|
import org.springblade.core.secure.utils.AuthUtil; |
|
|
|
|
import org.springblade.core.tool.api.R; |
|
|
|
|
import org.springblade.core.tool.utils.CollectionUtil; |
|
|
|
|
import org.springblade.core.tool.utils.Func; |
|
|
|
|
import org.springblade.core.tool.utils.ObjectUtil; |
|
|
|
|
import org.springblade.core.tool.utils.StringUtil; |
|
|
|
|
import org.springblade.system.dto.DeptStationDTO; |
|
|
|
|
import org.springblade.system.feign.IDeptClient; |
|
|
|
|
import org.springblade.system.user.feign.IUserClient; |
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
@ -29,15 +30,13 @@ import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import javax.servlet.http.HttpSession; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.TypeEnum; |
|
|
|
|
import static com.hnac.hzims.bigmodel.schedule.XxlJobHandlerConstant.HZIMS_BIGMODEL_ASK_KEY; |
|
|
|
|
|
|
|
|
|
import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.*; |
|
|
|
|
/** |
|
|
|
|
* @Author: huangxing |
|
|
|
|
* @Date: 2024/04/26 14:51 |
|
|
|
@ -47,12 +46,24 @@ import static com.hnac.hzims.bigmodel.interactive.constants.FunctionConstants.*;
|
|
|
|
|
@RequiredArgsConstructor |
|
|
|
|
public class InteractiveServiceImpl implements IInteractiveService { |
|
|
|
|
|
|
|
|
|
private final BladeLogger logger; |
|
|
|
|
|
|
|
|
|
private final IJumpPageService jumpPageService; |
|
|
|
|
|
|
|
|
|
private final IParamsService paramsService; |
|
|
|
|
|
|
|
|
|
private final IFunctionService functionService; |
|
|
|
|
|
|
|
|
|
private final BigModelInvokeUrl bigModelInvokeUrl; |
|
|
|
|
private final BladeLogger logger; |
|
|
|
|
|
|
|
|
|
private final IDeptClient deptClient; |
|
|
|
|
|
|
|
|
|
private final IUserClient userClient; |
|
|
|
|
|
|
|
|
|
private final RedisTemplate redisTemplate; |
|
|
|
|
|
|
|
|
|
private final ExtraResolveStrategyService extraResolveStrategyService; |
|
|
|
|
|
|
|
|
|
@Value("${fdp.host}") |
|
|
|
|
private String fdpHost; |
|
|
|
|
|
|
|
|
@ -64,11 +75,14 @@ public class InteractiveServiceImpl implements IInteractiveService {
|
|
|
|
|
switch (typeEnum) { |
|
|
|
|
// 页面跳转
|
|
|
|
|
case JUMP: |
|
|
|
|
String extra = jumpPageService.dealJumpTypeFunction(function, req.getFunctionArgs()); |
|
|
|
|
Assert.isTrue(StringUtil.isNotBlank(extra) && Func.isNotEmpty(extra), () -> { |
|
|
|
|
String jumpExtra = jumpPageService.dealJumpTypeFunction(function, req.getFunctionArgs()); |
|
|
|
|
Assert.isTrue(StringUtil.isNotBlank(jumpExtra) && Func.isNotEmpty(jumpExtra), () -> { |
|
|
|
|
throw new ServiceException("解析" + function.getName() + "函数失败!"); |
|
|
|
|
}); |
|
|
|
|
return R.data(extra); |
|
|
|
|
return R.data(jumpExtra); |
|
|
|
|
case PARAMS: |
|
|
|
|
String paramExtra = paramsService.dealJumpTypeFunction(function, req.getFunctionArgs()); |
|
|
|
|
return R.data(paramExtra); |
|
|
|
|
default: |
|
|
|
|
throw new ServiceException("函数解析失败!"); |
|
|
|
|
} |
|
|
|
@ -76,21 +90,77 @@ public class InteractiveServiceImpl implements IInteractiveService {
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public R ask(String question,String sessionId,String userId) { |
|
|
|
|
//TODO 保存问题
|
|
|
|
|
Map<String,String> params = new HashMap<>(); |
|
|
|
|
Map<String,Object> params = new HashMap<>(); |
|
|
|
|
params.put("id",sessionId); |
|
|
|
|
params.put("userid",userId.toString()); |
|
|
|
|
params.put("userid", userId); |
|
|
|
|
params.put("query",question); |
|
|
|
|
params.put("stationids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); |
|
|
|
|
params.put("projectids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).map(String::valueOf).toArray()); |
|
|
|
|
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantAsk()) |
|
|
|
|
.body(JSON.toJSONString(params)).execute(); |
|
|
|
|
logger.info("interactive:ask","问答传参为:" + JSON.toJSONString(params) + "结果为:" + response.body()); |
|
|
|
|
if(response.getStatus() != HttpServletResponse.SC_OK) { |
|
|
|
|
log.error("远程调用大模型【发起问答】接口失败!"); |
|
|
|
|
return R.fail("远程调用大模型【发起问答】接口失败!"); |
|
|
|
|
} |
|
|
|
|
this.addQuestionSessionId(sessionId); |
|
|
|
|
return R.success("消息发送成功"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void addQuestionSessionId(String sessionId) { |
|
|
|
|
// 添加redis问题会话
|
|
|
|
|
Object json = redisTemplate.opsForValue().get(HZIMS_BIGMODEL_ASK_KEY); |
|
|
|
|
if(ObjectUtil.isEmpty(json)){ |
|
|
|
|
List<String> asks = Collections.singletonList(sessionId); |
|
|
|
|
redisTemplate.opsForValue().set(HZIMS_BIGMODEL_ASK_KEY,JSONObject.toJSONString(asks)); |
|
|
|
|
}else{ |
|
|
|
|
List<String> asks = JSONObject.parseArray(json.toString(),String.class); |
|
|
|
|
if(!asks.contains(sessionId)){ |
|
|
|
|
asks.add(sessionId); |
|
|
|
|
} |
|
|
|
|
redisTemplate.opsForValue().set(HZIMS_BIGMODEL_ASK_KEY,JSONObject.toJSONString(asks)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public R specialAsk(String sessionId, String userId, Map<String, Object> extra) { |
|
|
|
|
Map<String,Object> params = new HashMap<>(); |
|
|
|
|
params.put("id",sessionId); |
|
|
|
|
params.put("userid", userId); |
|
|
|
|
params.put("extra",extra); |
|
|
|
|
params.put("stationids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId).filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).toArray()); |
|
|
|
|
params.put("projectids",this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getDeptId).filter(Func::isNotEmpty).map(String::valueOf).toArray()); |
|
|
|
|
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantSpecialAsk()) |
|
|
|
|
.body(JSON.toJSONString(params)).execute(); |
|
|
|
|
if(response.getStatus() != HttpServletResponse.SC_OK) { |
|
|
|
|
log.error("远程调用大模型【发起特殊问答】接口失败!"); |
|
|
|
|
return R.fail("远程调用大模型【发起特殊问答】接口失败!"); |
|
|
|
|
} |
|
|
|
|
this.addQuestionSessionId(sessionId); |
|
|
|
|
return R.success("消息发送成功"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void updateVideo(Map<String, Object> request) { |
|
|
|
|
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getUpdateVideo()) |
|
|
|
|
.body(JSON.toJSONString(request)).execute(); |
|
|
|
|
Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK, () -> { |
|
|
|
|
throw new ServiceException("远程调用大模型【更新实时监控】接口失败!"); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Boolean updateCanvas(Map<String, Object> request) { |
|
|
|
|
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getUpdateCanvas()) |
|
|
|
|
.body(JSON.toJSONString(request)).execute(); |
|
|
|
|
if (response.getStatus() == HttpServletResponse.SC_OK) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
log.error("远程调用大模型【更新实时画面】接口失败!"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<AnswerVO> getAnswerBySessionIds(String sessionIds) { |
|
|
|
|
Map<String,Object> params = new HashMap<>(); |
|
|
|
|
params.put("ids",Func.toStrList(",",sessionIds).toArray()); |
|
|
|
@ -100,18 +170,25 @@ public class InteractiveServiceImpl implements IInteractiveService {
|
|
|
|
|
Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK, () -> { |
|
|
|
|
throw new ServiceException("远程调用大模型【获取问题答案】接口失败!"); |
|
|
|
|
}); |
|
|
|
|
return JSONArray.parseArray(response.body(),AnswerVO.class); |
|
|
|
|
|
|
|
|
|
List<AnswerVO> result = JSONArray.parseArray(response.body(), AnswerVO.class); |
|
|
|
|
result.stream().filter(answerVO -> answerVO.getRunning() == 0).forEach(answerVO -> { |
|
|
|
|
Object[] extras = answerVO.getExtra(); |
|
|
|
|
if(ObjectUtil.isEmpty(answerVO.getExtra()) || extras.length == 0){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
List<String> extraList = Arrays.stream(extras).map(Object::toString).map(extraResolveStrategyService::resolve).map(JSON::toJSONString).collect(Collectors.toList()); |
|
|
|
|
answerVO.setExtra(extraList.toArray(new String[extraList.size()])); |
|
|
|
|
}); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Boolean authentication(String stationId, String userId, String funcCode) { |
|
|
|
|
// 站点鉴权
|
|
|
|
|
if(StringUtil.isNotBlank(stationId)) { |
|
|
|
|
R<List<String>> stationsR = deptClient.getStationPermissionsById(Long.valueOf(userId)); |
|
|
|
|
Assert.isTrue(stationsR.isSuccess() && CollectionUtil.isNotEmpty(stationsR.getData()),() -> { |
|
|
|
|
throw new ServiceException("获取人员站点权限失败!"); |
|
|
|
|
}); |
|
|
|
|
List<String> stations = stationsR.getData(); |
|
|
|
|
List<String> stations = this.getStationPermissionsById(userId).stream().map(DeptStationDTO::getStationId) |
|
|
|
|
.filter(StringUtil::isNotBlank).filter(Func::isNotEmpty).collect(Collectors.toList()); |
|
|
|
|
Assert.isTrue(stations.contains(stationId),() -> { |
|
|
|
|
throw new ServiceException("人员站点鉴权失败!"); |
|
|
|
|
}); |
|
|
|
@ -130,4 +207,38 @@ public class InteractiveServiceImpl implements IInteractiveService {
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Boolean removeSessionId(String sessionId) { |
|
|
|
|
// 调用大模型删除对话
|
|
|
|
|
Map<String,Object> params = new HashMap<>(); |
|
|
|
|
params.put("id",sessionId); |
|
|
|
|
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAskAbort()) |
|
|
|
|
.body(JSON.toJSONString(params)).execute(); |
|
|
|
|
if(response.getStatus() != HttpServletResponse.SC_OK) { |
|
|
|
|
log.error("远程调用大模型【删除对话】接口失败!"); |
|
|
|
|
throw new ServiceException("远程调用大模型【删除对话】接口失败!"); |
|
|
|
|
} |
|
|
|
|
// 移除对应reids中问题标记
|
|
|
|
|
Object json = redisTemplate.opsForValue().get(HZIMS_BIGMODEL_ASK_KEY); |
|
|
|
|
if(ObjectUtil.isEmpty(json)) { |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
List<String> sessionIds = JSONObject.parseArray(json.toString(),String.class); |
|
|
|
|
if(CollectionUtil.isEmpty(sessionIds)){ |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
if(sessionIds.contains(sessionId)){ |
|
|
|
|
sessionIds.remove(sessionId); |
|
|
|
|
redisTemplate.opsForValue().set(HZIMS_BIGMODEL_ASK_KEY,JSONObject.toJSONString(sessionIds)); |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<DeptStationDTO> getStationPermissionsById(String userId) { |
|
|
|
|
R<List<DeptStationDTO>> deptSattionR = deptClient.getStationPermissionsById(Long.valueOf(userId)); |
|
|
|
|
Assert.isTrue(deptSattionR.isSuccess() && CollectionUtil.isNotEmpty(deptSattionR.getData()),() -> { |
|
|
|
|
throw new ServiceException("获取人员站点权限失败!"); |
|
|
|
|
}); |
|
|
|
|
return deptSattionR.getData(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|