Spring AI中流式对话API如何使用-基于DeepSeek
Spring AI中流式对话API如何使用-基于DeepSeek

上一篇文章分享了Spring AI快速入门-基于DeepSeek&智谱实现聊天应用今天这篇文章我们来分享Spring AI中流式对话API如何使用-基于DeepSeek。
文章最后可以加入免费的Java技术栈沟通社群,一起探讨Java/你的产品如何与AI结合,请按照要求加入。在群中可以聊开发、系统设计、架构、行业趋势、AI等等话题
完整代码在文章最后,如果觉得本篇文章对你有用,记得点赞、关注、收藏哦。你的支持是我持续更新的动力!
AI专栏软件环境
- IntelliJ IDEA2024.3.2.2
- Spring AI 1.0.0-SNAPSHOT
- Spring Boot 3.4.2
- Spring 6.2.2
- ChatGPT
- DeepSeek
- JDK 17.0.12
我们先看本篇文章对应的项目结构,请看下图

在上一篇文章中演示了阻塞式的SpringAI聊天应用,一般来说,由于网络请求延迟或AI生成的文本内容过长等因素,会导致阻塞式的聊天应用对用户的体验非常不好。而目前对于对话式的应用场景,主流的调用方式基本采用的是流式对话。那么什么是流式对话呢?流式对话的核心就是流式传输,AI的响应数据是一点一点分批传过来,不用等AI将文本全部生成才进行传输。一定程度上能够提高使用时的响应速度,给用户一个非常好的产品体验。
目前流式对话的实现手段主要有两种:SSE
和WebSocket
协议。SSE是基于Http实现的一种服务端向客户端推送消息的技术,WebSocket则是基于TCP协议实现的全双工通信技术。SSE的实现较为简单,而WebSocket较为复杂,且后者对资源的占用率很高。OpenAI官网采用的是基于SSE实现。
1 SpringAI流式对话支持
Spring AI中流式对话接口采用Spring WebFlux异步网络框架实现,WebFlux底层默认采用Netty高性能异步网络框架,因此,如果需要了解Spring AI流式对话底层实现,则需要对异步网络编程实现原理进行掌握。当然,是否掌握底层原理并不妨碍我们进行简单的API调用。
2 项目搭建
2.1 pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.itbeien.ai</groupId>
<artifactId>spring-ai-labs</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-ai-lab02</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
</dependency>
</dependencies>
</project>
2.2 配置信息
spring.ai.openai.api-key=你的api-key
spring.ai.openai.base-url=https://api.deepseek.com
spring.ai.openai.chat.options.model=deepseek-chat
spring.ai.openai.chat.options.temperature=0.7
# The DeepSeek API doesn't support embeddings, so we need to disable it.
spring.ai.openai.embedding.enabled=false
2.3 controller
package cn.itbeien.ai.springai.controller;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.StreamingChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
/**
* @author itbeien
* 项目网站:https://www.itbeien.cn
* 公众号:贝恩聊架构
* 全网同名,欢迎小伙伴们关注
* Java/AI学习社群
* Copyright© 2025 itbeien
*/
@RestController
@RequestMapping("/ai")
public class AIChatController {
private final OpenAiChatModel chatModel;
private final StreamingChatModel streamingChatModel;
@Autowired
public AIChatController(OpenAiChatModel chatModel,StreamingChatModel streamingChatModel) {
this.chatModel = chatModel;
this.streamingChatModel = streamingChatModel;
}
@GetMapping("/hello")
String generation(@RequestParam("prompt") String userInput) {
return this.chatModel.call(userInput);
}
/**
* 流式调用 将produces声明为文本事件流
* @param message
* @return
*/
@GetMapping(value = "/stream",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> stream(@RequestParam("prompt")String message){
// 将流中的内容按顺序返回
/*Prompt prompt = new Prompt(new UserMessage(message));
return this.chatModel.stream(prompt);*/
return this.streamingChatModel.stream(new UserMessage(message));
}
}
3 代码测试
3.1 启动项目
运行cn.itbeien.ai.springai.Lab02Boot类main方法

3.2 使用apifox进行测试
在apifox访问http://localhost:8080/ai/stream 并添加以下对话参数,并发起访问,然后查看响应数据类型



以上就是今天Spring AI中流式对话API如何使用-基于DeepSeek全部内容,文章最后有源码下载地址
欢迎大家关注我的项目实战内容itbeien.cn,一起学习一起进步,在项目和业务中理解各种技术。

欢迎沟通交流Java/AI技术和支付业务,一起探讨大模型应用/SAAS多租户/聚合支付/预付卡系统业务、技术、系统架构、微服务、容器化。并结合大模型应用/SAAS多租户/聚合支付系统深入技术框架/微服务原理及分布式事务原理。加入我的知识星球吧

AI专栏
01IDEA&VsCode集成DeepSeek-V3 API提高编程效率
02IntelliJ IDEA集成主流 AI 编程助手及特性介绍
03Spring AI快速入门-基于DeepSeek&智谱实现聊天应用
SpringBoot3专栏
01SpringBoot3专栏-SpringBoot3.4.0整合Mybatis-plus和Mybatis
02SpringBoot3.4.0结合Mybatis-plus实现动态数据源
03mapstruct对象映射在Springboot3中这样用就对了
04RocketMQ5.3.1集成SpringBoot3.4.0就这样简单
05SpringBoot3.4.0整合Redisson实现分布式锁
06MySQL增量数据同步利器Canal1.1.7环境搭建流程
07SpringBoot3.4.0集成Canal1.1.7实现MySQL实时同步数据到Redis
08基于Docker-SpringBoot3.4.0集成Apache Pulsar4.0.1实现消息发布和订阅
09SpringBoot3.4.0整合消息中间件Kafka和RabbitMQ
10SpringBoot3.4.0整合ActiveMQ6.1.4
11SpringBoot3整合Spring Security6.4.2 安全认证框架实现简单身份认证
12SpringBoot3.4.1和Spring Security6.4.2实现基于内存和MySQL的用户认证
13SpringBoot3.4.1和Spring Security6.4.2结合OAuth2实现GitHub授权登录
14SpringBoot3.4.1和Spring Security6.4.2结合JWT实现用户登录
16SpringBoot3.4.1基于MySQL8和Quartz实现定时任务管理
跟着我学微服务系列
01跟着我学微服务,什么是微服务?微服务有哪些主流解决方案?
05SpringCloudAlibaba之图文搞懂微服务核心组件在企业级支付系统中的应用
06JDK17+SpringBoot3.4.0+Netty4.1.115搭建企业级支付系统POS网关
07JDK17+SpringCloud2023.0.3搭建企业级支付系统-预付卡支付交易微服务
08JDK17+Dubbo3.3.2搭建企业级支付系统-预付卡支付交易微服务
09JDK17+SpringBoot3.3.6+Netty4.1.115实现企业级支付系统POS网关签到功能
贝恩聊架构-项目实战地址
欢迎大家一起讨论学习,加我备注"JAVA"拉你进入技术讨论群,在技术学习、成长、工作的路上不迷路!加我后不要急,每天下午6点左右通过!营销号免入

4 源码地址
贝恩聊架构-SpringBoot3专栏系列文章、资料和源代码会同步到以下地址,代码和资料每周都会同步更新
该仓库地址主要用于贝恩聊架构-SpringBoot3专栏、基于企业级支付系统,学习微服务整体技术栈
