rasa 优化训练和启动接口

dev_v1.0.1
xueqingkun 2 years ago
parent b9e66386a5
commit 3b2d6c3adb

@ -0,0 +1,102 @@
package com.superversion.rasa.config;
import cn.hutool.json.JSONUtil;
import com.superversion.rasa.domian.GlobalResult;
import com.superversion.rasa.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.util.Objects;
import java.util.stream.Collectors;
/**
*
*
* @author wb
* @date 2022/3/10 13:24
*/
@Slf4j
@RestControllerAdvice(annotations = RestController.class, basePackages = {"com.**.controller"})
public class ResponseConfig implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(@Nullable MethodParameter methodParameter,
@Nullable Class<? extends HttpMessageConverter<?>> aClass) {
assert methodParameter != null;
return !methodParameter.getDeclaringClass().getName().contains("swagger");
}
@Override
public Object beforeBodyWrite(Object o, @Nullable MethodParameter methodParameter, @Nullable MediaType mediaType,
@Nullable Class<? extends HttpMessageConverter<?>> aClass, @Nullable ServerHttpRequest serverHttpRequest,
@Nullable ServerHttpResponse serverHttpResponse) {
if (Objects.isNull(o)) {
return JSONUtil.toJsonStr(GlobalResult.ok(null, "success"));
}
if (o instanceof GlobalResult) {
return o;
}
// 对于String类型的返回值需要进行特殊处理
if (o instanceof String) {
return JSONUtil.toJsonStr(GlobalResult.ok(o, "success"));
}
return GlobalResult.ok(o, "success");
}
/**
*
*
* @param exception
* @return
*/
@ExceptionHandler(BusinessException.class)
public GlobalResult<?> businessExceptionResponse(BusinessException exception) {
log.error(exception.getMessage(), exception);
return GlobalResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), exception.getMessage(), "业务异常");
}
/**
*
*
* @param exception
* @return
*/
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
public GlobalResult<?> validationExceptionResponse(MethodArgumentNotValidException exception) {
log.error(exception.getMessage(), exception);
// 格式化错误信息
String errorMsg = exception.getBindingResult().getFieldErrors().stream()
.map(e -> e.getField() + ":" + e.getDefaultMessage()).collect(Collectors.joining("、"));
return GlobalResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "参数验证异常", errorMsg);
}
/**
*
*
* @param exception
* @return
*/
@ExceptionHandler(Exception.class)
public GlobalResult<?> validationExceptionResponse(Exception exception) {
log.error(exception.getMessage(), exception);
return GlobalResult.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知错误", exception.getMessage());
}
}

@ -1,5 +1,7 @@
package com.superversion.rasa.controller;
import cn.hutool.core.util.StrUtil;
import com.superversion.rasa.exception.BusinessException;
import com.superversion.rasa.pojo.vo.RasaArgument;
import com.superversion.rasa.service.RasaCmdService;
import io.swagger.annotations.Api;
@ -32,7 +34,11 @@ public class RasaCmdController {
@PostMapping("/runExec")
public String runExec(@RequestBody RasaArgument argument) throws ExecutionException, InterruptedException, TimeoutException {
return rasaCmdService.runExec(argument);
String outString = rasaCmdService.runExec(argument);
if (StrUtil.isEmptyIfStr(outString) || !outString.contains("Rasa server is up and running")){
throw new BusinessException("任务执行异常。详细日志:"+outString);
}
return outString;
}

@ -0,0 +1,49 @@
package com.superversion.rasa.domian;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;
@Data
@ApiModel
public class GlobalResult<T> {
private int code = 200;
private String msg = "success";
@ApiModelProperty
private T data;
public static <T> GlobalResult<T> ok() {
return ok(null);
}
public static <T> GlobalResult<T> ok(T data) {
GlobalResult<T> globalResult = new GlobalResult<>();
globalResult.setData(data);
return globalResult;
}
public static <T> GlobalResult<T> ok(T data, String message) {
GlobalResult<T> globalResult = new GlobalResult<>();
globalResult.setMsg(message);
globalResult.setData(data);
return globalResult;
}
public static <T> GlobalResult<T> error(String msg) {
return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), null, msg);
}
public static <T> GlobalResult<T> error(int code, T data, String msg) {
GlobalResult<T> globalResult = new GlobalResult<>();
globalResult.setCode(code);
globalResult.setData(data);
globalResult.setMsg(msg);
return globalResult;
}
}

@ -0,0 +1,76 @@
/*
* : CustomException
* :
* : <>
* : RedName
* : 2022/8/5
* : <>
* : <>
* : <>
*/
package com.superversion.rasa.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
/**
* <>
*
*
* @author ljt
* @version [, 2022/8/5]
* @see [/]
* @since [/]
*/
@Slf4j
public class BusinessException extends RuntimeException {
/**
*
*/
private final Integer code;
/**
*
*/
private final String message;
public BusinessException(Throwable cause) {
super(cause);
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
this.message = null;
}
public BusinessException(Throwable cause, String message) {
super(cause);
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
this.message = message;
}
public BusinessException(String message) {
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
this.message = message;
}
public BusinessException(String message, Integer code) {
this.message = message;
this.code = code;
}
public BusinessException(String message, Throwable e) {
super(message, e);
log.error(message, e);
this.code = HttpStatus.INTERNAL_SERVER_ERROR.value();
this.message = message;
}
@Override
public String getMessage() {
return message;
}
public Integer getCode() {
return code;
}
}

@ -55,7 +55,6 @@ public class RasaCmdServiceImpl implements RasaCmdService {
cmds.add(argument.getFixedModelName());
log.info("trainExec cmd : {}",StrUtil.join(" ",cmds));
System.out.println("trainExec cmd sout:"+StrUtil.join(" ",cmds));
return String.join("\r\n",execCmd(cmds,s->false,90));
}
@ -69,7 +68,6 @@ public class RasaCmdServiceImpl implements RasaCmdService {
List<String> cmds = ListUtil.toList(shellEnv, runShell,mPath,endpoints,argument.getPort());
log.info("runExec cmd : {}",StrUtil.join(" ",cmds));
System.out.println("runExec cmd sout:"+StrUtil.join(" ",cmds));
return String.join("\r\n",execCmd(cmds,s-> StrUtil.isNotBlank(s)&& s.contains("Rasa server is up and running"),90));
}
@ -88,13 +86,13 @@ public class RasaCmdServiceImpl implements RasaCmdService {
BufferedReader bufferedReader = new BufferedReader(reader);
List<String> outString = new ArrayList<>();
String resultLines = bufferedReader.readLine();
while(resultLines != null) {
while( resultLines != null) {
resultLines = bufferedReader.readLine(); // 读取下一行
log.info("resultLines:{}",resultLines);
outString.add(resultLines);
if (endPredicate.test(resultLines)){
break;
}
outString.add(resultLines);
}
bufferedReader.close();
return outString;

@ -1,5 +1,5 @@
server:
port: 8081
port: 8082
servlet:
context-path: /

Loading…
Cancel
Save