Langchaine4j 流式输出 (6)

Langchaine4j 流式输出

大模型的流式输出是指大模型在生成文本或其他类型的数据时,不是等到整个生成过程完成后再一次性

返回所有内容,而是生成一部分就立即发送一部分给用户或下游系统,以逐步、逐块的方式返回结果。

这样,用户就不需要等待整个文本生成完成再看到结果。通过这种方式可以改善用户体验,因为用户不

需要等待太长时间,几乎可以立即开始阅读响应。

流式输出

添加流式输出依赖

复制代码
<!--流式输出-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-reactor</artifactId>
</dependency>

使用流式输出模型

复制代码
langchain4j:
 # 接入阿里百炼平台
  community:
    dashscope:
      streaming-chat-model:
        api-key: ${ALI_BAILIAN_TOKEN}
        model-name: qwen-plus

创建流式Assistant

复制代码
@AiService(wiringMode = AiServiceWiringMode.EXPLICIT,
        streamingChatModel = "qwenStreamingChatModel", // 这里注入 千问流式模型
        chatMemory = "chatMemory")
public interface StreamAssistant {
	// 使用WebFlux接受流式模型返回
    Flux<String> chat( String userMessage);

}

测试流式输出

  • 单元测试流式输出

    @SpringBootTest
    public class StreamModelTest {

    复制代码
      @Resource
      private StreamAssistant streamAssistant;
    
      @Test
      public void testStreamModel() throws InterruptedException {
          Flux<String> responseFlux = streamAssistant.chat("1+2等于几,322233222345的平方根是多少?");
    
          CountDownLatch latch = new CountDownLatch(1);
    
          responseFlux
                  .doOnSubscribe(sub -> System.out.println("Subscribed to flux"))
                  .subscribe(
                          chunk -> System.out.println("Received: " + chunk),
                          throwable -> {
                              System.err.println("Error occurred: " + throwable.getMessage());
                              latch.countDown();
                          },
                          () -> {
                              System.out.println("Completed");
                              latch.countDown();
                          }
                  );
    
          latch.await();
      }

    }

  • 接口流式测试

    创建对外接口:

    复制代码
    @RestController
    @RequestMapping("/stream")
    public class StreamController {
    
        @Resource
        private StreamAssistant streamAssistant;
    
        @Operation(summary = "对话")
        @GetMapping(value = "/chat", produces = "text/stream;charset=utf-8") // 设置响应类型为流式文本,并指定字符集为UTF-8
        public Flux<String> chat() {
            return streamAssistant.chat("1+2等于几,322233222345的平方根是多少?");
        }
    }
相关推荐
俊哥V2 小时前
AI一周事件(2025年5月27日-6月2日)
人工智能·ai
梁云亮2 小时前
SpringBoot中缓存@Cacheable出错
spring boot·缓存
小白跃升坊2 小时前
Prompt 生产及提示词案例(含完整提示词)
ai·大语言模型·maxkb·提示词模版
迢迢星万里灬2 小时前
Java求职者面试:Spring、Spring Boot、Spring MVC与MyBatis技术深度解析
java·spring boot·spring·面试·mybatis·spring mvc
代码老y2 小时前
基于springboot的图书管理系统的设计与实现
java·vue.js·spring boot·后端·毕业设计·课程设计·个人开发
ross2 小时前
更新已打包好的 Spring Boot JAR 文件中的 class 文件
spring boot·后端·jar
仙人掌_lz2 小时前
优化 Transformer 模型:基于知识蒸馏、量化技术及 ONNX
人工智能·深度学习·ai·语言模型·自然语言处理·llm·transformer
Java开发追求者3 小时前
java-springboot文件上传校验之只允许上传excel文件,且检查不能是脚本或者有害文件或可行性文件
java·spring boot·excel·上传文件校验
幽络源小助理3 小时前
SpringBoot+Vue+微信小程序校园自助打印系统
java·spring boot·微信小程序·小程序·vue
翻滚吧键盘3 小时前
数据库,Spring Boot,数据源
数据库·spring boot·后端