SpringBoot系列之Spring AI+DeekSeek创建AI应用

devtools/2025/3/4 22:39:22/

AI__DeepSeek__AI__1">使用 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.14
  • Spring Boot 3.2.0
  • IntelliJ IDEA 2022.2.5
  • Maven 3.8.1

AI_Starter__16">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 创建项目

  1. 访问 Spring Initializr:https://start.spring.io/
  2. 选择项目配置
    • Project:Maven
    • Language:Java
    • Spring Boot Version:3.2.0
    • Dependencies
      • Spring Web
      • Spring AI OpenAI Starter
      • Lombok
      • Spring Boot DevTools(可选,用于开发时的热部署)
  3. 生成项目:点击“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://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>

AI__133">三、选择 AI 模型

访问 SiliconFlow 模型列表,选择一个免费的 AI 模型。我们选择 DeepSeek 提供的 DeepSeek-R1-Distill-Llama-8B 模型。
在这里插入图片描述

获取 API 密钥

  1. 登录到 SiliconFlow 平台。
  2. 在平台中创建一个 API 密钥。
  3. 将密钥保存到本地,用于后续配置。
    在这里插入图片描述

配置文件

在项目根目录下创建 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  # 选择的模型

四、编写测试类

创建 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 创建了一个简单的智能对话应用。你可以根据需求进一步扩展功能,例如支持多语言、优化对话逻辑或集成到前端页面中。

如果你在实现过程中遇到任何问题,欢迎在评论区留言讨论。


http://www.ppmy.cn/devtools/164575.html

相关文章

java 项目中设计模式 之单例模式

本文是自己学习单例模式的记录&#xff0c;方便以后使用的时候查看&#xff0c;并不专业哈 一、单例模式 1、特点&#xff1a; 单例模式是最简单的一种模式&#xff0c;具有单例模式只能有一个实例&#xff0c;并且必须为其他对象提供一个访问点。单例模式分为懒汉模式和饥饿…

C语言总结目录

一、数据类型【文章链接】 二、标识符【文章链接】​​​​​​​ #编译GCC 三、程序结构语句 四、输入输出函数 五、数组 六、指针 七、数组指针&&指针数组 八、排序算法 九、结构体 十、链表 十一、宏定义 十二、文件读写 十三、静态库&&动态库…

计算机毕设JAVA——某高校宿舍管理系统(基于SpringBoot+Vue前后端分离的项目)

文章目录 概要项目演示图片系统架构技术运行环境系统功能简介 概要 网络上许多计算机毕设项目开发前端界面设计复杂、不美观&#xff0c;而且功能结构十分单一&#xff0c;存在很多雷同的项目&#xff1a;不同的项目基本上就是套用固定模板&#xff0c;换个颜色、改个文字&…

25年前端如何走的更稳

2025年&#xff0c;随着deepseek引起的AI大模型技术的深度革命&#xff0c;带来了很多机会和挑战&#xff0c;前端程序员作为互联网里一个普通但必不可少的岗位&#xff0c;在当前形势下&#xff0c;需要主动变革才能走的更稳。本文简单介绍三个方向&#xff0c;Web3前端、全栈…

本地部署 DeepSeek-R1大模型详细教程(桌面客户端美观UI)

大家好&#xff01;今天我来分享一篇超级详细的教程&#xff0c;教你如何在本地部署 DeepSeek-R1 大模型&#xff0c;让你的电脑也能成为一个强大的 AI 工作站&#xff01;这篇文章会从零开始&#xff0c;手把手带你完成所有步骤&#xff0c;适合小白操作。废话不多说&#xff…

Vue3 TransitionGroup组件深入解析:结合Element Plus实践指南

引言 在动态列表交互场景中&#xff0c;元素的增删排序需要优雅的过渡效果。Vue3的TransitionGroup组件为这类需求提供了专业解决方案。本文将通过Element Plus等流行UI库的实战案例&#xff0c;深入剖析TransitionGroup的应用技巧。 一、TransitionGroup核心特性 1.1 与Tran…

可终身授权的外国工具,不限次数使用!PDF转CAD的软件

最近有不少朋友问我有没有好用的CAD转换工具&#xff0c;今天就来给大家分享两款超实用的小软件&#xff0c;希望能帮到大家。 第一款软件是一款国外开发的&#xff0c;它专门用来把PDF文件转换成CAD格式&#xff0c;特别方便。 这款软件的操作非常简单&#xff0c;打开后无需安…

el-table input textarea 文本域 自适应高度,切换分页滚动失效处理办法

场景&#xff1a; el-table 表格 需要 input类型是 textarea 高度是自适应&#xff0c;第一页数据都是单行数据 不会产生滚动条&#xff0c;但是第二页数据是多行数据 会产生滚动条&#xff0c; bug: 第一页切换到第二页 第二页滚动条无法展示 解决办法&#xff1a;直接修改样…