136 lines
4.3 KiB
Java
136 lines
4.3 KiB
Java
package com.cmvr.test.service;
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.cmvr.common.exception.GlobalException;
|
|
import com.cmvr.common.utils.http.CallAPIUtil;
|
|
import com.cmvr.common.utils.uuid.IdUtils;
|
|
import com.cmvr.edge.client.service.EdgeBioHeadService;
|
|
import com.cmvr.test.enums.FlowiseActionEnum;
|
|
import com.cmvr.test.model.vo.FlowiseActionRequestVO;
|
|
import com.cmvr.test.model.vo.FlowiseChatRequestVO;
|
|
import com.cmvr.test.model.vo.FlowiseStartRequestVO;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class FlowiseActionService {
|
|
|
|
private final EdgeBioHeadService edgeBioHeadService;
|
|
|
|
@Value("${flowise.tts}")
|
|
private String flowiseTts;
|
|
|
|
@Value("${flowise.start}")
|
|
private String flowiseStart;
|
|
|
|
@Value("${flowise.abort}")
|
|
private String flowiseAbort;
|
|
|
|
@Value("${flowise.api-key}")
|
|
private String flowiseApiKey;
|
|
|
|
@Resource(name = "threadPoolTaskExecutor")
|
|
private final ThreadPoolTaskExecutor executor;
|
|
|
|
|
|
private final Object lock = new Object();
|
|
private String chatId = null;
|
|
|
|
public String start(FlowiseStartRequestVO request) {
|
|
synchronized (lock) {
|
|
if (chatId != null) {
|
|
throw new GlobalException("Flow is already running: " + chatId);
|
|
}
|
|
|
|
chatId = IdUtils.fastUUID();
|
|
|
|
JSONObject body = new JSONObject();
|
|
body.put("question", request.getQuestion());
|
|
body.put("chatId", chatId);
|
|
body.put("streaming", true);
|
|
|
|
executor.submit(() -> {
|
|
try {
|
|
HttpRequest.post(flowiseStart)
|
|
.header("Content-Type", "application/json")
|
|
.body(body.toJSONString())
|
|
.timeout(0)
|
|
.execute()
|
|
.body();
|
|
} catch (Exception e) {
|
|
throw new GlobalException("flowise 流程执行失败: ", e.getMessage());
|
|
}
|
|
});
|
|
|
|
return chatId;
|
|
}
|
|
}
|
|
|
|
public String abort() {
|
|
CallAPIUtil.doPostJson(flowiseTts + "stop", null, null);
|
|
synchronized (lock) {
|
|
|
|
if (chatId == null) {
|
|
throw new GlobalException( "No flow running.");
|
|
}
|
|
|
|
String id = chatId;
|
|
|
|
String result = HttpRequest.put(flowiseAbort + id)
|
|
.header("Authorization", "Bearer " + flowiseApiKey)
|
|
.execute()
|
|
.body();
|
|
|
|
chatId = null;
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
public String command(FlowiseActionRequestVO request) {
|
|
String action = request.getAction();
|
|
FlowiseActionEnum actionEnum = FlowiseActionEnum.fromAction(action);
|
|
String terminalId = "4ed1246c465b97975f96c9ef8371a3bd";
|
|
String bioId = "bio_head";
|
|
switch (actionEnum) {
|
|
// ==== 表情 ====
|
|
case SMILE:
|
|
return edgeBioHeadService.specialExpression(terminalId, bioId, 1);
|
|
case SURPRISED:
|
|
return edgeBioHeadService.specialExpression(terminalId, bioId, 2);
|
|
case ANGRY:
|
|
return edgeBioHeadService.specialExpression(terminalId, bioId, 3);
|
|
case SAD:
|
|
return edgeBioHeadService.specialExpression(terminalId, bioId, 4);
|
|
case SLEEP:
|
|
return edgeBioHeadService.specialExpression(terminalId, bioId, 5);
|
|
case YAWN:
|
|
return edgeBioHeadService.specialExpression(terminalId, bioId, 6);
|
|
|
|
// ==== 动作 ====
|
|
|
|
default:
|
|
throw new UnsupportedOperationException("未实现的 Flowise Action: " + action);
|
|
}
|
|
}
|
|
|
|
public String chat(FlowiseChatRequestVO requestVO) {
|
|
Map<String, String> body = new HashMap<>();
|
|
body.put("text", requestVO.getText());
|
|
body.put("voice", "x4_yezi");
|
|
|
|
CallAPIUtil.doPostJson(flowiseTts + "play", null, body);
|
|
|
|
return "ok";
|
|
}
|
|
} |