当前位置: 首页 > news >正文

深度求索—DeepSeek API的简单调用(Java)

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预先设置的角色进行相关问答测试,以上为该案例的运行结果

相关文章:

  • 简单的异步图片上传
  • 游戏引擎学习第104天
  • ABB能源自动化选用宏集Cogent DataHub避免DCOM问题,实现高效、安全的数据传输
  • cuML机器学习GPU库
  • vue3的响应式的理解,与普通对象的区别
  • ROS基本功能
  • 【吾爱出品】视频素材资源搜索、解析、下载助手
  • 深入解析操作系统控制台:阿里云Alibaba Cloud Linux(Alinux)的运维利器
  • 自动化运维之ansible快速入门
  • Ubuntu24.04更新国内源
  • Facebook精准获客:外贸企业如何通过社群营销将产品推广到海外
  • 【2025最新版】软件测试面试题总结(150道题含答案解析)
  • C++ 后台开发简历优化指南和如何利用DeepSeek优化简历
  • 【MySQL数据库】Ubuntu下的mysql
  • 侯捷 C++ 课程学习笔记:C++ 面向对象开发的艺术
  • 协议-NVME
  • springboot整合mybatis-plus(保姆教学) 及搭建项目
  • 离线量化算法和工具 --学习记录1
  • mongodb主从
  • Linux软件编程——标准IO(2025.2.14)
  • 习近平会见斯洛伐克总理菲佐
  • 19个剧团15台演出,上海民营院团尝试文旅融合新探索
  • 讲座预告|全球贸易不确定情况下企业创新生态构建
  • 马上评丨行人转身相撞案:走路该保持“安全距离”吗
  • 司法部:加快研究制定行政执法监督条例,建立完善涉企行政执法监督长效机制
  • 上海如何为街镇营商环境赋能?送政策、配资源、解难题、强活力