增加内存缓存和redis缓存两种模式

This commit is contained in:
wulin 2023-11-21 17:44:15 +08:00
parent 39a1a6681f
commit b37c185dc3
2 changed files with 56 additions and 35 deletions

View File

@ -81,11 +81,51 @@ public class AliYunXingChen {
.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,
IQianWen<? super String> onNext,
QWenReplyResponse qwenReplyResponse,
ReactiveStringRedisTemplate reactiveStringRedisTemplate){
log.info("调用通义星尘回答:{}", msg);
log.info("调用通义星尘redis缓存回答:{}", msg);
return reactiveStringRedisTemplate.opsForValue().get(RedisConstans.TONGYI_TALK_CONTENT + userId)
.defaultIfEmpty("")
.map(s -> {
@ -113,39 +153,20 @@ public class AliYunXingChen {
}
}).flatMap(msgs ->{
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();
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);
return sendMsg(onNext, qwenReplyResponse)
.map(b -> {
if(b){
reactiveStringRedisTemplate.opsForValue().set(RedisConstans.TONGYI_TALK_CONTENT + userId, JSONObject.toJSONString(messages), Duration.ofDays(1L))
.map(bb -> {
log.info("保存聊天缓存状态{}", bb);
if(bb){
messages.clear();
}
return bb;
}).subscribe();
}
return b;
});
});
}

View File

@ -50,7 +50,7 @@ public class TongYiXinChenService implements ITongYi{
}
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);
});