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的平方根是多少?");
        }
    }
相关推荐
apple_692658852 小时前
DLSS5画质增强 mayflyMods v1.2.5 四合一工具:DLSS5画质增强 + MFG多帧生成 + 游戏库启动 + MOD模组管理
ai·dlss5
云烟成雨TD11 小时前
LlamaIndex 系列【31】检索增强策略:命名实体识别(NER)
ai·agent·rag·llamaindex
2601_9548118212 小时前
AI科学实验室自律系统:闭环自动化与智能实验设计技术路径 — 自动化技术
ai
2601_9548118213 小时前
AI智能教育多智能体协同:备教辅全链路架构实现 — 技术架构
ai
Java后端的Ai之路13 小时前
Python进阶探索17 - Python中的深拷贝与浅拷贝
人工智能·python·ai·浅拷贝·深拷贝
龙智DevSecOps解决方案13 小时前
AI驱动的知识管理指南:基于Atlassian Intelligence、Rovo、Teamwork Collection打造团队知识引擎
ai·atlassian·需求管理·团队协作·知识管理
李燚13 小时前
Agent 要用 API key,但明文一次都不能进模型——Secret Runtime 落地实录(第110篇)
ai·agent·credential·secret·eino·deepflux·secret runtime
奇牙coding14 小时前
gpt-5.6-luna 接入踩坑实录:model_not_found 排查与 reasoning_summary 字段使用说明
ai
霸道流氓气质14 小时前
Prompt 工程最佳实践与企业级模板库建设
ai
海带紫菜菠萝汤14 小时前
本周 AI 观察:嘴上喊着降速,手上全踩油门
人工智能·深度学习·ai·开源·大模型