在SpringBoot項目中接入DeepSeek大模型的完整流程
一、基礎(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_cn4)啟動項目,訪問 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-chatapi-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)
這篇文章主要給大家介紹了關(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順序不對的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

