Java接入DeepSeek的保姆級(jí)教程(適合新手)
前言
對(duì)于目前的DeepSeek大家應(yīng)該都不是很陌生,目前也是最流行的一款A(yù)I軟件了,所以為了讓我們開發(fā)更全面,能夠在自己的項(xiàng)目中融入AI那就會(huì)很全面了,所以這次的文章,將模擬一個(gè)基礎(chǔ)案例,可以在這個(gè)基礎(chǔ)案例迭代實(shí)現(xiàn)出你自己的AI。
話不多說,也不介紹我的網(wǎng)站了,直接開始進(jìn)行一下流程。
使用的:JDK 17
1、獲取自己在DeepSeek上的token
網(wǎng)站: DeepSeek | 深度求索 ,點(diǎn)擊API開放平臺(tái)找到API keys 獲取自己的key,注意你的key一定要保存好了
2、引入依賴
這個(gè)就不多說了,在你的pom文件中引入相對(duì)應(yīng)的依賴即可。
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>3.4.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>3.4.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.30</version> <scope>provided</scope> </dependency> <!-- HTTP客戶端 --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.0</version> </dependency> <!-- JSON處理 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>com.mashape.unirest</groupId> <artifactId>unirest-java</artifactId> <version>1.4.9</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpasyncclient</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> <version>4.3.6</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency> </dependencies>
3、創(chuàng)建實(shí)體類
@Data @Builder public class DeeseekRequest { private String model; private List<Message> messages; @Data @Builder public static class Message { private String role; private String content; } }
4、創(chuàng)建Controller層
package com.wdc.dk; import com.google.gson.Gson; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; import com.mashape.unirest.http.exceptions.UnirestException; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @RestController public class AIController { private final Gson gson = new Gson(); @PostMapping("tall") public String tallQuestion(@org.springframework.web.bind.annotation.RequestBody String question) throws IOException, UnirestException { Unirest.setTimeouts(0, 0); //DeeseekRequest: 自己的實(shí)體類名稱 List<DeeseekRequest.Message> messages = new ArrayList<>(); //給deepSeek一個(gè)角色 messages.add(DeeseekRequest.Message.builder().role("system").content("你是一個(gè)語言學(xué)家").build()); // question:說你自己想說的話 messages.add(DeeseekRequest.Message.builder().role("user").content(question).build()); DeeseekRequest requestBody = DeeseekRequest.builder() .model("deepseek-chat") .messages(messages) .build(); HttpResponse<String> response = Unirest.post("https://api.deepseek.com/chat/completions") .header("Content-Type", "application/json") .header("Accept", "application/json") .header("Authorization", "Bearer "+"自己的key") .body(gson.toJson(requestBody)) .asString(); return response.getBody(); } }
5、啟動(dòng)項(xiàng)目、調(diào)用自己的接口
你就會(huì)發(fā)現(xiàn),你所需要的答案就會(huì)被AI回答出來,快去試試吧,像你的目標(biāo)前進(jìn)!
到此這篇關(guān)于Java接入DeepSeek的保姆級(jí)教程(適合新手)的文章就介紹到這了,更多相關(guān)Java接入DeepSeek內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot整合vue項(xiàng)目(小試牛刀)
這篇文章主要介紹了springboot整合vue項(xiàng)目(小試牛刀),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09maven?springboot如何將jar包打包到指定目錄
這篇文章主要介紹了maven?springboot如何將jar包打包到指定目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12詳解MyBatis?ResultSetHandler?結(jié)果集的解析過程
這篇文章主要為大家介紹了MyBatis?ResultSetHandler?結(jié)果集的解析過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)-連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫
這篇文章給大家分享了Java實(shí)現(xiàn)飛機(jī)大戰(zhàn)中連接數(shù)據(jù)庫并把得分寫入數(shù)據(jù)庫的相關(guān)知識(shí)點(diǎn)和代碼,有興趣的可以學(xué)習(xí)參考下。2018-07-07Spark Streaming算子開發(fā)實(shí)例
這篇文章主要介紹了Spark Streaming算子開發(fā)實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06關(guān)于Mybatis中foreach遍歷Map的實(shí)現(xiàn)示例
這篇文章主要介紹了關(guān)于Mybatis中foreach遍歷Map的實(shí)現(xiàn)示例,MyBatis?是一款優(yōu)秀的半自動(dòng)的ORM持久層框架,它支持自定義?SQL、存儲(chǔ)過程以及高級(jí)映射,需要的朋友可以參考下2023-05-05