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的平方根是多少?");
        }
    }
相关推荐
清水1 小时前
Spring Boot企业级开发入门
java·spring boot·后端
Q_Q5110082852 小时前
python+django/flask的校园活动中心场地预约系统
spring boot·python·django·flask·node.js·php
水冗水孚2 小时前
类比前端知识来学习Java的Spring Boot实现MySql的全栈CRUD功能——搭配Svelte+Vite
spring boot·svelte
Oxo Security2 小时前
【AI安全】检索增强生成(RAG)
人工智能·安全·网络安全·ai
少林码僧2 小时前
2.3 Transformer 变体与扩展:BERT、GPT 与多模态模型
人工智能·gpt·ai·大模型·bert·transformer·1024程序员节
淘源码d3 小时前
什么是医院随访系统?成熟在用的智慧随访系统源码
java·spring boot·后端·开源·源码·随访系统·随访系统框架
Q_Q19632884754 小时前
python+django/flask基于机器学习的就业岗位推荐系统
spring boot·python·django·flask·node.js·php
ArabySide4 小时前
【Spring Boot】REST与RESTful详解,基于Spring Boot的RESTful API实现
spring boot·后端·restful
程序定小飞5 小时前
基于springboot的学院班级回忆录的设计与实现
java·vue.js·spring boot·后端·spring
攀小黑5 小时前
基于若依-内容管理动态修改,通过路由字典配置动态管理
java·vue.js·spring boot·前端框架·ruoyi