langChain4j-流式输出

发布于:2025-06-20 ⋅ 阅读:(19) ⋅ 点赞:(0)

基础搭建如上一篇

引入依赖

<!--    不是spring家族,在wen应用中需要引入webflux -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

配置文件

langchain4j:
  community:
    dashscope:
      chat-model:
        api-key: xxx
        model: qwen-max
      streaming-chat-model:
        api-key: xxx
        model: qwen-32b

测试

package com.yd.langchain4j_springboot;

import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.chat.response.StreamingChatResponseHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("ai")
public class ChatController {
    @Autowired
    QwenChatModel qwenChatModel;
    @Autowired
    QwenStreamingChatModel qwenStreamingChatModel;
    @RequestMapping("/chat")
    public String test(@RequestParam(defaultValue="你是谁") String message){
        String chat = qwenChatModel.chat(message);
        return chat;
    }
    @RequestMapping(value = "/stream",produces = "text/stream;charset=UTF-8")
    public Flux<String> stream(@RequestParam(defaultValue="你是谁") String message){
        return Flux.create(sink->{
        	//需要两个参数,第二个参数为一个handler,需要重写三个方法
            qwenStreamingChatModel.chat(message, new StreamingChatResponseHandler() {
                @Override
                public void onPartialResponse(String s) {//返回一个个token
                    sink.next(s);
                }

                @Override
                public void onCompleteResponse(ChatResponse chatResponse) {
                    sink.complete();
                }

                @Override
                public void onError(Throwable throwable) {
                    sink.error(throwable);
                }
            });
        });
    }
}

效果

在这里插入图片描述
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到