随着人工智能技术的飞速发展,AI 已经成为现代软件应用中不可或缺的一部分。从智能对话系统到内容生成工具,AI 的应用场景日益丰富。Spring AI 是 Spring 官方推出的用于简化 AI 集成的框架,而 DeepSeek 是一个强大的 AI 平台,提供了高效、灵活的语言模型和 API 接口。通过将 Spring AI 与 DeepSeek 结合,开发者可以在 Spring Boot 应用中快速实现智能对话、文本生成等 AI 功能。
在本教程中,我们将详细介绍如何使用 Spring Boot 3.2.x、Spring AI 和 DeepSeek 创建一个智能 AI 应用。
以下是实验环境的配置:
spring-ai-openai-spring-boot-starter
对 JDK 和 Spring Boot 的版本有以下要求:
也可以在idea里调用,然后生成项目
选择需要的依赖
以下是生成的 pom.xml
文件内容:
<?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 https://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>
访问 SiliconFlow 模型列表,选择一个免费的 AI 模型。我们选择 DeepSeek 提供的 DeepSeek-R1-Distill-Llama-8B
模型。
在项目根目录下创建 application.yml
文件,并添加以下配置内容:
spring:
ai:
openai:
api-key: your-key # 替换为你的 API 密钥
base-url: https://api.siliconflow.cn # DeepSeek API 地址
chat:
options:
model: deepseek-ai/DeepSeek-R1-Distill-Llama-8B # 选择的模型
在 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 创建了一个简单的智能对话应用。你可以根据需求进一步扩展功能,例如支持多语言、优化对话逻辑或集成到前端页面中。
如果你在实现过程中遇到任何问题,欢迎在评论区留言讨论。