DeepSeek简介
DeepSeek(深度求索)是由中国人工智能公司深度求索(DeepSeek Inc.)研发的大规模语言模型(LLM),专注于提供高效、智能的自然语言处理能力,支持多种场景下的文本生成、对话交互和多模态任务。
网页体验链接
DeepSeek开放平台
简单实现DeepSeek API的调用
1、API key申请
- 登陆注册,实名认证之后,在deepseek开放平台上申请APIkey:
- 点击创建API key
- 输入名称即可完成创建,创建后会获得一个key
注:注意保存好这个key,点击关闭后无法再重新点开查看,如果丢失则需要重新进行创建
2、查看接口文档
DeepSeek接口文档
参数
- base_url: https://api.deepseek.com
- api_key: apply for an key
3、简单的一个调用案例
- 在pom.xml文件中导入依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version> <!-- 请检查最新版本 -->
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version> <!-- 请检查最新版本 -->
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
- 创建一个简单的案例
import okhttp3.*;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.util.Scanner;
public class DeepSeekClient {
private static final String BASE_URL = "https://api.deepseek.com/v1/chat/completions"; // API 端点
private static final String API_KEY = "***"; // 替换为自己的 API Key
private final OkHttpClient client;
public DeepSeekClient() {
this.client = new OkHttpClient();
}
public String sendChatCompletionRequest(JSONArray messages) throws IOException {
// 构建请求体
JSONObject requestBody = new JSONObject();
requestBody.put("model", "deepseek-chat"); // 模型名称
requestBody.put("messages", messages); // 包含对话历史的 messages
// 创建请求体
RequestBody body = RequestBody.create(
requestBody.toString(),
MediaType.parse("application/json")
);
// 创建请求
Request request = new Request.Builder()
.url(BASE_URL)
.post(body)
.addHeader("Authorization", "Bearer " + API_KEY)
.addHeader("Content-Type", "application/json")
.build();
// 发送请求并获取响应
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code: " + response.code());
}
return response.body().string();
}
}
public static void main(String[] args) {
DeepSeekClient client = new DeepSeekClient();
Scanner scanner = new Scanner(System.in);
JSONArray messages = new JSONArray(); // 用于保存对话历史
// 设置高数老师的角色
JSONObject systemMessage = new JSONObject();
systemMessage.put("role", "system");
systemMessage.put("content", "你是一位高数老师。请用简洁明了的语言回答我的问题。");
messages.put(systemMessage);
System.out.println("高数老师: 你好!你可以问我任何问题,输入 '退出' 来结束对话。");
// 开始对话循环
while (true) {
System.out.print("你: ");
String userInput = scanner.nextLine();
// 如果用户输入 "退出",结束对话
if ("退出".equalsIgnoreCase(userInput)) {
System.out.println("高数老师: 再见!如果有其他问题,随时来找我。");
break;
}
// 将用户输入添加到 messages
JSONObject userMessage = new JSONObject();
userMessage.put("role", "user");
userMessage.put("content", userInput);
messages.put(userMessage);
try {
// 发送请求并获取 AI 的回复
String response = client.sendChatCompletionRequest(messages);
JSONObject jsonResponse = new JSONObject(response);
JSONArray choices = jsonResponse.getJSONArray("choices");
if (choices.length() > 0) {
JSONObject firstChoice = choices.getJSONObject(0);
JSONObject message = firstChoice.getJSONObject("message");
String content = message.getString("content");
// 打印 AI 的回复
System.out.println("高数老师: " + content);
// 将 AI 的回复添加到 messages
JSONObject assistantMessage = new JSONObject();
assistantMessage.put("role", "assistant");
assistantMessage.put("content", content);
messages.put(assistantMessage);
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("高数老师: 抱歉,我暂时无法回答你的问题。请稍后再试。");
}
}
scanner.close();
}
}
4、运行程序
可以根据自己对AI预先设置的角色进行相关问答测试,以上为该案例的运行结果