SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn)
引言
DeepSeek最近異?;鸨鳛樯疃惹笏鞴咎峁┑拇竽P?,提供了強(qiáng)大的自然語言處理和其他AI功能,通過調(diào)用其接口,可以在Spring Boot項(xiàng)目中實(shí)現(xiàn)智能對(duì)話、內(nèi)容生成等多種功能。本文將詳細(xì)介紹如何在Spring Boot中調(diào)用DeepSeek接口,并給出詳細(xì)的介入步驟和代碼示例。
1、申請DeepSeek API Key
在調(diào)用DeepSeek接口之前,首先需要申請一個(gè)API Key。這是訪問DeepSeek API的憑證,用于驗(yàn)證請求者的身份和權(quán)限。
1) 訪問DeepSeek官網(wǎng):
打開瀏覽器,輸入DeepSeek的官網(wǎng)地址(如https://platform.deepseek.com/usage),進(jìn)入DeepSeek的開放平臺(tái)頁面。
2) 創(chuàng)建API Key:
在開放平臺(tái)頁面中,找到API keys相關(guān)選項(xiàng),點(diǎn)擊進(jìn)入API Key管理頁面。點(diǎn)擊“創(chuàng)建API Key”按鈕,根據(jù)提示填寫相關(guān)信息,如應(yīng)用名稱、描述等。創(chuàng)建完成后,系統(tǒng)會(huì)生成一個(gè)唯一的API Key,務(wù)必妥善保存,因?yàn)殛P(guān)閉頁面后將無法再次查看。
2、創(chuàng)建Spring Boot項(xiàng)目
接下來,我們需要?jiǎng)?chuàng)建一個(gè)Spring Boot項(xiàng)目來調(diào)用DeepSeek接口??梢允褂肧pring Initializr(https://start.spring.io/)來快速生成項(xiàng)目結(jié)構(gòu)。
1) 訪問Spring Initializr:
打開瀏覽器,輸入Spring Initializr的地址,進(jìn)入項(xiàng)目生成頁面。
2)配置項(xiàng)目參數(shù):
- Project:選擇項(xiàng)目構(gòu)建工具(如Maven或Gradle),設(shè)置項(xiàng)目語言(Java)、Spring Boot版本等。
- Dependencies:添加必要的依賴項(xiàng)。由于我們需要調(diào)用DeepSeek的HTTP接口,因此需要添加
spring-boot-starter-web
依賴。此外,還可以根據(jù)需要添加其他依賴項(xiàng),如日志框架(spring-boot-starter-logging
)、數(shù)據(jù)庫連接池(spring-boot-starter-data-jpa
)等。
3) 生成項(xiàng)目:
配置完成后,點(diǎn)擊“Generate”按鈕生成項(xiàng)目結(jié)構(gòu)。將生成的項(xiàng)目文件下載到本地,并導(dǎo)入到IDE(如IntelliJ IDEA或Eclipse)中進(jìn)行開發(fā)。
3、 配置application.yml
在Spring Boot項(xiàng)目中,通常使用`application.yml``文件來配置應(yīng)用的相關(guān)參數(shù)。為了調(diào)用DeepSeek接口,我們需要在配置文件中添加DeepSeek的API Key和請求URL。
添加以下配置:
deepseek: api: key: sk-63************5f # 替換為你的DeepSeek API Key url: https://api.deepseek.com/chat/completions # DeepSeek API請求URL
4、編寫配置類
為了更方便地管理DeepSeek API的配置信息,我們可以編寫一個(gè)配置類來讀取application.yml
中的配置。
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Getter; @Configuration @Getter public class DeepSeekConfig { @Value("${deepseek.api.key}") private String apiKey; @Value("${deepseek.api.url}") private String apiUrl; }
5 編寫請求/響應(yīng)模型
在調(diào)用DeepSeek接口時(shí),我們需要定義請求和響應(yīng)的數(shù)據(jù)結(jié)構(gòu)。根據(jù)DeepSeek API的文檔,請求體通常包含模型名稱、消息列表等字段,而響應(yīng)體則包含生成的回復(fù)選項(xiàng)等字段。
import lombok.Data; import java.util.List; @Data public class DeepSeekRequest { private String model; private List<Message> messages; private boolean stream; @Data public static class Message { private String role; private String content; } } @Data public class DeepSeekResponse { private List<Choice> choices; @Data public static class Choice { private Delta delta; @Data public static class Delta { private String content; } } }
6 編寫服務(wù)類
服務(wù)類用于封裝向DeepSeek發(fā)出查詢的過程。我們將使用RestTemplate
來發(fā)送HTTP請求,并處理響應(yīng)結(jié)果。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.util.UriComponentsBuilder; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @Service public class DeepSeekService { @Autowired private RestTemplate restTemplate; @Autowired private DeepSeekConfig deepSeekConfig; private final ObjectMapper objectMapper = new ObjectMapper(); public String askDeepSeek(String question) throws JsonProcessingException { DeepSeekRequest request = new DeepSeekRequest(); request.setModel("deepseek-chat"); request.setStream(false); List<DeepSeekRequest.Message> messages = List.of( new DeepSeekRequest.Message("user", question) ); request.setMessages(messages); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAuthorization("Bearer " + deepSeekConfig.getApiKey()); HttpEntity<String> entity = new HttpEntity<>(objectMapper.writeValueAsString(request), headers); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(deepSeekConfig.getApiUrl()); ResponseEntity<String> response = restTemplate.postForEntity(builder.toUriString(), entity, String.class); if (response.getStatusCode().is2xxSuccessful()) { DeepSeekResponse deepSeekResponse = objectMapper.readValue(response.getBody(), DeepSeekResponse.class); if (deepSeekResponse != null && deepSeekResponse.getChoices() != null && !deepSeekResponse.getChoices().isEmpty()) { return deepSeekResponse.getChoices().get(0).getDelta().getContent(); } } return "No valid response from DeepSeek"; } }
7 編寫控制器類
控制器類用于處理HTTP請求,并調(diào)用服務(wù)類的方法來獲取DeepSeek的響應(yīng)結(jié)果。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.http.ResponseEntity; @RestController public class DeepSeekController { @Autowired private DeepSeekService deepSeekService; @GetMapping("/ask") public ResponseEntity<String> askDeepSeek(@RequestParam String question) { try { String response = deepSeekService.askDeepSeek(question); return ResponseEntity.ok(response); } catch (Exception e) { return ResponseEntity.status(500).body("Error occurred while communicating with DeepSeek: " + e.getMessage()); } } }
8 測試與驗(yàn)證
完成以上步驟后,我們可以啟動(dòng)Spring Boot應(yīng)用,并通過瀏覽器或Postman等工具來測試DeepSeek接口是否調(diào)用成功。
1)啟動(dòng)Spring Boot應(yīng)用:
在IDE中運(yùn)行@SpringBootApplication
主類,觀察控制臺(tái)輸出:
2024-02-20T14:30:00.000+08:00 INFO 8080 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http)
2) 構(gòu)造測試請求:
使用Postman發(fā)送GET請求:
GET http://localhost:8080/ask?question=如何學(xué)習(xí)Spring Boot框架?
3) 驗(yàn)證正常響應(yīng):
應(yīng)收到JSON格式的AI響應(yīng):
{ "content": "學(xué)習(xí)Spring Boot可以從以下幾個(gè)步驟入手...(具體學(xué)習(xí)建議)" }
4) 異常場景測試:
- 例如:無效API Key測試:應(yīng)收到401 Unauthorized錯(cuò)誤:
deepseek.api.key=sk-invalid_key
{ "code": "DEEPSEEK_API_ERROR", "message": "Invalid API Key" }
總結(jié)
本文介紹了如何在Spring Boot項(xiàng)目中調(diào)用DeepSeek接口實(shí)現(xiàn)智能對(duì)話功能。首先,需要申請DeepSeek API Key并創(chuàng)建Spring Boot項(xiàng)目。接著,在application.yml中配置API Key和請求URL,并編寫配置類來管理這些配置。然后,定義請求/響應(yīng)模型,編寫服務(wù)類使用RestTemplate發(fā)送HTTP請求并處理響應(yīng)。最后,編寫控制器類處理HTTP請求,并測試驗(yàn)證接口調(diào)用是否成功。通過這些步驟,可以在Spring Boot項(xiàng)目中輕松集成DeepSeek大模型,實(shí)現(xiàn)智能對(duì)話和內(nèi)容生成等功能。
到此這篇關(guān)于SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot調(diào)用DeepSeek接口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot兩種方式接入DeepSeek的實(shí)現(xiàn)
- SpringBoot快速接入DeepSeek?api(帶頁面)保姆級(jí)教程
- SpringBoot或SpringAI對(duì)接DeepSeek大模型的詳細(xì)步驟
- SpringBoot接入deepseek深度求索示例代碼(jdk1.8)
- SpringBoot整合DeepSeek實(shí)現(xiàn)AI對(duì)話功能
- SpringBoot調(diào)用DeepSeek?API的完整操作指南
- springboot接入deepseek深度求索代碼示例(java版)
- springboot集成Deepseek4j的項(xiàng)目實(shí)踐
相關(guān)文章
JAVA“無法驗(yàn)證證書。將不執(zhí)行該應(yīng)用程序?!碧崾窘鉀Q辦法
這篇文章主要給大家介紹了關(guān)于JAVA“無法驗(yàn)證證書,將不執(zhí)行該應(yīng)用程序”提示的解決辦法,要解決Java無法驗(yàn)證證書的問題,可以嘗試下本文的方法,需要的朋友可以參考下2024-03-03Java項(xiàng)目防止SQL注入的幾種方法總結(jié)
SQL注入是比較常見的網(wǎng)絡(luò)攻擊方式之一,在客戶端在向服務(wù)器發(fā)送請求的時(shí)候,sql命令通過表單提交或者url字符串拼接傳遞到后臺(tái)持久層,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java項(xiàng)目防止SQL注入的幾種方法,需要的朋友可以參考下2023-04-04SpringBoot整合Swagger3生成接口文檔過程解析
這篇文章主要介紹了SpringBoot整合Swagger3生成接口文檔過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Mybatis-Plus實(shí)現(xiàn)多主鍵批量保存及更新詳情
這篇文章主要介紹了Mybatis-Plus實(shí)現(xiàn)多主鍵批量保存及更新詳情,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09idea2019導(dǎo)入maven項(xiàng)目中的某些問題及解決方法
這篇文章主要介紹了idea2019導(dǎo)入maven項(xiàng)目中的某些問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08springboot項(xiàng)目不同環(huán)境的配置讀取方式
SpringBoot支持application.properties、application.yml、application.yaml三種配置文件類型,可同時(shí)存在并合并配置,配置文件的讀取優(yōu)先級(jí)為:application.properties > application.yml > application.yaml,不同位置的相同類型配置文件2024-11-11Springboot的spring-boot-maven-plugin導(dǎo)入失敗的解決方案
這篇文章主要介紹了Springboot的spring-boot-maven-plugin導(dǎo)入失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07詳解Spring-Cloud2.0之Feign調(diào)用遠(yuǎn)程服務(wù)指南
這篇文章主要介紹了詳解Spring-Cloud2.0之Feign調(diào)用遠(yuǎn)程服務(wù)指南,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01