当前位置: 首页 > article >正文

23. AI-大语言模型-DeepSeek赋能开发-Spring AI集成

文章目录

  • 前言
  • 一、Spring AI 集成 DeepSeek
    • 1. 开发AI程序
    • 2. DeepSeek 大模型
    • 3. 集成 DeepSeek 大模型
      • 1. 接入前准备
      • 2. 引入依赖
      • 3. 工程配置
      • 4. 调用示例
      • 5. 小结
    • 4. 集成第三方平台(已集成 DeepSeek 大模型)
      • 1. 接入前准备
      • 2. POM依赖
      • 3. 工程配置
      • 4. 调用示例
      • 5. 调用测试
      • 6. 小结
    • 5. 集成 DeepSeek4j 1.4版本
      • 1. 为什么需要 DeepSeek4j?
      • 2. 依赖
      • 3. 配置
      • 4. 示例


前言

DeepSeek赋能

DeepSeek 是深度求索公司发布的大模型,是国产之光。大家应该学会如何使用 DeepSeek 大模型,本文主要探讨,如何开发基于 DeepSeek 大模型的智能应用。


一、Spring AI 集成 DeepSeek

接下来深入了解如何使用 Spring Boot 和 DeepSeek 开发 AI 程序。

1. 开发AI程序

  在当今数字化时代,人工智能(AI)已成为推动技术进步和创新的核心力量。从智能语音助手到图像识别系统,从个性化推荐引擎到自动化流程,AI 的应用无处不在,正深刻地改变着我们的生活和工作方式。

  与此同时,软件开发领域也在不断演进,以适应快速变化的技术需求和业务场景。Spring Boot 作为 Java 生态系统中最受欢迎的框架之一,以其 “约定优于配置” 的理念和丰富的功能,为开发者提供了一种高效、便捷的方式来构建企业级应用程序。

  DeepSeek 则是 AI 领域的一颗新星,致力于开发先进的大语言模型(LLM)和相关技术 。它的出现为 AI 技术的发展注入了新的活力,其模型在性能和成本效益方面展现出了卓越的优势,在多项测试中表现出色,甚至超越了一些行业领先的模型,且设计成本相对较低。

  Spring Boot 的强大功能和便捷性,使得开发者能够快速搭建稳定的后端服务,而 DeepSeek 的先进大语言模型则为应用赋予了强大的智能交互和处理能力。通过将 DeepSeek 的 AI 能力集成到 Spring Boot 应用中,我们可以轻松实现智能聊天机器人、智能文档处理、智能代码生成等各种创新应用,为用户提供更加智能化、个性化的服务体验。

2. DeepSeek 大模型

DeepSeek 推出两款模型:

  • DeepSeek V 系列,对于V系列主要 对话,模型名称:deepseek-chat
  • DeepSeek R 系统,对于R系统主要 推理, 模型名称:deepseek-reasoner

DeepSeek 官方更新日志,可以看到模型发布和演化的过程。

https://api-docs.deepseek.com/zh-cn/updates

3. 集成 DeepSeek 大模型

  DeepSeek AI提供开源的 DeepSeek V3 模型,该模型以其尖端的推理和解决问题的能力而闻名。

  Spring AI 通过重用现有的 OpenAI 客户端与 DeepSeek AI 集成。首先,需要获取 DeepSeek API 密钥,配置基本 URL,并选择其中一个受支持的模型。

SpringAI集成DS

1. 接入前准备

  • 创建 API 密钥:
    访问此处:https://api-docs.deepseek.com/zh-cn/,创建 API 密钥。
    使用 Spring AI 项目中的 spring.ai.openai.api-key 属性对其进行配置。

  • 设置 DeepSeek 基本 URL:
    将 spring.ai.openai.base-url 属性设置为 api.deepseek.com。

  • 选择 DeepSeek 模型:
    使用属性 spring.ai.openai.chat.model= 指定模型。有关可用选项,请参阅支持的型号。

2. 引入依赖

<dependency>
- <groupId>org.springframework.ai</groupId>
- <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

3. 工程配置

spring:
  ai:
- openai:
-   api-key: sk-xxx   # 填写自己申请的key
-   base-url: https://api.deepseek.com
-   chat:
- - options:
- -   model: deepseek-chat

4. 调用示例

package com.demo.controller;

import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Map;


@RestController
public class ChatController {

- private final OpenAiChatModel chatModel;
- 
- public ChatController(OpenAiChatModel chatModel) {
- - this.chatModel = chatModel;
- }

- @GetMapping("/ai/generate")
- public Map<String, String> generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
- - return Map.of("generation", this.chatModel.call(message));
- }

- @GetMapping("/ai/generateStream")
- public Flux<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
- - Prompt prompt = new Prompt(new UserMessage(message));
- - return this.chatModel.stream(prompt);
- }
}

