崩溃了几天的deepseek 接口官网今天终于好了:https://api-docs.deepseek.com/zh-cn/,打开一看貌似没有集成JAVA开发如下图,只有curl.python,nodejs三种形式
既然可以curl形式调用接口理论上任何编程语言都可以调用。
第一步:引入pom依赖jar,习惯了使用okhttp
<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>logging-interceptor</artifactId><version>5.0.0-alpha.6</version> </dependency>
第二步调用接口:
package com.jachs.deepseek;import org.junit.jupiter.api.Test;import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response;/**** @author 79951*/ public class Demo {//deepseek接口地址public static String URL="https://api.deepseek.com/chat/completions";OkHttpClient client = new OkHttpClient();public static final MediaType JSON= MediaType.get("application/json; charset=utf-8");@Testpublic void t1() throws Exception {String json=""" {"model": "deepseek-chat","messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Hello!"}],"stream": false}"""; RequestBody body = RequestBody.create(json, JSON);Request request = new Request.Builder().addHeader ( "Authorization", "Bearer " )//Bearer 必须带后面是自己的key .url(URL).post(body).build();Response response = client.newCall(request).execute();System.out.println ( response.body().string());}}
第三步看控制台输出如下:
{"id":"082903e8-32ad-48bb-a534-fda715e08645","object":"chat.completion","created":1738811085,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"你好用英文表达是 \"Hello\" 或 \"Hi\"。这两个词都是常见的问候语,用于日常交流中。"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":18,"completion_tokens":25,"total_tokens":43,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":18},"system_fingerprint":"fp_3a5770e1b4"}