5分钟搞定Spring项目与DeepSeek集成,让你的应用更智能!

DeepSeek 作为一款卓越的国产 AI 大模型,性能和 ChatGPT 不相上下,而且无需复杂的网络环境,更适合国人。目前越来越多的公司考虑在自己的应用中集成。Java 开发者可以借助 Spring AI 集成 DeepSeek,非常简单方便!

简介

Spring AI 是 Spring 生态中的一个新项目,其主要完成了几种常见生成式模型的适配,包括对话、文生图、文生语音等。Spring AI 目前是通过使用现有的 OpenAI 客户端与 DeepSeek AI 集成的,如图所示:

动手体验

1. 新建项目

首先新建一个Maven项目,JDK选的是17版本

2. pom.xml文件引入依赖

项目pom.xml文件引入

spring-ai-openai-spring-boot-starter依赖

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jms</groupId>
    <artifactId>jms-ai-deepseek</artifactId>
    <version>1.0.0</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <logback.version>1.5.16</logback.version>
        <spring-boot.version>3.4.2</spring-boot.version>
        <spring-ai-openai-spring-boot-starter.version>1.0.0-M6</spring-ai-openai-spring-boot-starter.version>
        <lombok.version>1.18.26</lombok.version>
        <spring-cloud-function.version>4.2.1</spring-cloud-function.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
            <version>${spring-ai-openai-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
        </dependency>
        <!--logback-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-core</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-function-context</artifactId>
                <version>${spring-cloud-function.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-function-core</artifactId>
                <version>${spring-cloud-function.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

3. 添加DeepSeek API KEY

DeepSeek API KEY 可以在 DeepSeek 开放平台中自行创建,地址:platform.deepseek.com/api\\_keys

然后在Spring Boot项目添加application.properties配置文件,加入以下内容(替成你创建的api-key)

ini 复制代码
# 服务端口
server.port=8089

# 服务名
spring.application.name=jms-ai-deepseek

# DeepSeek的OpenAI式端点
spring.ai.openai.base-url=https://api.deepseek.com/v1

# DeepSeek API KEY
spring.ai.openai.api-key=你创建的api-key

# 配置AI大模型
# deepseek-chat 模型已全面升级为 DeepSeek-V3,接口不变。 通过指定 
model=deepseek-chat 即可调用 DeepSeek-V3
# deepseek-reasoner 是 DeepSeek 最新推出的推理模型 DeepSeek-R1。通过指定 
model=deepseek-reasoner,即可调用 DeepSeek-R1。
spring.ai.openai.chat.options.model=deepseek-chat

4. 添加Spring Boot启动类

添加Spring Boot启动类 DeepSeekApplication

typescript 复制代码
@SpringBootApplication
public class DeepSeekApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeepSeekApplication.class,args);
    }
}

5. 添加Controller控制器类

新建一个控制器类DeepSeekController,添加文本接口:

less 复制代码
@RestController
@RequestMapping("/ai")
@CrossOrigin
@RequiredArgsConstructor
public class DeepSeekController {
    private final DeepSeekService deepSeekService;

    /** 
     * 对接文本模型 
     * 
     * @param message 消息内容 
     * @return AI答案 
     */
     @GetMapping("/chat")
     public String completion(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {    
         return deepSeekService.completion(message);
     }
}

6. 添加Service服务接口和实现类

新建一个DeepSeekService接口,支持文本模型处理方法:

typescript 复制代码
public interface DeepSeekService {
    /**
     * 文本模型
     *
     * @param message 消息内容
     * @return AI答案
     */
    String completion(String message);
}

新建一个DeepSeekServiceImpl类实现DeepSeekService接口,具体实现如下:

typescript 复制代码
@Slf4j
@Service
public class DeepSeekServiceImpl implements DeepSeekService {
    private final ChatClient chatClient;
    
    public DeepSeekServiceImpl(ChatClient.Builder chatClientBuilder) {
        // 构造方法注入 ChatClient.Builder,用于构建 ChatClient 实例
        this.chatClient = chatClientBuilder.build();
    }

    /**
     * 文本模型
     *
     * @param message 消息内容
     * @return AI答案
     */
    @Override
    public String completion(String message) {
        log.info("DeepSeek response");
        // 调用 ChatClient 的 prompt 方法生成响应
        // 1. new Prompt(message): 创建一个包含用户输入消息的 Prompt 对象
        // 2. call(): 调用ChatClient与AI模型交互以获取响应
        // 3. content(): 获取响应的内容部分
        return chatClient.prompt(new Prompt(message)).call().content();
    }
}

可以看到,几行代码就搞定了,非常简单。

接下来启动DeepSeekApplication****应用来测试下效果!!!

测试

调用文本模型接口,可以看到AI生成了一个笑话。

总结

Spring AI框架提供了多种模型选择,简化了我们AI开发功能,只需简单几步即可轻松实现。Spring项目接入DeepSeek大模型介绍到此就结束了,感兴趣的同学可以去试试哈!

推荐阅读

轻松掌握二分法查找

十大经典排序算法之选择排序

Java日志脱敏框架sensitive使用

JRebel激活指南:让你的Java开发更高效!

Zookeeper客户端Curator使用(分布式锁实现)

真绝了~使用Wireshark工具抓取https数据明文包

相关推荐
王亭_6664 分钟前
VSCode集成deepseek使用介绍(Visual Studio Code)
ide·vscode·编辑器·deepseek·openrouter
d3soft1 小时前
deepseek清华大学第二版 如何获取 DeepSeek如何赋能职场应用 PDF文档 电子档(附下载)
ai·pdf·教程·deepseek·赋能职场
Asthenia04122 小时前
浏览器缓存机制深度解析:电商场景下的性能优化实践
后端
AIGC大时代2 小时前
DeepSeek学术指南:利用DeepSeek撰写学术论文和需要注意的问题
chatgpt·学术论文·deepseek·aiwritepaper
databook4 小时前
『Python底层原理』--Python对象系统探秘
后端·python
超爱吃士力架5 小时前
MySQL 中的回表是什么?
java·后端·面试
banjin5 小时前
免费体验,在阿里云平台零门槛调用满血版DeepSeek-R1模型
阿里云·自然语言处理·云计算·ai编程·ai写作·deepseek
王会举5 小时前
DeepSeek模型集成到java中使用(阿里云版)超简单版
java·阿里云·deepseek
追逐时光者5 小时前
Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
后端·.net
苏三说技术6 小时前
10亿数据,如何迁移?
后端