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的平方根是多少?");
        }
    }
相关推荐
难受啊马飞2.01 小时前
如何判断 AI 将优先自动化哪些任务?
运维·人工智能·ai·语言模型·程序员·大模型·大模型学习
全干engineer3 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
a_Dragon13 小时前
Spring Boot多环境开发-Profiles
java·spring boot·后端·intellij-idea
ChinaRainbowSea4 小时前
补充:问题:CORS ,前后端访问跨域问题
java·spring boot·后端·spring
运器1234 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
虾条_花吹雪5 小时前
2、Connecting to Kafka
分布式·ai·kafka
DeepSeek大模型官方教程5 小时前
NLP之文本纠错开源大模型:兼看语音大模型总结
大数据·人工智能·ai·自然语言处理·大模型·产品经理·大模型学习
程序员鱼皮7 小时前
Cursor 1.2重磅更新,这个痛点终于被解决了!
ai·程序员·编程·agent·软件开发
全栈凯哥7 小时前
02.SpringBoot常用Utils工具类详解
java·spring boot·后端
我爱一条柴ya8 小时前
【AI大模型】线性回归:经典算法的深度解析与实战指南
人工智能·python·算法·ai·ai编程