Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么不支持sse流式输出? #43

Open
cxyxscn opened this issue Feb 21, 2025 · 6 comments
Open

为什么不支持sse流式输出? #43

cxyxscn opened this issue Feb 21, 2025 · 6 comments

Comments

@cxyxscn
Copy link

cxyxscn commented Feb 21, 2025

@GetMapping(value = "/chatStream", produces = {MediaType.TEXT_EVENT_STREAM_VALUE})
    public void getChatMessageStream(@RequestParam String question, HttpServletResponse response) throws Exception {
        // 中文乱码问题
        response.setCharacterEncoding("UTF-8");
        // 获取OpenAi的聊天服务
        IChatService chatService = aiService.getChatService(PlatformType.DEEPSEEK);
        // 创建请求参数
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("deepseek-chat")
                .message(ChatMessage.withUser(question))
                .build();
        PrintWriter writer = response.getWriter();
        // 发送chat请求
        SseListener sseListener = new SseListener() {
            @Override
            protected void send() {
                log.info("getCurrData:{}", this.getCurrData());
                writer.write(this.getCurrData());
                writer.flush();
            }
        };
        chatService.chatCompletionStream(chatCompletion, sseListener);
        writer.close();
    }

Image

@LnYo-Cly
Copy link
Owner

你鼠标往后拉拉,数据应该在一行里面吧。你可以使用getCurrStr,只打印当前content中的内容。

@cxyxscn
Copy link
Author

cxyxscn commented Feb 24, 2025

那怎样实现这种格式的输出?
Image

@LnYo-Cly
Copy link
Owner

this.getCurrData() + '\n'即可

@LnYo-Cly
Copy link
Owner

那怎样实现这种格式的输出? Image

你也可以将返回对象修改为SseEmitter :

    @GetMapping("/chatStream2")
    public SseEmitter getChatMessageStream(@RequestParam String question) {
        SseEmitter emitter = new SseEmitter();

        // 获取聊天服务
        IChatService chatService = aiService.getChatService(PlatformType.DEEPSEEK);

        // 创建请求参数
        ChatCompletion chatCompletion = ChatCompletion.builder()
                .model("deepseek-chat")
                .message(ChatMessage.withUser(question))
                .build();

        Executors.newSingleThreadExecutor().submit(() -> {
            try {
                SseListener sseListener = new SseListener() {
                    @Override
                    protected void send() {
                        try {
                            emitter.send(this.getCurrData());
                            System.out.println(this.getCurrData());  // 打印当前发送的内容
                        } catch (IOException e) {
                            emitter.completeWithError(e);
                        }
                    }
                };

                // 发送流式数据
                chatService.chatCompletionStream(chatCompletion, sseListener);

                // 完成后关闭连接
                emitter.complete();
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        });

        return emitter;
    }

效果图如下:
Image

@luhengyu1
Copy link

那怎样实现这种格式的输出? Image

你也可以将返回对象修改为SseEmitter :

@GetMapping("/chatStream2")
public SseEmitter getChatMessageStream(@RequestParam String question) {
    SseEmitter emitter = new SseEmitter();

    // 获取聊天服务
    IChatService chatService = aiService.getChatService(PlatformType.DEEPSEEK);

    // 创建请求参数
    ChatCompletion chatCompletion = ChatCompletion.builder()
            .model("deepseek-chat")
            .message(ChatMessage.withUser(question))
            .build();

    Executors.newSingleThreadExecutor().submit(() -> {
        try {
            SseListener sseListener = new SseListener() {
                @Override
                protected void send() {
                    try {
                        emitter.send(this.getCurrData());
                        System.out.println(this.getCurrData());  // 打印当前发送的内容
                    } catch (IOException e) {
                        emitter.completeWithError(e);
                    }
                }
            };

            // 发送流式数据
            chatService.chatCompletionStream(chatCompletion, sseListener);

            // 完成后关闭连接
            emitter.complete();
        } catch (Exception e) {
            emitter.completeWithError(e);
        }
    });

    return emitter;
}

效果图如下: Image

Image
请问下为什么我跑这个代码,postman一直这样子,控制台也什么都没有打印

@luhengyu1
Copy link

那怎样实现这种格式的输出? Image

你也可以将返回对象修改为SseEmitter :

@GetMapping("/chatStream2")
public SseEmitter getChatMessageStream(@RequestParam String question) {
    SseEmitter emitter = new SseEmitter();

    // 获取聊天服务
    IChatService chatService = aiService.getChatService(PlatformType.DEEPSEEK);

    // 创建请求参数
    ChatCompletion chatCompletion = ChatCompletion.builder()
            .model("deepseek-chat")
            .message(ChatMessage.withUser(question))
            .build();

    Executors.newSingleThreadExecutor().submit(() -> {
        try {
            SseListener sseListener = new SseListener() {
                @Override
                protected void send() {
                    try {
                        emitter.send(this.getCurrData());
                        System.out.println(this.getCurrData());  // 打印当前发送的内容
                    } catch (IOException e) {
                        emitter.completeWithError(e);
                    }
                }
            };

            // 发送流式数据
            chatService.chatCompletionStream(chatCompletion, sseListener);

            // 完成后关闭连接
            emitter.complete();
        } catch (Exception e) {
            emitter.completeWithError(e);
        }
    });

    return emitter;
}

效果图如下: Image

Image 请问下为什么我跑这个代码,postman一直这样子,控制台也什么都没有打印

哦好了,我用key调了真实的DeepSeek可以,调用本地ollama部署的小的1.5b的版本就不行,不知道是为什么。。。

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants