一、准备工作
注册账号
- 访问 DeepSeek 官网(如 https://www.deepseek.com/)或对应平台。
- 完成注册并登录,部分服务可能需要企业认证或申请权限。
获取 API 密钥(如使用 API)
- 进入控制台或开发者页面,创建 API Key。
- 保存密钥(如 sk-xxxxxxxxx),勿泄露给他人。
安装必要工具
- 如使用编程调用,需安装 Python 环境及依赖库:
pip install requests openai # 部分服务可能需要特定 SDK
二、基础使用(以 API 为例)
示例 1:通过 HTTP 请求调用
import requestsurl = "https://api.deepseek.com/v1/chat/completions" # 替换为实际 API 地址
headers = {"Authorization": "Bearer YOUR_API_KEY","Content-Type": "application/json"
}
data = {"model": "deepseek-chat", # 指定模型名称"messages": [{"role": "user", "content": "你好,请介绍 DeepSeek 的功能。"}]
}response = requests.post(url, json=data, headers=headers)
print(response.json())
示例 2:使用官方 SDK(如有)
若提供 SDK,安装后调用更便捷:
from deepseek import DeepSeekClientclient = DeepSeekClient(api_key="YOUR_API_KEY")
response = client.chat_completions.create(model="deepseek-chat",messages=[{"role": "user", "content": "写一首关于春天的诗"}]
)
print(response.choices[0].message.content)
三、高级功能
1. 调整模型参数
- 控制生成结果的参数(如随机性、长度):
{"temperature": 0.7, # 值越高越随机(0-2)"max_tokens": 500, # 生成最大长度"top_p": 0.9 # 多样性控制
}
2. 多轮对话
- 维护 messages 历史记录实现连续对话:
messages = [{"role": "user", "content": "如何学习机器学习?"},{"role": "assistant", "content": "建议从基础数学和 Python 开始..."},{"role": "user", "content": "推荐一些实践项目。"}
]
————————————————
四. 添加HTTP客户端依赖使用Java的HTTP客户端库(如Apache HttpClient或OkHttp)来发送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>
- 创建HTTP请求使用HTTP客户端库创建请求,设置请求头、URL和请求体
示例1. 使用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(); } }}
示例2. 使用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发送向deepseek发送请求。
————————————————