5分钟上手!Spring AI与DeepSeek的Java生态集成

目录

  • 前言
  • 环境准备
  • 创建一个springboot项目
  • 实战测试
  • 总结

前言

随着人工智能技术的不断演进,如何快速在Java项目中集成先进的AI能力,成为了越来越多开发者关注的焦点。
Spring官方推出的Spring AI,极大地降低了AI应用在Java生态中的集成门槛;而DeepSeek作为新兴的强大大模型平台,也迅速赢得了开发者的青睐。

本篇将以实战为核心手把手带你用5分钟快速上手,完成Spring AI与DeepSeek在Java项目中的无缝集成,助你轻松开启AI应用开发之旅!

环境准备

  1. JDK 17+ :Spring Boot 3.x强制要求(执行java -version验证)
  2. API密钥申请
    • 登录DeepSeek开放平台https://platform.deepseek.com/api_keys
    • 点击「创建密钥」生成sk-开头的API Key(仅显示一次,需保存

创建一个springboot项目

1.输入项目信息

2.引入项目所需要的依赖 Spring Web、OpenaAI

3.下载完后 会有对应的依赖引入,这边是删减后完整的依赖

    <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>
        <spring-ai-openai-spring-boot-starter.version>1.0.0-M6</spring-ai-openai-spring-boot-starter.version>
        <spring-boot.version>3.4.2</spring-boot.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.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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.36</version>
        </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>
        </dependencies>
    </dependencyManagement>

4.application.yml 输入deepseek申请的apikey,以及其他信息

server:
  port: 8881 # 服务启动端口

spring:
  application:
    name: SpringAI-Demo-DeepSeek # 应用名称

  ai:
    openai:
      # DeepSeek申请的 apikey
      api-key: 【你申请的 apikey】
      # DeepSeek的OpenAI式端点
      base-url: https://api.deepseek.com
      chat:
        options:
          # 配置使用的聊天模型,这里是DeepSeek V3,# 如果想切换为推理模型(DeepSeek-R1),改成 deepseek-reasoner
          model: deepseek-chat
          # 用来控制文本生成的随机性(创造力),值越小越严谨
          temperature: 0.8

5.开发对应的接口

package com.cv.springaidemodeepseek.controller;

import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 测试
 * @Author: funnywus
 * @Date: 2025/4/28 23:01
 */
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/ai")
public class AiController {

    private final OpenAiChatModel openAiChatModel;

    public AiController(OpenAiChatModel openAiChatModel) {
        this.openAiChatModel = openAiChatModel;
    }


    /**
     * 文本对话接口
     * http://localhost:8881/ai/chat/讲一个笑话
     * @param message 用户消息
     * @return AI 回复
     */
    @GetMapping("/chat/{message}")
    public String chat(@PathVariable("message") String message) {
        return this.openAiChatModel.call(message);
    }
}

6.到这儿我们一个简单的AI应用已经开发完成了,最终项目结构如下

实战测试

启动服务,我们只需要在浏览器中输入:
http://localhost:8881/ai/chat/【输入给ai的对话】

示例:http://localhost:8881/ai/chat/讲一个笑话

总结

如何学习更多AI的技能

https://github.com/springaialibaba/spring-ai-alibaba-examples

此仓库中包含许多 Example 来介绍 Spring AI Alibaba 从基础到高级的各种用法和 AI 项目的最佳实践。

感兴趣的小伙伴可以自己到上面GitHub仓库看代码研究哦~🚀👀
本期内容到这儿就结束了!🎉
希望对您有所帮助,我们下期再见!👋😊

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非鱼牛马社

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值