JAVA調(diào)用Deepseek的api完成基本對話簡單代碼示例
獲取API密鑰首先,從DeepSeek平臺獲取API密鑰,用于身份驗證。
添加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)建請求,設置請求頭、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\"}"; // 替換為實際請求體 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\"}"; // 替換為實際請求體 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é)
到此這篇關于JAVA調(diào)用Deepseek的api完成基本對話的文章就介紹到這了,更多相關JAVA調(diào)用Deepseek的api內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot命令行啟動添加參數(shù)的三種方式
在命令行中,常見的參數(shù)可以分為三類:選項參數(shù)、非選項參數(shù)和系統(tǒng)參數(shù),本文就來介紹一下Spring Boot命令行三種參數(shù)形式,感興趣的可以了解一下2023-09-09Spring Boot Hello World的實現(xiàn)代碼
這篇文章主要介紹了Spring Boot Hello World的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06