增加内存缓存和redis缓存两种模式
This commit is contained in:
parent
39a1a6681f
commit
b37c185dc3
@ -81,11 +81,51 @@ public class AliYunXingChen {
|
|||||||
.build()
|
.build()
|
||||||
).build();
|
).build();
|
||||||
}
|
}
|
||||||
|
public Mono<Boolean> sendMessage(String msg,
|
||||||
|
IQianWen<? super String> onNext,
|
||||||
|
QWenReplyResponse qwenReplyResponse) {
|
||||||
|
log.info("调用通义星尘内存缓存回答:{}", msg);
|
||||||
|
Message message = Message.builder().name(userName).role("user").content(msg).build();
|
||||||
|
addMessage(message);
|
||||||
|
return sendMsg(onNext, qwenReplyResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Mono<Boolean> sendMsg(IQianWen<? super String> onNext,
|
||||||
|
QWenReplyResponse qwenReplyResponse){
|
||||||
|
chatReqParams.setMessages(messages);
|
||||||
|
qwenReplyResponse.setCode(200);
|
||||||
|
try {
|
||||||
|
Flowable<ChatResult> response = api.streamOut(chatReqParams);
|
||||||
|
RecordMessage recordMessage = new RecordMessage();
|
||||||
|
response.blockingForEach(m -> {
|
||||||
|
String v = m.getChoices().get(0).getMessages().get(0).getContent().replaceAll(recordMessage.getMsg(), "");
|
||||||
|
onNext.sendMessage(v);
|
||||||
|
recordMessage.setMsg(m.getChoices().get(0).getMessages().get(0).getContent());
|
||||||
|
if("stop".equals(m.getChoices().get(0).getStopReason())){
|
||||||
|
//
|
||||||
|
|
||||||
|
Message message1 = Message.builder()
|
||||||
|
.role(m.getChoices().get(0).getMessages().get(0).getRole())
|
||||||
|
.content(m.getChoices().get(0).getMessages().get(0).getContent())
|
||||||
|
.build();
|
||||||
|
qwenReplyResponse.setResut(message1.getContent());
|
||||||
|
addMessage(message1);
|
||||||
|
onNext.finish();
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Mono.just(true);
|
||||||
|
}catch (Exception e){
|
||||||
|
log.info("调用星尘异常{}", e);
|
||||||
|
qwenReplyResponse.setCode(500);
|
||||||
|
}
|
||||||
|
return Mono.just(false);
|
||||||
|
}
|
||||||
public Mono<Boolean> sendMessage(String msg,
|
public Mono<Boolean> sendMessage(String msg,
|
||||||
IQianWen<? super String> onNext,
|
IQianWen<? super String> onNext,
|
||||||
QWenReplyResponse qwenReplyResponse,
|
QWenReplyResponse qwenReplyResponse,
|
||||||
ReactiveStringRedisTemplate reactiveStringRedisTemplate){
|
ReactiveStringRedisTemplate reactiveStringRedisTemplate){
|
||||||
log.info("调用通义星尘回答:{}", msg);
|
log.info("调用通义星尘redis缓存回答:{}", msg);
|
||||||
return reactiveStringRedisTemplate.opsForValue().get(RedisConstans.TONGYI_TALK_CONTENT + userId)
|
return reactiveStringRedisTemplate.opsForValue().get(RedisConstans.TONGYI_TALK_CONTENT + userId)
|
||||||
.defaultIfEmpty("")
|
.defaultIfEmpty("")
|
||||||
.map(s -> {
|
.map(s -> {
|
||||||
@ -113,39 +153,20 @@ public class AliYunXingChen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}).flatMap(msgs ->{
|
}).flatMap(msgs ->{
|
||||||
chatReqParams.setMessages(messages);
|
return sendMsg(onNext, qwenReplyResponse)
|
||||||
qwenReplyResponse.setCode(200);
|
.map(b -> {
|
||||||
try {
|
if(b){
|
||||||
Flowable<ChatResult> response = api.streamOut(chatReqParams);
|
reactiveStringRedisTemplate.opsForValue().set(RedisConstans.TONGYI_TALK_CONTENT + userId, JSONObject.toJSONString(messages), Duration.ofDays(1L))
|
||||||
RecordMessage recordMessage = new RecordMessage();
|
.map(bb -> {
|
||||||
response.blockingForEach(m -> {
|
log.info("保存聊天缓存状态{}", bb);
|
||||||
String v = m.getChoices().get(0).getMessages().get(0).getContent().replaceAll(recordMessage.getMsg(), "");
|
if(bb){
|
||||||
onNext.sendMessage(v);
|
messages.clear();
|
||||||
recordMessage.setMsg(m.getChoices().get(0).getMessages().get(0).getContent());
|
}
|
||||||
if("stop".equals(m.getChoices().get(0).getStopReason())){
|
return bb;
|
||||||
//
|
}).subscribe();
|
||||||
|
}
|
||||||
Message message1 = Message.builder()
|
return b;
|
||||||
.role(m.getChoices().get(0).getMessages().get(0).getRole())
|
});
|
||||||
.content(m.getChoices().get(0).getMessages().get(0).getContent())
|
|
||||||
.build();
|
|
||||||
qwenReplyResponse.setResut(message1.getContent());
|
|
||||||
addMessage(message1);
|
|
||||||
onNext.finish();
|
|
||||||
reactiveStringRedisTemplate.opsForValue().set(RedisConstans.TONGYI_TALK_CONTENT + userId, JSONObject.toJSONString(messages), Duration.ofDays(1L))
|
|
||||||
.map(b -> {
|
|
||||||
log.info("保存聊天缓存状态{}", b);
|
|
||||||
return b;
|
|
||||||
}).subscribe();
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return Mono.just(true);
|
|
||||||
}catch (Exception e){
|
|
||||||
log.info("调用星尘异常{}", e);
|
|
||||||
qwenReplyResponse.setCode(500);
|
|
||||||
}
|
|
||||||
return Mono.just(false);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -50,7 +50,7 @@ public class TongYiXinChenService implements ITongYi{
|
|||||||
}
|
}
|
||||||
QWenReplyResponse qWenReplyResponse = new QWenReplyResponse();
|
QWenReplyResponse qWenReplyResponse = new QWenReplyResponse();
|
||||||
|
|
||||||
return aliXingChen.sendMessage(rest.getText(), onNext, qWenReplyResponse, reactiveStringRedisTemplate).flatMap(b -> {
|
return aliXingChen.sendMessage(rest.getText(), onNext, qWenReplyResponse/*, reactiveStringRedisTemplate*/).flatMap(b -> {
|
||||||
return Mono.just(qWenReplyResponse);
|
return Mono.just(qWenReplyResponse);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user