Java接入deepseek,我们首先看看接入接口
调用对话 API
在创建 API key 之后,你可以使用以下样例脚本的来访问 DeepSeek API。样例为非流式输出,您可以将 stream 设置为 true 来使用流式输出。
接口文档如下:
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
curl https://api.deepseek.com/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <DeepSeek API Key>" \
-d '{
"model": "deepseek-chat",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
"stream": false
}'
当然首先生产key,接下来就是直接编写Java接口了。我这里使用的是spring fegin的方式
1、接口类:DeepSeekClient
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import cn.hutool.json.JSONObject;
/**
* @date:2025年2月10日
*/
@FeignClient(name = "deepseek", url = "https://api.deepseek.com/")
public interface DeepSeekClient {
/**
* deepseek对话
* @param params
* @return
*/
@PostMapping("chat/completions")
JSONObject deepseekChat(@RequestHeader("Authorization") String authorization,