最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在SpringBoot項目中接入DeepSeek大模型的完整流程

 更新時間:2026年07月21日 09:23:41   作者:錢六兩  
想用SpringBoot3和SpringAI接入DeepSeek大模型嗎,這篇教程從JDK21環(huán)境搭建、項目創(chuàng)建到整合Hutool和Knife4j,再到配置ChatClient實現(xiàn)多會話記憶對話,一步步帶你跑通完整流程,輕松上手AI開發(fā),需要的朋友可以參考下

一、基礎(chǔ)環(huán)境準(zhǔn)備

  • 安裝的 JDK 版本必須是 17 或 21,不能選擇其他版本! 因為項目使用最新版本的 Spring Boot 3 和 Spring AI 開發(fā)框架。

推薦使用 21 版本,因為支持虛擬線程這個王炸功能。

二、新建項目

在 IDEA 中新建項目,選擇 Spring Initializr 模板,注意需要確保 Server URL 為 https://start.spring.io/。

Java 版本選擇 21。

選擇 Spring Boot 3.4.4 版本,可以根據(jù)自己的需要添加一些依賴,比如 Spring Web 和 Lombok。

當(dāng)然,后續(xù)通過修改 Maven 配置添加依賴也是可以的。

點擊創(chuàng)建,就得到了一個 Spring Boot 項目,需要等待 Maven 為我們安裝依賴。

小提示,如果 Lombok 依賴報錯的話,可以手動指定 Lombok 的版本,pom.xml 代碼如下:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.36</version>
    <optional>true</optional>
</dependency>

三、整合依賴

可以根據(jù)自己的需要,整合一些開發(fā)項目常用的依賴。此處我們整合 Hutool 工具庫和 Knife4j 接口文檔即可。

1、Hutool 工具庫

Hutool 是主流的 Java 工具類庫,集合了豐富的工具類,涵蓋字符串處理、日期操作、文件處理、加解密、反射、正則匹配等常見功能。

參考官方文檔引入:https://doc.hutool.cn/pages/index/#%F0%9F%8D%8Amaven

在 Maven 的 pom.xml 中添加依賴:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.37</version>
</dependency>

2、Knife4j 接口文檔

Knife4j 是基于 Swagger 接口文檔的增強工具,提供了更加友好的 API 文檔界面和功能擴展,例如動態(tài)參數(shù)調(diào)試、分組文檔等。

參考 官方文檔 引入,注意我們使用的是 Spring Boot 3.x。

1)在 Maven 的 pom.xml 中添加依賴:

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
    <version>4.4.0</version>
</dependency>

2)新建 controller 包用于存放 API 接口,編寫一個健康檢查接口用于測試接口文檔是否正常引入:

@RestController
@RequestMapping("/health")
public class HealthController {

  @GetMapping("/check")
  public String checkHealth(){
    return "OK";
  }
}

3)在 application.yml 中追加接口文檔配置,掃描 controller 包:

server:
  port: 8080
  servlet:
    context-path: /api
######################################################
############          接口文檔配置           #####################
######################################################
# 訪問地址   ${server.servlet.context-path}/doc.html
springdoc:
  swagger-ui:
    path: /swagger-ui.html
    tags-sorter: alpha
    operations-sorter: alpha
  api-docs:
    path: /v3/api-docs
  group-configs:
    - group: 'default'
      paths-to-match: '/**'
      packages-to-scan: com.qianliuliang.love.controller # 配置掃描哪些包的接口要加入到文檔
# knife4j config
knife4j:
  enable: true
  setting:
    language: zh_cn

4)啟動項目,訪問 http://localhost:8080/api/doc.html 能夠看到接口文檔。

如果是要開發(fā)完整項目,除了引入依賴之外,還要編寫一些通用基礎(chǔ)代碼,比如自定義異常、響應(yīng)包裝類、全局異常處理器、請求包裝類、全局跨域配置等。

5)驗證項目是否正常,直接在接口文檔發(fā)起調(diào)試,成功返回"OK",說明項目初始化沒有問題

四、大模型接入

使用SpringAI接口DeepSeek模型(需準(zhǔn)備DeepSeek ApiKey)

1)在pom.xml中引入依賴

 <!--整合LLM-->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

2)在DeepSeek官網(wǎng)中申請APIKey
官網(wǎng)地址 https://www.deepseek.com/
操作步驟如下:

點擊<API開放平臺>

依次點擊 、<創(chuàng)建API key>

創(chuàng)建號API key后妥善保管,也不要提交到遠(yuǎn)端

3)在application.yaml 添加模型配置

spring:
  application:
    name: AI_LOVE
  #ds模型配置
  ai:
    openai:
      api-key: 你的apikey
      base-url: https://api.deepseek.com
      chat:
        options:
          model: deepseek-chat

api-key: 你的apikey 把申請的key填入

4)配置ChatClient bean

  • 在項目中創(chuàng)建package 名稱為:config;在包下面創(chuàng)建類,內(nèi)容如下:
package com.qianliuliang.love.config;

import com.qianliuliang.love.advisor.MyLoggerAdvisor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * chatClient bean
 */
@Configuration
public class Client {