5. 小结

  Spring AI 接入 DeepSeek 大模型是非常简单的,实现了阻塞和流式聊天模式。
  对于 DeepSeek 大模型的函数调用,角色定义以及结构化输出等是一致的。

4. 集成第三方平台(已集成 DeepSeek 大模型)

1. 接入前准备

硅基流动平台,注册地址 如下:

https://cloud.siliconflow.cn/i/pCa1dBVX

  1. 选择一个对话功能的免费模型,如果你想用其他,生图,视频,语音相关,里面也可以自行选择。

硅基流动平台
2. 硅基流动官网注册后后,点击API密钥菜单,生成密钥,点击复制。

创建API密钥

2. POM依赖

- <properties>
- - <java.version>17</java.version>
- - <spring-ai.version>1.0.0-M5</spring-ai.version>
- - <maven.compiler.source>17</maven.compiler.source>
- - <maven.compiler.target>17</maven.compiler.target>
- </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>
- </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>

3. 工程配置

spring:
  ai:
- openai:
-   api-key: # 这里是你自己的api key
-   base-url: https://api.siliconflow.cn
-   chat:
- - options:
- -   model: deepseek-ai/DeepSeek-R1-Distill-Llama-8B

4. 调用示例

package com.demo.controller;

import groovy.util.logging.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*")
@Slf4j
public class ChatBotController {

- private final ChatClient chatClient;

- public ChatBotController(ChatClient.Builder builder) {
- - this.chatClient = builder.defaultSystem("你是一个天气预报员,当有人输入日期的时候,你输出北京的天气预报信息," +
- - - - "生成结果在html页面中以markdown的格式输出,最后输出结尾的时候始终以下面的语句结尾:感谢您的咨询,我是舆情君。").build();
- }

- @GetMapping(value = "ai/chat/{message}")
- public String chat(@PathVariable("message") String message) {
- - return chatClient.prompt()
- - - - .user(message)
- - - - .call()
- - - - .content();
- }
}

5. 调用测试

启动项目,地址栏输入:http://localhost:8080/ai/chat/2025年2月18日

6. 小结

以上是简单的演示,项目中可以直接写程序,通过大模型的能力,直接以json的格式输出,系统执行之后,直接插入数据库,也可以做到数据采集,助力企业的项目。

5. 集成 DeepSeek4j 1.4版本

1. 为什么需要 DeepSeek4j?

DeepSeek4J 是专为 Java 生态打造的 DeepSeek 模型集成框架。其 API 设计简洁优雅,仅需一行代码,即可完成 DeepSeek 的接入。

现有框架的局限性

  • 思维链内容丢失:R1 最核心的推理过程完全被忽略。
  • 响应模式不兼容:无法处理“思考在前、结论在后”的输出模式。
  • 参数限制:temperature、top_p 等关键参数设置失效。
  • 流式处理不完善:用户体验欠佳。

解决方案
面向 DeepSeek 的开箱即用方案——DeepSeek4j。

  • 增强支持 DeepSeek 独有的思维链和账单特性。
  • 增加 Project Reactor 的全面响应式支持。
  • 提供集成 Spring Boot Starter,支持自动配置。

2. 依赖

3. 配置

4. 示例


本文的引用仅限自我学习如有侵权,请联系作者删除。
参考知识
如何用Spring AI 结合 DeepSeek开发你的第一个AI程序?
Spring 宣布接入 DeepSeek!!
DeepSeek4J 再更新!Java 项目一行代码集成 DeepSeek !!



http://www.kler.cn/a/554501.html

相关文章:

  • 自适应SQL计划管理(Adaptive SQL Plan Management)在Oracle 12c中的应用
  • 企业软件合规性管理:构建高效、安全的软件资产生态
  • Linux系统配置阿里云yum源,安装docker
  • mac开发环境配置笔记
  • 什么是跨站脚本攻击(XSS)?
  • python:多重继承、MRO(方法解析顺序)
  • 茶叶叶片叶子品相识别检测数据集VOC+YOLO格式5631张2类别
  • TensorFlow深度学习实战——构建卷积神经网络实现CIFAR-10图像分类
  • 2025华为OD机试真题-猜字谜(C++)-E卷-100分
  • Ubuntu 服务器Llama Factory 搭建DeepSeek-R1微调训练环境
  • 【分布式理论16】分布式调度2:资源划分和调度策略
  • Zookeeper应用案例-服务器列表动态更新实现
  • 网关断网缓存:让网络连接更可靠
  • 电脑机箱散热风扇声音大的影响因素
  • 学习记录 DAY10 Tensorflow,神经网络视觉
  • 工控自动化领域:数字量信号与模拟量信号的差异解析
  • 2024华为OD机试真题-恢复数字序列(C++/Java/Python)-E卷-100分
  • 深入理解 Rust 中的 `Box<T>`:堆上的数据与递归类型
  • Grok 3 官宣免费使用
  • 2025鸿蒙开发面试题汇总——通俗易懂