您的位置:首页 > 娱乐 > 八卦 > 外包小程序开发的价格_建设工程信息网官网新网站_seo优化行业_北京厦门网站优化

外包小程序开发的价格_建设工程信息网官网新网站_seo优化行业_北京厦门网站优化

2025/8/8 7:21:42 来源:https://blog.csdn.net/m0_58617940/article/details/148366671  浏览:    关键词:外包小程序开发的价格_建设工程信息网官网新网站_seo优化行业_北京厦门网站优化
外包小程序开发的价格_建设工程信息网官网新网站_seo优化行业_北京厦门网站优化

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 {@Resourceprivate StreamAssistant streamAssistant;@Testpublic 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();}
}

PixPin_2025-06-01_15-07-16

  • 接口流式测试

    创建对外接口:

    @RestController
    @RequestMapping("/stream")
    public class StreamController {@Resourceprivate StreamAssistant streamAssistant;@Operation(summary = "对话")@GetMapping(value = "/chat", produces = "text/stream;charset=utf-8") // 设置响应类型为流式文本,并指定字符集为UTF-8public Flux<String> chat() {return streamAssistant.chat("1+2等于几,322233222345的平方根是多少?");}
    }
    

    chrome_stream

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com