    /**
     * 基于內(nèi)存會話記憶的  client
     *
     * @param openAiChatModel
     * @return
     */
    @Bean
    public ChatClient chatClientWithChatMemory(OpenAiChatModel openAiChatModel) {
        // 基于內(nèi)存的會話記憶,可實現(xiàn)上下文記憶會話
        ChatMemory chatMemory = new InMemoryChatMemory();
        return ChatClient.builder(openAiChatModel)
                .defaultSystem("你是一個智能戀愛助手!名字叫LoveDD") // 系統(tǒng)提示詞
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory),
                        new MyLoggerAdvisor()) // 自定義日志
                .build();
    }

}
  • 創(chuàng)建chat對話
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 簡單會話
 */
@Slf4j
@Component
public class SimpleChat {

    @Autowired
    private ChatClient chatClient; 

    /**
     * 普通文本會話
     *
     * @param msg 用戶消息
     * @param chatId 會話id
     * @return
     */
    public String doChatWithString(@RequestParam String msg, String chatId) {
        ChatResponse chatResponse = chatClient.prompt()
                .advisors(advisorSpec ->
                        advisorSpec.param("chat_memory_conversation_id", chatId) //會話id/聊天室id
                        .param("chat_memory_response_size", 10))// 支持10輪會話
                .user(msg)
                //.advisors(new SimpleLoggerAdvisor())//日志打印
                .call()
                .chatResponse();
        return chatResponse.getResult().getOutput().getText();// 返回與大模型的對話的內(nèi)容
    }
}
  • 創(chuàng)建controller
import com.qianliuliang.love.chat.SimpleChat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/chat")
public class ChatController {


    @Autowired
    private SimpleChat simpleChat;

    /**
     * 普通文本會話
     *
     * @param msg
     * @param chatId
     * @return
     */
    @PostMapping("/doChatWithString")
    public String doChatWithString( String msg, String chatId) {
        return simpleChat.doChatWithString(msg, chatId);
    }
}
  • 重啟項目,測試

在會話ID為1的會話中(聊天室1),告訴大模型"我是六兩"

在會話ID為2的會話中(聊天室2),問大模型"我是誰",此時大模型不知道我是誰,因為在不同的聊天室

在會話ID為1的會話中(回到聊天室1),問大模型"我是誰",此時大模型知道我是六兩“”,因為在聊天室1中我告訴過大模型“我是六兩”【此時大模型有記憶】

好了,到這里就結(jié)束了。

以上就是在SpringBoot項目中接入DeepSeek大模型的完整流程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot接入DeepSeek大模型的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring Security學(xué)習(xí)之rememberMe自動登錄的實現(xiàn)

    Spring Security學(xué)習(xí)之rememberMe自動登錄的實現(xiàn)

    這篇文章主要給大家介紹了關(guān)于Spring Security學(xué)習(xí)之rememberMe自動登錄的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 解決String字符串轉(zhuǎn)JSONObject順序不對的問題

    解決String字符串轉(zhuǎn)JSONObject順序不對的問題

    這篇文章主要介紹了解決String字符串轉(zhuǎn)JSONObject順序不對的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • spring boot 枚舉使用的坑整理

    spring boot 枚舉使用的坑整理

    在本篇文章里我們給大家整理了關(guān)于spring boot 枚舉使用的坑以及相關(guān)知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • 淺談Spring-boot事件監(jiān)聽

    淺談Spring-boot事件監(jiān)聽

    這篇文章主要介紹了淺談Spring-boot事件監(jiān)聽,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 詳解SpringMVC中的異常處理

    詳解SpringMVC中的異常處理

    這篇文章主要介紹了SpringMVC中的異常處理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用SpringMVC,感興趣的朋友可以了解下
    2021-03-03
  • mybatis時間范圍查詢代碼示例

    mybatis時間范圍查詢代碼示例

    這篇文章主要給大家介紹了關(guān)于mybatis時間范圍查詢的相關(guān)資料,在項?中避免不了要?到時間范圍查詢,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Java中自動生成構(gòu)造方法詳解

    Java中自動生成構(gòu)造方法詳解

    這篇文章主要介紹了Java中自動生成構(gòu)造方法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 淺談線性表的原理及簡單實現(xiàn)方法

    淺談線性表的原理及簡單實現(xiàn)方法

    下面小編就為大家?guī)硪黄獪\談線性表的原理及簡單實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 淺析對java枚舉類型的認(rèn)識

    淺析對java枚舉類型的認(rèn)識

    在本文里我們給大家分享了關(guān)于對java枚舉類型的認(rèn)識和相關(guān)知識點內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。
    2019-03-03
  • Eclipse中自動添加注釋(兩種)

    Eclipse中自動添加注釋(兩種)

    本文主要介紹了Eclipse中自動添加注釋的兩種方法。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02

最新評論

银川市| 修武县| 磐石市| 花莲县| 连南| 白银市| 乐昌市| 西盟| 庐江县| 盐边县| 易门县| 鹤庆县| 盐池县| 汾阳市| 玛多县| 图木舒克市| 新巴尔虎左旗| 和田市| 平凉市| 辉县市| 宁津县| 邵东县| 宣化县| 咸阳市| 咸丰县| 辰溪县| 贵德县| 常山县| 会同县| 迁安市| 大城县| 金平| 丰镇市| 徐闻县| 宜君县| 高密市| 景洪市| 哈巴河县| 沙雅县| 九江县| 乐陵市|