Browse Source

update: 智能问答增加调用知识库问答接口

zhongwei
liwen 6 months ago
parent
commit
89ec1c4bf6
  1. 1
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/BigModelInvokeUrl.java
  2. 13
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java
  3. 25
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java
  4. 18
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java

1
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/configuration/BigModelInvokeUrl.java

@ -27,4 +27,5 @@ public class BigModelInvokeUrl {
private String updateCanvas; private String updateCanvas;
private String assistantKnowledgeAsk;
} }

13
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java

@ -2,11 +2,8 @@ package com.hnac.hzims.bigmodel.interactive.service;
import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq; import com.hnac.hzims.bigmodel.interactive.req.ModelFunctionReq;
import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO; import com.hnac.hzims.bigmodel.interactive.vo.AnswerVO;
import io.swagger.annotations.ApiParam;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,4 +30,14 @@ public interface IInteractiveService {
Boolean authentication(String stationId, String userId, String func); Boolean authentication(String stationId, String userId, String func);
Boolean removeSessionId(String sessionId); Boolean removeSessionId(String sessionId);
/**
* 发起知识库问答
* @param question 问题
* @param sessionId 会话id
* @param userId 用户id
* @param knowledge 知识库名称
* @return 发起结果
*/
R knowledgeAsk(String question, String sessionId, String userId, String knowledge);
} }

25
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java

@ -246,6 +246,31 @@ public class InteractiveServiceImpl implements IInteractiveService {
return true; return true;
} }
/**
* 发起知识库问答
*/
@Override
public R knowledgeAsk(String question, String sessionId, String userId, String knowledge) {
Map<String,Object> params = new HashMap<>();
params.put("id", sessionId);
params.put("userid", userId);
params.put("query", question);
params.put("knowledge", knowledge);
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.getAssistantKnowledgeAsk())
.body(JSON.toJSONString(params)).execute();
logger.info("interactive:knowledgeAsk", "知识库问答传参为:" + JSON.toJSONString(params) + " 结果为:" + response.body());
if (response.getStatus() != HttpServletResponse.SC_OK) {
log.error("远程调用大模型【发起知识库问答】接口失败!");
return R.fail("远程调用大模型【发起知识库问答】接口失败!");
}
this.addQuestionSessionId(sessionId);
return R.success("消息发送成功");
}
private List<DeptStationDTO> getStationPermissionsById(String userId) { private List<DeptStationDTO> getStationPermissionsById(String userId) {
R<List<DeptStationDTO>> deptSattionR = deptClient.getStationPermissionsById(Long.valueOf(userId)); R<List<DeptStationDTO>> deptSattionR = deptClient.getStationPermissionsById(Long.valueOf(userId));
Assert.isTrue(deptSattionR.isSuccess() && CollectionUtil.isNotEmpty(deptSattionR.getData()),() -> { Assert.isTrue(deptSattionR.isSuccess() && CollectionUtil.isNotEmpty(deptSattionR.getData()),() -> {

18
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java

@ -62,20 +62,26 @@ public class InteractiveHandler extends TextWebSocketHandler {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
}else { } else {
String sessionId = InteractiveSessionManager.getEntryBySession(session).getKey(); String sessionId = InteractiveSessionManager.getEntryBySession(session).getKey();
this.handleMessage(messageJSON,sessionId); this.handleMessage(messageJSON, sessionId);
} }
} }
private void handleMessage(JSONObject messageContext,String sessionId) { private void handleMessage(JSONObject messageContext, String sessionId) {
Boolean isSpecial = messageContext.getBoolean("isSpecial"); Boolean isSpecial = messageContext.getBoolean("isSpecial");
if(Func.isEmpty(isSpecial) || !isSpecial) { if (Func.isEmpty(isSpecial) || !isSpecial) {
String context = messageContext.getString("context"); String context = messageContext.getString("context");
String userId = messageContext.getString("userId"); String userId = messageContext.getString("userId");
String knowledge = messageContext.getString("knowledge");
IInteractiveService interactiveService = SpringUtil.getBean(IInteractiveService.class); IInteractiveService interactiveService = SpringUtil.getBean(IInteractiveService.class);
R askResult = interactiveService.ask(context, sessionId, userId); if (knowledge != null) {
log.info("返回结果为:"+ JSON.toJSONString(askResult)); R askResult = interactiveService.knowledgeAsk(context, sessionId, userId, knowledge);
log.info("知识库问答返回结果:" + JSON.toJSONString(askResult));
} else {
R askResult = interactiveService.ask(context, sessionId, userId);
log.info("返回结果为:"+ JSON.toJSONString(askResult));
}
} }
else { else {
String funcCode = messageContext.getString("funcCode"); String funcCode = messageContext.getString("funcCode");

Loading…
Cancel
Save