JAVA調(diào)用Deepseek的api完成基本對話簡單代碼示例
獲取API密鑰首先,從DeepSeek平臺獲取API密鑰,用于身份驗(yàn)證。
添加HTTP客戶端依賴使用Java的HTTP客戶端庫(如Apache HttpClient或OkHttp)來發(fā)送HTTP請求。如果使用Maven,可以在pom.xml中添加依賴:、
<!-- Apache HttpClient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <!-- OkHttp --> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.3</version> </dependency>
創(chuàng)建HTTP請求使用HTTP客戶端庫創(chuàng)建請求,設(shè)置請求頭、URL和請求體
使用Apache HttpClient示例:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class DeepSeekClient {
private static final String API_URL = "https://api.deepseek.com/v1/your-endpoint";
private static final String API_KEY = "your-api-key";
public static void main(String[] args) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(API_URL);
httpPost.setHeader("Authorization", "Bearer " + API_KEY);
httpPost.setHeader("Content-Type", "application/json");
String json = "{\"name\":\"tom\"}"; // 替換為實(shí)際請求體
httpPost.setEntity(new StringEntity(json));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity);
System.out.println(result);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用OkHttp示例
import okhttp3.*;
import java.io.IOException;
public class DeepSeekClient {
private static final String API_URL = "https://api.deepseek.com/v1/your-endpoint";
private static final String API_KEY = "your-api-key";
public static void main(String[] args) {
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
String json = "{\"name\":\"tom\"}"; // 替換為實(shí)際請求體
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url(API_URL)
.post(body)
.addHeader("Authorization", "Bearer " + API_KEY)
.addHeader("Content-Type", "application/json")
.build();
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
System.out.println(response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
通過以上步驟,你可以在Java中成功對接DeepSeek API,也可整合springboot,通過springboot發(fā)送向deepseek發(fā)送請求。
總結(jié)
到此這篇關(guān)于JAVA調(diào)用Deepseek的api完成基本對話的文章就介紹到這了,更多相關(guān)JAVA調(diào)用Deepseek的api內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)人臉識別等多種登錄方式
本文主要介紹了SpringBoot實(shí)現(xiàn)人臉識別等多種登錄方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
springboot整合security和vue的實(shí)踐
本文主要介紹了springboot整合security和vue的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Spring?Boot?集成?Quartz并使用Cron?表達(dá)式實(shí)現(xiàn)定時任務(wù)
本篇文章介紹了如何在?Spring?Boot?中集成?Quartz?進(jìn)行定時任務(wù)調(diào)度,并通過?Cron?表達(dá)式?控制任務(wù)執(zhí)行時間,Quartz?提供了更強(qiáng)大的任務(wù)調(diào)度能力,比?@Scheduled?注解更靈活,適用于復(fù)雜的定時任務(wù)需求2025-04-04
解決@Test注解在Maven工程的Test.class類中無法使用的問題
這篇文章主要介紹了解決@Test注解在Maven工程的Test.class類中無法使用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
解決@Transactional遇上@synchronized的生產(chǎn)問題
這篇文章主要介紹了解決@Transactional遇上@synchronized的生產(chǎn)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06
IntelliJ IDEA2021.1 配置大全(超詳細(xì)教程)
這篇文章主要介紹了IntelliJ IDEA2021.1 配置大全(超詳細(xì)教程),需要的朋友可以參考下2021-04-04

