Commit f09e9537 authored by 赵啸非's avatar 赵啸非

修改配置文件

parent 1ace8571
package com.mortals.xhx.webflux.factory; package com.mortals.xhx.webflux.factory;
import cn.hutool.core.util.StrUtil;
import com.mortals.xhx.webflux.common.JsonUtils; import com.mortals.xhx.webflux.common.JsonUtils;
import com.mortals.xhx.webflux.service.FlowChatCallBack; import com.mortals.xhx.webflux.service.FlowChatCallBack;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.reactivestreams.Subscriber; import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription; import org.reactivestreams.Subscription;
import org.springframework.util.ObjectUtils;
import reactor.core.Disposable; import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink; import reactor.core.publisher.FluxSink;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -55,6 +58,9 @@ public class FlowChatSubscriber implements Subscriber<String>, Disposable { ...@@ -55,6 +58,9 @@ public class FlowChatSubscriber implements Subscriber<String>, Disposable {
public void onNext(String data) { public void onNext(String data) {
try { try {
data = callBack.onNext(data, context); data = callBack.onNext(data, context);
if(ObjectUtils.isEmpty(data)){
this.stopFlag=true;
}
log.info(" onNext data:{}", data); log.info(" onNext data:{}", data);
} catch (Exception e) { } catch (Exception e) {
log.error("流式问答异常:{}", e.getMessage()); log.error("流式问答异常:{}", e.getMessage());
......
package com.mortals.xhx.webflux.service.impl; package com.mortals.xhx.webflux.service.impl;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
...@@ -49,6 +50,11 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -49,6 +50,11 @@ public class BaseChatService extends AbstractFlowChatTemplate {
@Override @Override
public String onNext(String data, FlowChatContext context) { public String onNext(String data, FlowChatContext context) {
if (StrUtil.equals("[DONE]", data)) {//[DONE]是消息结束标识
return null;
}
// 解析响应数据项 // 解析响应数据项
JSONObject bodyJson = JSONObject.parseObject(data); JSONObject bodyJson = JSONObject.parseObject(data);
if (bodyJson == null) { if (bodyJson == null) {
...@@ -70,6 +76,8 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -70,6 +76,8 @@ public class BaseChatService extends AbstractFlowChatTemplate {
obj.put("delta", delta); obj.put("delta", delta);
jsonArray.add(obj); jsonArray.add(obj);
rspJson.put("delta", jsonArray); rspJson.put("delta", jsonArray);
rspJson.put("index", 0);
rspJson.put("finish_reason", null);
num++; num++;
context.getEmitter().next(rspJson.toJSONString()); context.getEmitter().next(rspJson.toJSONString());
...@@ -113,6 +121,8 @@ public class BaseChatService extends AbstractFlowChatTemplate { ...@@ -113,6 +121,8 @@ public class BaseChatService extends AbstractFlowChatTemplate {
if ("stop".equals(finish_reason)) { if ("stop".equals(finish_reason)) {
delta.put("content", ""); delta.put("content", "");
this.num = 0; this.num = 0;
}else{
delta.put("finish_reason", null);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment