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

SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn)

 更新時(shí)間:2025年02月28日 09:12:01   作者:拾光編程  
本文主要介紹了SpringBoot調(diào)用DeepSeek接口的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

引言

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è)試:
    deepseek.api.key=sk-invalid_key
    
    應(yīng)收到401 Unauthorized錯(cuò)誤:
    {
      "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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

油尖旺区| 冕宁县| 新郑市| 财经| 河南省| 洪湖市| 庄浪县| 马关县| 呼伦贝尔市| 交城县| 平泉县| 长白| 巩留县| 涞水县| 民和| 泸西县| 岳阳市| 揭东县| 营山县| 望城县| 调兵山市| 综艺| 化隆| 惠来县| 嘉祥县| 石城县| 盐山县| 佛山市| 肥乡县| 雷州市| 宁晋县| 宁都县| 云龙县| 达拉特旗| 泸州市| 抚宁县| 黄冈市| 依安县| 乐昌市| 子洲县| 高碑店市|