在大模型会话中,会有一个功能是停止生成功能。这个功能如果在前端实现,既取消监听后端的流式返回事件,会导致后端日志中报错连接中断等错误。
由此引出的需求,我的接口A中使用了sse流式返回,需要做一个接口B,B的功能是中止第一个接口的流式返回,以下是核心代码和思路:
方案一:需要借助redis,在输出时循环判定来解决。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet