大模型修改成配置文件可配

This commit is contained in:
wulin 2023-11-17 09:16:20 +08:00
parent b370b13fe0
commit 435c7d8187
13 changed files with 79 additions and 30 deletions

View File

@ -59,6 +59,7 @@ public class AliYunQianWen {
public Mono<Boolean> sendMessage(String msg, public Mono<Boolean> sendMessage(String msg,
IQianWen<? super String> onNext, IQianWen<? super String> onNext,
QWenReplyResponse qwenReplyResponse) { QWenReplyResponse qwenReplyResponse) {
log.info("调用通义星尘回答:{}", msg);
if(!canAsk){ if(!canAsk){
msgManager = new MessageManager(10); msgManager = new MessageManager(10);
} }

View File

@ -73,15 +73,18 @@ public class AliYunXingChen {
).build(); ).build();
} }
public Mono<Boolean> sendMessage(String msg, public Mono<Boolean> sendMessage(String msg,
IQianWen<? super String> onNext){ IQianWen<? super String> onNext,
QWenReplyResponse qwenReplyResponse){
log.info("调用通义星尘回答:{}", msg);
Message message = Message.builder().name(userName).role("user").content(msg).build(); Message message = Message.builder().name(userName).role("user").content(msg).build();
addMessage(message); addMessage(message);
chatReqParams.setMessages(messages); chatReqParams.setMessages(messages);
qwenReplyResponse.setCode(200);
return Mono.defer(() -> { return Mono.defer(() -> {
try { try {
Flowable<ChatResult> response = api.streamOut(chatReqParams); Flowable<ChatResult> response = api.streamOut(chatReqParams);
RecordMessage recordMessage = new RecordMessage(); RecordMessage recordMessage = new RecordMessage();
response.forEach(m -> { response.blockingForEach(m -> {
String v = m.getChoices().get(0).getMessages().get(0).getContent().replaceAll(recordMessage.getMsg(), ""); String v = m.getChoices().get(0).getMessages().get(0).getContent().replaceAll(recordMessage.getMsg(), "");
onNext.sendMessage(v); onNext.sendMessage(v);
if("stop".equals(m.getChoices().get(0).getStopReason())){ if("stop".equals(m.getChoices().get(0).getStopReason())){
@ -97,6 +100,7 @@ public class AliYunXingChen {
}); });
}catch (Exception e){ }catch (Exception e){
log.info("调用星尘异常{}", e); log.info("调用星尘异常{}", e);
qwenReplyResponse.setCode(500);
} }
return Mono.just(true); return Mono.just(true);
}); });

View File

@ -0,0 +1,9 @@
package com.qiuguo.iot.third.service;
import com.qiuguo.iot.data.request.qwen.TongYiCommunicationRest;
import com.qiuguo.iot.data.resp.qg.algorithm.QWenReplyResponse;
import reactor.core.publisher.Mono;
public interface ITongYi {
Mono<QWenReplyResponse> communication(TongYiCommunicationRest rest, IQianWen<? super String> onNext);
}

View File

@ -28,7 +28,7 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Service @Service
public class QWenService { public class QWenService implements ITongYi{
@Resource @Resource
private ReactiveStringRedisTemplate reactiveStringRedisTemplate; private ReactiveStringRedisTemplate reactiveStringRedisTemplate;
@ -37,6 +37,7 @@ public class QWenService {
protected static ConcurrentHashMap<String, AliYunQianWen> qianwenGroup = new ConcurrentHashMap<>(); protected static ConcurrentHashMap<String, AliYunQianWen> qianwenGroup = new ConcurrentHashMap<>();
@Override
public Mono<QWenReplyResponse> communication(TongYiCommunicationRest rest, IQianWen<? super String> onNext){ public Mono<QWenReplyResponse> communication(TongYiCommunicationRest rest, IQianWen<? super String> onNext){
AliYunQianWen aliQianWen = null; AliYunQianWen aliQianWen = null;
if (!qianwenGroup.containsKey(rest.getOnlyId())) { if (!qianwenGroup.containsKey(rest.getOnlyId())) {

View File

@ -30,13 +30,13 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
@Service @Service
public class TongYiXinChenService { public class TongYiXinChenService implements ITongYi{
protected static ConcurrentHashMap<String, AliYunXingChen> qianwenGroup = new ConcurrentHashMap<>(); protected static ConcurrentHashMap<String, AliYunXingChen> qianwenGroup = new ConcurrentHashMap<>();
@Override
public Mono<QWenReplyResponse> communication(TongYiCommunicationRest rest, IQianWen<? super String> onNext){ public Mono<QWenReplyResponse> communication(TongYiCommunicationRest rest, IQianWen<? super String> onNext){
AliYunXingChen aliXingChen = null; AliYunXingChen aliXingChen = null;
if (!qianwenGroup.containsKey(rest.getOnlyId())) { if (!qianwenGroup.containsKey(rest.getOnlyId())) {
@ -48,7 +48,8 @@ public class TongYiXinChenService {
aliXingChen = qianwenGroup.get(rest.getOnlyId()); aliXingChen = qianwenGroup.get(rest.getOnlyId());
} }
QWenReplyResponse qWenReplyResponse = new QWenReplyResponse(); QWenReplyResponse qWenReplyResponse = new QWenReplyResponse();
return aliXingChen.sendMessage(rest.getText(), onNext).flatMap(b -> {
return aliXingChen.sendMessage(rest.getText(), onNext, qWenReplyResponse).flatMap(b -> {
return Mono.just(qWenReplyResponse); return Mono.just(qWenReplyResponse);
}); });

View File

@ -4,6 +4,7 @@ import cn.hutool.extra.spring.SpringUtil;
import com.qiuguo.iot.base.constans.Log4Constans; import com.qiuguo.iot.base.constans.Log4Constans;
import com.qiuguo.iot.base.enums.*; import com.qiuguo.iot.base.enums.*;
import com.qiuguo.iot.base.utils.StringUtils; import com.qiuguo.iot.base.utils.StringUtils;
import com.qiuguo.iot.box.websocket.api.config.properties.SomeProperties;
import com.qiuguo.iot.box.websocket.api.domain.BaseSession; import com.qiuguo.iot.box.websocket.api.domain.BaseSession;
import com.qiuguo.iot.box.websocket.api.domain.QueueMessage; import com.qiuguo.iot.box.websocket.api.domain.QueueMessage;
import com.qiuguo.iot.box.websocket.api.domain.box.BoxSession; import com.qiuguo.iot.box.websocket.api.domain.box.BoxSession;
@ -17,6 +18,7 @@ import com.qiuguo.iot.third.nlp.action.Actions;
import com.qiuguo.iot.third.service.*; import com.qiuguo.iot.third.service.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC; import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Value;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers; import reactor.core.scheduler.Schedulers;
@ -26,8 +28,10 @@ import javax.annotation.Resource;
public abstract class ActionCommand { public abstract class ActionCommand {
@Resource @Resource
protected QWenService qwenService; SomeProperties someProperties;
@Resource @Resource
protected MqService mqService; protected MqService mqService;
@ -118,25 +122,13 @@ public abstract class ActionCommand {
} }
protected Mono<Void> toQianWen(Action action, BaseSession baseSession, Integer type){ private IQianWen<String> getIQianWen(Action action,
log.info("调用千问{}", action.getAsk()); BaseSession baseSession,
TongYiCommunicationRest tongYiCommunicationRest = new TongYiCommunicationRest(); TongYiCommunicationRest tongYiCommunicationRest,
tongYiCommunicationRest.setText(action.getAsk()); Integer type,
tongYiCommunicationRest.setStatus("2"); QueueMessage queueMessage) {
tongYiCommunicationRest.setRequestId(baseSession.getRequestId());
//调用星尘
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
//下面是调用千问
/*if(baseSession instanceof BoxSession){
tongYiCommunicationRest.setOnlyId(baseSession.getSn());
}else{
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
}*/
return new IQianWen<String>() {
QueueMessage queueMessage = new QueueMessage();
queueMessage.setRequestId(baseSession.getRequestId());
return qwenService.communication(tongYiCommunicationRest, new IQianWen<String>() {
@Override @Override
public void sendMessage(String message) { public void sendMessage(String message) {
//通知到客户端 //通知到客户端
@ -158,7 +150,37 @@ public abstract class ActionCommand {
queueMessage.setStatus(YesNo.NO.getCode()); queueMessage.setStatus(YesNo.NO.getCode());
MDC.remove(Log4Constans.PRINT_LOG_ID); MDC.remove(Log4Constans.PRINT_LOG_ID);
} }
}).flatMap(data ->{ };
}
protected Mono<Void> toTongYi(Action action, BaseSession baseSession, Integer type){
log.info("调用千问{}", action.getAsk());
TongYiCommunicationRest tongYiCommunicationRest = new TongYiCommunicationRest();
tongYiCommunicationRest.setText(action.getAsk());
tongYiCommunicationRest.setStatus("2");
tongYiCommunicationRest.setRequestId(baseSession.getRequestId());
ITongYi iTongYi = SpringUtil.getBean(someProperties.getTongYi());
if(iTongYi instanceof TongYiXinChenService){
//调用星尘
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
tongYiCommunicationRest.setRoleId(baseSession.getRoleId());
}else{
if(baseSession instanceof BoxSession){
tongYiCommunicationRest.setOnlyId(baseSession.getSn());
}else{
tongYiCommunicationRest.setOnlyId(baseSession.getUserId().toString());
}
}
QueueMessage queueMessage = new QueueMessage();
queueMessage.setRequestId(baseSession.getRequestId());
return iTongYi.communication(tongYiCommunicationRest, getIQianWen(action,
baseSession,
tongYiCommunicationRest,
type,
queueMessage)).flatMap(data ->{
queueMessage.setStatus(YesNo.NO.getCode()); queueMessage.setStatus(YesNo.NO.getCode());
if(data.getCode() == 200){ if(data.getCode() == 200){
log.info("千问正常结束"); log.info("千问正常结束");
@ -171,7 +193,7 @@ public abstract class ActionCommand {
return Mono.empty(); return Mono.empty();
}); });
} }
})/*.subscribeOn(Schedulers.boundedElastic()).subscribe()*/; });
} }
protected Mono<Boolean> sendMq(String msg){ protected Mono<Boolean> sendMq(String msg){

View File

@ -12,7 +12,7 @@ public class QianWenActionCommand extends ActionCommand implements IActionComma
public Mono<Boolean> process(Action action, BaseSession baseSession) { public Mono<Boolean> process(Action action, BaseSession baseSession) {
log.info("千问 Action{}", action.getAsk()); log.info("千问 Action{}", action.getAsk());
return toQianWen(action, return toTongYi(action,
baseSession, baseSession,
action.getSystemTalkAnswerConfigEntity() == null ? AskTypeEnum.TTS.getCode() : action.getSystemTalkAnswerConfigEntity().getAnswerType()).flatMap(vo ->{ action.getSystemTalkAnswerConfigEntity() == null ? AskTypeEnum.TTS.getCode() : action.getSystemTalkAnswerConfigEntity().getAnswerType()).flatMap(vo ->{
//千问只调一次 //千问只调一次

View File

@ -13,7 +13,7 @@ public class TtsActionCommand extends ActionCommand implements IActionCommand{
log.info("纯文本Tts Action{}", action.getAsk()); log.info("纯文本Tts Action{}", action.getAsk());
if(!action.getAction().equals(action.getAsk())){ if(!action.getAction().equals(action.getAsk())){
return toQianWen( return toTongYi(
action, action,
baseSession, baseSession,
AskTypeEnum.TTS.getCode() AskTypeEnum.TTS.getCode()

View File

@ -27,5 +27,8 @@ public class SomeProperties {
@Value("${device.timeout}") @Value("${device.timeout}")
private Long timeOut;//2分钟 private Long timeOut;//2分钟
@Value("${Ali.tongyi}")
protected String tongYi;
} }

View File

@ -57,4 +57,9 @@ public class BaseSession {
* TTS合成声音的声音标识 * TTS合成声音的声音标识
*/ */
String ttsId; String ttsId;
/**
* 形成角色id
*/
String roleId;
} }

View File

@ -270,6 +270,7 @@ public class BoxWebSocketHandler implements WebSocketHandler {
entity.setOtherDeviceId(dv.getSn()); entity.setOtherDeviceId(dv.getSn());
entity.setCategoryCode(DeviceCodeEnum.BOX.getName()); entity.setCategoryCode(DeviceCodeEnum.BOX.getName());
entity.setBindName("果宝儿Box"); entity.setBindName("果宝儿Box");
boxSession.setRoleId(entity.getCategoryCode());
return deviceUserBindService.setNoMain(userId, DeviceTypeEnum.GUO_BOX.getCode()).defaultIfEmpty(0).flatMap(m ->{ return deviceUserBindService.setNoMain(userId, DeviceTypeEnum.GUO_BOX.getCode()).defaultIfEmpty(0).flatMap(m ->{
log.info("解除历史isMain标注个数{}", m); log.info("解除历史isMain标注个数{}", m);
return deviceUserBindService.insertDeviceUserBind(entity).flatMap(l ->{ return deviceUserBindService.insertDeviceUserBind(entity).flatMap(l ->{
@ -279,6 +280,7 @@ public class BoxWebSocketHandler implements WebSocketHandler {
}else{ }else{
boxSession.setTtsId(entity.getTts()); boxSession.setTtsId(entity.getTts());
boxSession.setRoleId(entity.getCategoryCode());
if(entity.getIsBind().equals(YesNo.YES.getCode())){ if(entity.getIsBind().equals(YesNo.YES.getCode())){
//通知用户端设备绑定成功 //通知用户端设备绑定成功
sendNoticeToUser(userId, "设备联网成功,设备序列号:" + dv.getSn(), AskTypeEnum.BOX_ON_LINE.getCode()); sendNoticeToUser(userId, "设备联网成功,设备序列号:" + dv.getSn(), AskTypeEnum.BOX_ON_LINE.getCode());

View File

@ -164,7 +164,7 @@ public class CustomerWebSocketHandler implements WebSocketHandler {
.flatMap(deviceUserBindEntity -> { .flatMap(deviceUserBindEntity -> {
if(deviceUserBindEntity.getId() != null){ if(deviceUserBindEntity.getId() != null){
log.info("用户绑定信息为{}", deviceUserBindEntity); log.info("用户绑定信息为{}", deviceUserBindEntity);
userSession.setRoleId(deviceUserBindEntity.getCategoryCode());
userSession.setDeviceId(deviceUserBindEntity.getDeviceId()); userSession.setDeviceId(deviceUserBindEntity.getDeviceId());
userSession.setSn(deviceUserBindEntity.getOtherDeviceId()); userSession.setSn(deviceUserBindEntity.getOtherDeviceId());

View File

@ -59,6 +59,7 @@ Ali:
qianwen: 'sk-8d64677afaf6404cb83ce1910b5b2558' qianwen: 'sk-8d64677afaf6404cb83ce1910b5b2558'
accesskeId: 'LTAI5t7d1iZb18SvGQhtDnyN' accesskeId: 'LTAI5t7d1iZb18SvGQhtDnyN'
accesskeySecret: 'j2Cp3uCDGuiA7xZIJmYOCmDhJl9HuJ' accesskeySecret: 'j2Cp3uCDGuiA7xZIJmYOCmDhJl9HuJ'
tongyi: 'tongYiXinChenService'
xingchen: xingchen:
key: 'lm-dIINta/ISx0/8aZK5xKt/A==' key: 'lm-dIINta/ISx0/8aZK5xKt/A=='
basepath: https://nlp.aliyuncs.com basepath: https://nlp.aliyuncs.com