以下是使用 Java 调用 DeepSeek API 的示例。我们将使用 Java 的 HttpURLConnection
或第三方库(如 OkHttp
)来发送 HTTP 请求,并处理 API 的响应。
HttpURLConnection
调用 DeepSeek API示例代码
java
复制
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class DeepSeekApiExample { public static void main(String[] args) { try { // DeepSeek API 的 URL String apiUrl = "https://api.deepseek.com/v1/your-endpoint"; // 替换为实际的 API 端点 // 你的 API 密钥 String apiKey = "your_api_key_here"; // 请求参数 String jsonInputString = "{"text": "这是一个测试文本", "language": "zh", "max_tokens": 50}"; // 创建 URL 对象 URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为 POST connection.setRequestMethod("POST"); // 设置请求头 connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Authorization", "Bearer " + apiKey); // 启用输出流 connection.setDoOutput(true); // 发送请求 try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } // 获取响应代码 int responseCode = connection.getResponseCode(); System.out.println("响应代码: " + responseCode); // 读取响应 if (responseCode == HttpURLConnection.HTTP_OK) { try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println("响应内容: " + response.toString()); } } else { System.out.println("请求失败,响应代码: " + responseCode); } // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
OkHttp
调用 DeepSeek APIOkHttp
是一个流行的 HTTP 客户端库,简化了 HTTP 请求的发送和响应的处理。
添加依赖
在 pom.xml
中添加 OkHttp
依赖:
xml
复制
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> <version>4.9.3</version> </dependency>
运行 HTML
示例代码
java
复制
import okhttp3.*; import java.io.IOException; public class DeepSeekApiExample { public static void main(String[] args) { // DeepSeek API 的 URL String apiUrl = "https://api.deepseek.com/v1/your-endpoint"; // 替换为实际的 API 端点 // 你的 API 密钥 String apiKey = "your_api_key_here"; // 请求参数 String jsonInputString = "{"text": "这是一个测试文本", "language": "zh", "max_tokens": 50}"; // 创建 OkHttp 客户端 OkHttpClient client = new OkHttpClient(); // 创建请求体 RequestBody body = RequestBody.create(jsonInputString, MediaType.parse("application/json")); // 创建请求 Request request = new Request.Builder() .url(apiUrl) .post(body) .addHeader("Content-Type", "application/json") .addHeader("Authorization", "Bearer " + apiKey) .build(); // 发送请求并处理响应 try (Response response = client.newCall(request).execute()) { if (response.isSuccessful() && response.body() != null) { String responseBody = response.body().string(); System.out.println("响应内容: " + responseBody); } else { System.out.println("请求失败,响应代码: " + response.code()); } } catch (IOException e) { e.printStackTrace(); } } }
假设 DeepSeek 提供了一个分词 API,以下是如何调用它的示例:
使用 HttpURLConnection
java
复制
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class DeepSeekTokenizeExample { public static void main(String[] args) { try { String apiUrl = "https://api.deepseek.com/v1/tokenize"; // 假设的分词 API 端点 String apiKey = "your_api_key_here"; String jsonInputString = "{"text": "这是一个中文分词测试", "language": "zh"}"; URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Authorization", "Bearer " + apiKey); connection.setDoOutput(true); try (OutputStream os = connection.getOutputStream()) { byte[] input = jsonInputString.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = connection.getResponseCode(); System.out.println("响应代码: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println("分词结果: " + response.toString()); } } else { System.out.println("请求失败,响应代码: " + responseCode); } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
使用 OkHttp
java
复制
import okhttp3.*; import java.io.IOException; public class DeepSeekTokenizeExample { public static void main(String[] args) { String apiUrl = "https://api.deepseek.com/v1/tokenize"; // 假设的分词 API 端点 String apiKey = "your_api_key_here"; String jsonInputString = "{"text": "这是一个中文分词测试", "language": "zh"}"; OkHttpClient client = new OkHttpClient(); RequestBody body = RequestBody.create(jsonInputString, MediaType.parse("application/json")); Request request = new Request.Builder() .url(apiUrl) .post(body) .addHeader("Content-Type", "application/json") .addHeader("Authorization", "Bearer " + apiKey) .build(); try (Response response = client.newCall(request).execute()) { if (response.isSuccessful() && response.body() != null) { String responseBody = response.body().string(); System.out.println("分词结果: " + responseBody); } else { System.out.println("请求失败,响应代码: " + response.code()); } } catch (IOException e) { e.printStackTrace(); } } }
使用 HttpURLConnection
是 Java 标准库的方式,无需额外依赖。
使用 OkHttp
更加简洁和高效,适合复杂的 HTTP 请求场景。
根据 DeepSeek 的 API 文档,调整请求参数和处理响应数据。
如果有更多具体需求,可以参考 DeepSeek 的官方文档或联系技术支持。