SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn)
引言
DeepSeek最近異?;鸨?,作為深度求索公司提供的大模型,提供了強(qiáng)大的自然語言處理和其他AI功能,通過調(diào)用其接口,可以在Spring Boot項(xiàng)目中實(shí)現(xiàn)智能對(duì)話、內(nèi)容生成等多種功能。本文將詳細(xì)介紹如何在Spring Boot中調(diào)用DeepSeek接口,并給出詳細(xì)的介入步驟和代碼示例。
1、申請(qǐng)DeepSeek API Key
在調(diào)用DeepSeek接口之前,首先需要申請(qǐng)一個(gè)API Key。這是訪問DeepSeek API的憑證,用于驗(yàn)證請(qǐng)求者的身份和權(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和請(qǐng)求URL。
添加以下配置:
deepseek:
api:
key: sk-63************5f # 替換為你的DeepSeek API Key
url: https://api.deepseek.com/chat/completions # DeepSeek API請(qǐng)求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 編寫請(qǐng)求/響應(yīng)模型
在調(diào)用DeepSeek接口時(shí),我們需要定義請(qǐng)求和響應(yīng)的數(shù)據(jù)結(jié)構(gòu)。根據(jù)DeepSeek API的文檔,請(qǐng)求體通常包含模型名稱、消息列表等字段,而響應(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請(qǐng)求,并處理響應(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請(qǐng)求,并調(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 測(cè)試與驗(yàn)證
完成以上步驟后,我們可以啟動(dòng)Spring Boot應(yīng)用,并通過瀏覽器或Postman等工具來測(cè)試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)造測(cè)試請(qǐng)求:
使用Postman發(fā)送GET請(qǐng)求:
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) 異常場(chǎng)景測(cè)試:
- 例如:無效API Key測(cè)試:應(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ì)話功能。首先,需要申請(qǐng)DeepSeek API Key并創(chuàng)建Spring Boot項(xiàng)目。接著,在application.yml中配置API Key和請(qǐng)求URL,并編寫配置類來管理這些配置。然后,定義請(qǐng)求/響應(yīng)模型,編寫服務(wù)類使用RestTemplate發(fā)送HTTP請(qǐng)求并處理響應(yīng)。最后,編寫控制器類處理HTTP請(qǐng)求,并測(cè)試驗(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)容請(qǐng)搜索腳本之家以前的文章或繼續(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實(shí)現(xiàn)1M圖片壓縮優(yōu)化到100kb實(shí)現(xiàn)示例
這篇文章主要為大家介紹了java實(shí)現(xiàn)1M圖片壓縮優(yōu)化到100kb示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
Spring Boot中l(wèi)ombok的安裝與使用詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot中l(wèi)ombok安裝與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Java+Selenium實(shí)現(xiàn)控制瀏覽器的啟動(dòng)選項(xiàng)Options
這篇文章主要為大家詳細(xì)介紹了如何使用java代碼利用selenium控制瀏覽器的啟動(dòng)選項(xiàng)Options的代碼操作,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-01-01
一文詳解Java?Condition的await和signal等待通知機(jī)制
這篇文章主要為大家詳細(xì)介紹了Java?Condition的await和signal等待通知機(jī)制的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2025-02-02
實(shí)現(xiàn)了基于TCP的Java Socket編程實(shí)例代碼
這篇文章主要介紹了基于TCP的Java Socket編程實(shí)例代碼,有需要的朋友可以參考一下2013-12-12
Java程序中添加播放MIDI音樂功能的實(shí)現(xiàn)方法詳解
本篇文章是對(duì)在Java程序中添加播放MIDI音樂功能的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

