SpringBoot系列之SpringAI+DeekSeek创建AI应用
- 开源代码
- 2025-09-13 19:21:01

使用 Spring AI 与 DeepSeek 创建智能 AI 应用
随着人工智能技术的飞速发展,AI 已经成为现代软件应用中不可或缺的一部分。从智能对话系统到内容生成工具,AI 的应用场景日益丰富。Spring AI 是 Spring 官方推出的用于简化 AI 集成的框架,而 DeepSeek 是一个强大的 AI 平台,提供了高效、灵活的语言模型和 API 接口。通过将 Spring AI 与 DeepSeek 结合,开发者可以在 Spring Boot 应用中快速实现智能对话、文本生成等 AI 功能。
在本教程中,我们将详细介绍如何使用 Spring Boot 3.2.x、Spring AI 和 DeepSeek 创建一个智能 AI 应用。
一、环境准备以下是实验环境的配置:
JDK 17.0.14Spring Boot 3.2.0IntelliJ IDEA 2022.2.5Maven 3.8.1 Spring AI Starter 的版本要求spring-ai-openai-spring-boot-starter 对 JDK 和 Spring Boot 的版本有以下要求:
JDK 要求: 最低要求:JDK 17。推荐版本:JDK 17 或更高版本。 Spring Boot 要求: 支持的版本范围:Spring Boot 3.2.x 和 3.3.x。最低版本:Spring Boot 3.2。 二、创建项目 使用 Spring Initializr 创建项目 访问 Spring Initializr: start.spring.io/选择项目配置: Project:MavenLanguage:JavaSpring Boot Version:3.2.0Dependencies: Spring WebSpring AI OpenAI StarterLombokSpring Boot DevTools(可选,用于开发时的热部署) 生成项目:点击“Generate”按钮下载项目压缩包,并解压到本地。也可以在idea里调用,然后生成项目 选择需要的依赖
Maven 配置文件以下是生成的 pom.xml 文件内容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// .w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>springboot-ai-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-ai-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>17</java.version> <spring-ai.version>1.0.0-M5</spring-ai.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-openai-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-bom</artifactId> <version>${spring-ai.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <annotationProcessorPaths> <path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project> 三、选择 AI 模型访问 SiliconFlow 模型列表,选择一个免费的 AI 模型。我们选择 DeepSeek 提供的 DeepSeek-R1-Distill-Llama-8B 模型。
获取 API 密钥 登录到 SiliconFlow 平台。在平台中创建一个 API 密钥。将密钥保存到本地,用于后续配置。 配置文件在项目根目录下创建 application.yml 文件,并添加以下配置内容:
spring: ai: openai: api-key: your-key # 替换为你的 API 密钥 base-url: api.siliconflow # DeepSeek API 地址 chat: options: model: deepseek-ai/DeepSeek-R1-Distill-Llama-8B # 选择的模型 四、编写测试类 创建 ChatController在 com.example.ai.controller 包下创建一个名为 ChatController 的类,用于处理用户请求并调用 DeepSeek 模型。
package com.example.ai.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @CrossOrigin(origins = "*") @Slf4j public class ChatController { private final ChatClient chatClient; public ChatController(ChatClient.Builder builder) { this.chatClient = builder.defaultSystem("你是一个AI智能应用").build(); } @GetMapping(value = "/chat/{message}") public String chat(@PathVariable("message") String message) { return chatClient.prompt() .user(message) .call() .content(); } } 测试接口启动 Spring Boot 应用,并通过以下命令调用接口:
curl http://127.0.0.1:8080/chat/今天上海天气咋样返回结果示例:
根据当前的天气数据,**今天上海的天气**大致为高温天气,温度范围在**19℃至25℃**之间,多云,偶尔有小阵雨,风力较小,东南风1级。建议根据天气情况适时调整出行穿着,注意防晒和保暖。总结
通过以上步骤,我们成功地使用 Spring Boot 3.2.x、Spring AI 和 DeepSeek 创建了一个简单的智能对话应用。你可以根据需求进一步扩展功能,例如支持多语言、优化对话逻辑或集成到前端页面中。
如果你在实现过程中遇到任何问题,欢迎在评论区留言讨论。
SpringBoot系列之SpringAI+DeekSeek创建AI应用由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“SpringBoot系列之SpringAI+DeekSeek创建AI应用”
上一篇
6.C#对接微信Native支付(退款申请、退款回调通知)
下一篇
虚拟机配置