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

调用ollama平台,流式输出无法回调到send()方法 #40

Open
yywx-zZ opened this issue Feb 20, 2025 · 1 comment
Open

调用ollama平台,流式输出无法回调到send()方法 #40

yywx-zZ opened this issue Feb 20, 2025 · 1 comment

Comments

@yywx-zZ
Copy link

yywx-zZ commented Feb 20, 2025

使用的依赖ai4j-spring-boot-starter,版本1.2.1

配置如下
ai: ollama: api-host: http://localhost:11434/

代码如下:

`@GetMapping(value = "/ai/chatstream")

public ResponseBodyEmitter generateStreamAsString2(@RequestParam(value = "message") String message) throws Exception {
    // 中文乱码问题
    response.setCharacterEncoding("UTF-8");
    // 构造请求参数
    IChatService chatService = aiService.getChatService(PlatformType.OLLAMA);
    ChatCompletion chatCompletion = ChatCompletion.builder()
            .model("deepseek-r1:8b")
            .message(ChatMessage.withUser(message))
            .build();
    ResponseBodyEmitter emitter = new ResponseBodyEmitter();
    Executors.newSingleThreadExecutor().submit(() -> {
        try {
            SseListener sseListener = new SseListener() {
                @Override
                protected void send() {
                    try {
                        emitter.send(this.getCurrStr());
                        System.out.println(this.getCurrStr());  // 打印当前发送的内容
                    } catch (IOException e) {
                        emitter.completeWithError(e);
                    }
                }
            };

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

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

    return emitter;
}`
@LnYo-Cly
Copy link
Owner

已修复,请进行测试

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

2 participants