Browse Source

add:大模型管理

zhongwei
haungxing 7 months ago
parent
commit
83043a79ad
  1. 4
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java
  2. 2
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/IInteractiveService.java
  3. 21
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/service/impl/InteractiveServiceImpl.java
  4. 3
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java
  5. 5
      hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/websocket/handler/InteractiveHandler.java

4
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/interactive/controller/InteractiveController.java

@ -39,8 +39,8 @@ public class InteractiveController {
@ApiOperation("提问")
@ApiOperationSupport(order = 2)
@GetMapping("/ask")
public R ask(@RequestParam @ApiParam("用户提出问题") String question,@RequestParam @ApiParam("问答sessionId") String sessionId) {
return interactiveService.ask(question, sessionId);
public R ask(@RequestParam @ApiParam("用户提出问题") String question,@RequestParam @ApiParam("问答sessionId") String sessionId,@RequestParam @ApiParam("用户Id") String userId) {
return interactiveService.ask(question, sessionId, userId);
}
@ApiOperation("站点、菜单鉴权")

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

@ -17,7 +17,7 @@ public interface IInteractiveService {
R resolve(ModelFunctionReq req);
R ask(String question,String sessionId);
R ask(String question,String sessionId,String userId);
List<AnswerVO> getAnswerBySessionIds(String sessionIds);

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

@ -16,6 +16,7 @@ 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.Func;
@ -46,11 +47,13 @@ public class InteractiveServiceImpl implements IInteractiveService {
private final IJumpPageService jumpPageService;
private final IFunctionService functionService;
private final BigModelInvokeUrl bigModelInvokeUrl;
private final BladeLogger logger;
@Value("${fdp.host}")
private String fdpHost;
@Override
public R resolve(ModelFunctionReq req) {
logger.info("interactive:resolve","开始解析大模型函数,函数内容为:" + JSON.toJSONString(req));
//TODO 数据鉴权
FunctionEntity function = functionService.getFunctionByCode(req.getFunctionName());
TypeEnum typeEnum = TypeEnum.getTypeEnumByType(function.getType());
@ -68,8 +71,7 @@ public class InteractiveServiceImpl implements IInteractiveService {
}
@Override
public R ask(String question,String sessionId) {
Long userId = AuthUtil.getUserId();
public R ask(String question,String sessionId,String userId) {
//TODO 保存问题
Map<String,String> params = new HashMap<>();
params.put("id",sessionId);
@ -77,10 +79,11 @@ public class InteractiveServiceImpl implements IInteractiveService {
params.put("query",question);
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantAsk())
.body(JSON.toJSONString(params)).execute();
Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK && "1".equals(JSONObject.parseObject(response.body()).getString("success")), () -> {
throw new ServiceException("远程调用大模型【发起问答】接口失败!");
});
return R.data(JSONObject.parseObject(response.body()).getString("data"));
if(response.getStatus() != HttpServletResponse.SC_OK) {
log.error("远程调用大模型【发起问答】接口失败!");
return R.fail("远程调用大模型【发起问答】接口失败!");
}
return R.success("消息发送成功");
}
@Override
@ -89,11 +92,11 @@ public class InteractiveServiceImpl implements IInteractiveService {
params.put("ids",sessionIds);
HttpResponse response = HttpRequest.post(fdpHost + bigModelInvokeUrl.getAssistantStatus())
.body(JSON.toJSONString(params)).execute();
Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK && "1".equals(JSONObject.parseObject(response.body()).getString("success")), () -> {
logger.info("interactive:getAnswerBySessionIds","获取答案:" + response.body());
Assert.isTrue(response.getStatus() == HttpServletResponse.SC_OK, () -> {
throw new ServiceException("远程调用大模型【获取问题答案】接口失败!");
});
String data = JSONObject.parseObject(response.body()).getString("data");
return JSONArray.parseArray(data,AnswerVO.class);
return JSONArray.parseArray(response.body(),AnswerVO.class);
}
@Override

3
hzims-service/hzims-big-model/src/main/java/com/hnac/hzims/bigmodel/schedule/InteractiveSchedule.java

@ -68,9 +68,12 @@ public class InteractiveSchedule {
@XxlJob(GET_INTERACTIVE_RESULT)
public ReturnT execute(String params) {
List<String> sessionIds = InteractiveSessionManager.getSessionIds();
XxlJobLogger.log("开始获取到sessionId:"+sessionIds);
List<AnswerVO> AnswerList = interactiveService.getAnswerBySessionIds(sessionIds.stream().collect(Collectors.joining(",")));
XxlJobLogger.log("获取到答案:"+JSON.toJSONString(AnswerList));
AnswerList.parallelStream().forEach(answerVO -> {
CompletableFuture.runAsync(() -> {
XxlJobLogger.log("开始获取"+answerVO.getSessionId()+"答案!");
WebSocketSession session = InteractiveSessionManager.get(answerVO.getSessionId());
TextMessage message = InteractiveSessionManager.getTextMessage("1",JSON.toJSONString(answerVO));
wsService.sendMessage(session,message);

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

@ -60,9 +60,10 @@ public class InteractiveHandler extends TextWebSocketHandler {
else {
// 发送问题
String context = messageJSON.getString("context");
String userId = messageJSON.getString("userId");
IInteractiveService interactiveService = SpringUtil.getBean(IInteractiveService.class);
R askResult = interactiveService.ask(context, InteractiveSessionManager.getEntryBySession(session).getKey());
log.info("message handle successful!返回结果为:"+ JSON.toJSONString(askResult));
R askResult = interactiveService.ask(context, InteractiveSessionManager.getEntryBySession(session).getKey(), userId);
log.info("返回结果为:"+ JSON.toJSONString(askResult));
}
}
}

Loading…
Cancel
Save