大唐工作室 網(wǎng)站制作天貓seo搜索優(yōu)化
文章目錄
- 前言
- 一、Spring AI 集成 DeepSeek
- 1. 開發(fā)AI程序
- 2. DeepSeek 大模型
- 3. 集成 DeepSeek 大模型
- 1. 接入前準(zhǔn)備
- 2. 引入依賴
- 3. 工程配置
- 4. 調(diào)用示例
- 5. 小結(jié)
- 4. 集成第三方平臺(已集成 DeepSeek 大模型)
- 1. 接入前準(zhǔn)備
- 2. POM依賴
- 3. 工程配置
- 4. 調(diào)用示例
- 5. 調(diào)用測試
- 6. 小結(jié)
- 5. 集成 DeepSeek4j 1.4版本
- 1. 為什么需要 DeepSeek4j?
- 2. 依賴
- 3. 配置
- 4. 示例
前言
DeepSeek賦能
?
DeepSeek 是深度求索公司發(fā)布的大模型,是國產(chǎn)之光。大家應(yīng)該學(xué)會如何使用 DeepSeek 大模型,本文主要探討,如何開發(fā)基于 DeepSeek 大模型的智能應(yīng)用。
一、Spring AI 集成 DeepSeek
接下來深入了解如何使用 Spring Boot 和 DeepSeek 開發(fā) AI 程序。
1. 開發(fā)AI程序
??在當(dāng)今數(shù)字化時代,人工智能(AI)已成為推動技術(shù)進(jìn)步和創(chuàng)新的核心力量。從智能語音助手到圖像識別系統(tǒng),從個性化推薦引擎到自動化流程,AI 的應(yīng)用無處不在,正深刻地改變著我們的生活和工作方式。
??與此同時,軟件開發(fā)領(lǐng)域也在不斷演進(jìn),以適應(yīng)快速變化的技術(shù)需求和業(yè)務(wù)場景。Spring Boot 作為 Java 生態(tài)系統(tǒng)中最受歡迎的框架之一,以其 “約定優(yōu)于配置” 的理念和豐富的功能,為開發(fā)者提供了一種高效、便捷的方式來構(gòu)建企業(yè)級應(yīng)用程序。
??DeepSeek 則是 AI 領(lǐng)域的一顆新星,致力于開發(fā)先進(jìn)的大語言模型(LLM)和相關(guān)技術(shù) 。它的出現(xiàn)為 AI 技術(shù)的發(fā)展注入了新的活力,其模型在性能和成本效益方面展現(xiàn)出了卓越的優(yōu)勢,在多項(xiàng)測試中表現(xiàn)出色,甚至超越了一些行業(yè)領(lǐng)先的模型,且設(shè)計成本相對較低。
??Spring Boot 的強(qiáng)大功能和便捷性,使得開發(fā)者能夠快速搭建穩(wěn)定的后端服務(wù),而 DeepSeek 的先進(jìn)大語言模型則為應(yīng)用賦予了強(qiáng)大的智能交互和處理能力。通過將 DeepSeek 的 AI 能力集成到 Spring Boot 應(yīng)用中,我們可以輕松實(shí)現(xiàn)智能聊天機(jī)器人、智能文檔處理、智能代碼生成等各種創(chuàng)新應(yīng)用,為用戶提供更加智能化、個性化的服務(wù)體驗(yàn)。
2. DeepSeek 大模型
DeepSeek 推出兩款模型:
- DeepSeek V 系列,對于V系列主要 對話,模型名稱:deepseek-chat
- DeepSeek R 系統(tǒng),對于R系統(tǒng)主要 推理, 模型名稱:deepseek-reasoner
DeepSeek 官方更新日志,可以看到模型發(fā)布和演化的過程。
https://api-docs.deepseek.com/zh-cn/updates
3. 集成 DeepSeek 大模型
??DeepSeek AI提供開源的 DeepSeek V3 模型,該模型以其尖端的推理和解決問題的能力而聞名。
??Spring AI 通過重用現(xiàn)有的 OpenAI 客戶端與 DeepSeek AI 集成。首先,需要獲取 DeepSeek API 密鑰,配置基本 URL,并選擇其中一個受支持的模型。
1. 接入前準(zhǔn)備
-
創(chuàng)建 API 密鑰:
訪問此處:https://api-docs.deepseek.com/zh-cn/,創(chuàng)建 API 密鑰。
使用 Spring AI 項(xiàng)目中的 spring.ai.openai.api-key 屬性對其進(jìn)行配置。 -
設(shè)置 DeepSeek 基本 URL:
將 spring.ai.openai.base-url 屬性設(shè)置為 api.deepseek.com。 -
選擇 DeepSeek 模型:
使用屬性 spring.ai.openai.chat.model= 指定模型。有關(guān)可用選項(xiàng),請參閱支持的型號。
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. 調(diào)用示例
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. 小結(jié)
??Spring AI 接入 DeepSeek 大模型是非常簡單的,實(shí)現(xiàn)了阻塞和流式聊天模式。
??對于 DeepSeek 大模型的函數(shù)調(diào)用,角色定義以及結(jié)構(gòu)化輸出等是一致的。
4. 集成第三方平臺(已集成 DeepSeek 大模型)
1. 接入前準(zhǔn)備
硅基流動平臺,注冊地址 如下:
https://cloud.siliconflow.cn/i/pCa1dBVX
- 選擇一個對話功能的免費(fèi)模型,如果你想用其他,生圖,視頻,語音相關(guān),里面也可以自行選擇。
2. 硅基流動官網(wǎng)注冊后后,點(diǎn)擊API密鑰菜單,生成密鑰,點(diǎn)擊復(fù)制。
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. 調(diào)用示例
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("你是一個天氣預(yù)報員,當(dāng)有人輸入日期的時候,你輸出北京的天氣預(yù)報信息," +
- - - - "生成結(jié)果在html頁面中以markdown的格式輸出,最后輸出結(jié)尾的時候始終以下面的語句結(jié)尾:感謝您的咨詢,我是輿情君。").build();
- }- @GetMapping(value = "ai/chat/{message}")
- public String chat(@PathVariable("message") String message) {
- - return chatClient.prompt()
- - - - .user(message)
- - - - .call()
- - - - .content();
- }
}
5. 調(diào)用測試
啟動項(xiàng)目,地址欄輸入:http://localhost:8080/ai/chat/2025年2月18日
6. 小結(jié)
以上是簡單的演示,項(xiàng)目中可以直接寫程序,通過大模型的能力,直接以json的格式輸出,系統(tǒng)執(zhí)行之后,直接插入數(shù)據(jù)庫,也可以做到數(shù)據(jù)采集,助力企業(yè)的項(xiàng)目。
5. 集成 DeepSeek4j 1.4版本
1. 為什么需要 DeepSeek4j?
DeepSeek4J 是專為 Java 生態(tài)打造的 DeepSeek 模型集成框架。其 API 設(shè)計簡潔優(yōu)雅,僅需一行代碼,即可完成 DeepSeek 的接入。
現(xiàn)有框架的局限性
- 思維鏈內(nèi)容丟失:R1 最核心的推理過程完全被忽略。
- 響應(yīng)模式不兼容:無法處理“思考在前、結(jié)論在后”的輸出模式。
- 參數(shù)限制:temperature、top_p 等關(guān)鍵參數(shù)設(shè)置失效。
- 流式處理不完善:用戶體驗(yàn)欠佳。
解決方案
面向 DeepSeek 的開箱即用方案——DeepSeek4j。
- 增強(qiáng)支持 DeepSeek 獨(dú)有的思維鏈和賬單特性。
- 增加 Project Reactor 的全面響應(yīng)式支持。
- 提供集成 Spring Boot Starter,支持自動配置。
2. 依賴
3. 配置
4. 示例
本文的引用僅限自我學(xué)習(xí)如有侵權(quán),請聯(lián)系作者刪除。
參考知識
如何用Spring AI 結(jié)合 DeepSeek開發(fā)你的第一個AI程序?
Spring 宣布接入 DeepSeek!!
DeepSeek4J 再更新!Java 項(xiàng)目一行代碼集成 DeepSeek !!