效果演示

DeepSeek key获取

第一步进入官网 DeepSeek官网https://www.deepseek.com/

 第二步使用手机号注册或登录

 第三步创建API keys并复制保存

Java调用官网DeepSeek

添加配置(properties/yml)

##官方DeepSeek对话补齐AIP
deepSeek.chat.url=https://api.deepseek.com/chat/completions
##DeepSeek key
deepSeek.key=you deepseek key

关键调用DeepSeek代码

    @Value("${deepSeek.chat.url}")
    private String deepSeekChatUrl;

    @Value("${deepSeek.key}")
    private String deepSeekKey;


public String officialDeepSeekAip(String message, String systemMessage) throws IOException {
        /**
         * 创建DeepSeekRequest对象 属性注解均在实体类中
         * 官方案例文档地址:https://api-docs.deepseek.com/zh-cn/api/create-chat-completion
         */
        DeepSeekRequest requestObject = new DeepSeekRequest();
        List<DeepSeekRequest.Message> messages = new ArrayList<>();
        //用户消息
        messages.add(new DeepSeekRequest.Message(message, "user"));
        //上一条deepseek消息
        messages.add(new DeepSeekRequest.Message(systemMessage, "system"));
        requestObject.setMessages(messages);
        // 模型 根据自身情况调整
        requestObject.setModel("deepseek-chat");
        requestObject.setFrequency_penalty(0);
        requestObject.setMax_tokens(2048);
        requestObject.setPresence_penalty(0);
        requestObject.setResponse_format(new DeepSeekRequest.ResponseFormat("text"));
        requestObject.setStop(null);
        requestObject.setStream(false);
        requestObject.setStream_options(null);
        requestObject.setTemperature(1);
        requestObject.setTop_p(1);
        requestObject.setTools(null);
        requestObject.setTool_choice("none");
        requestObject.setLogprobs(false);
        requestObject.setTop_logprobs(null);

        // 使用Gson将请求对象转换为JSON字符串
        Gson gson = new Gson();
        String jsonBody = gson.toJson(requestObject);

        // 创建OkHttpClient实例,并设置超时时间
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(300, java.util.concurrent.TimeUnit.SECONDS)
                .readTimeout(300, java.util.concurrent.TimeUnit.SECONDS)
                .writeTimeout(300, java.util.concurrent.TimeUnit.SECONDS)
                .build();
        // 设置请求体的媒体类型为JSON
        MediaType mediaType = MediaType.parse("application/json");
        // 创建请求体,包含JSON字符串
        RequestBody body = RequestBody.create(mediaType, jsonBody);
        // 创建HTTP POST请求
        Request request = new Request.Builder()
                .url(deepSeekChatUrl)
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .addHeader("Authorization", "Bearer " + deepSeekKey)
                .build();

        // 发送请求并获取响应
        Response response = client.newCall(request).execute();
        try {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response + " with body " + response.body().string());
            }
            // 读取响应体内容
            String responseBodyString = response.body().string();
            // 将响应体内容转换为DeepSeekResponse对象
            DeepSeekChatResponse deepSeekChatResponse = gson.fromJson(responseBodyString, DeepSeekChatResponse.class);

            // 检查响应内容是否为空
            if (deepSeekChatResponse == null || deepSeekChatResponse.getChoices() == null || deepSeekChatResponse.getChoices().isEmpty()) {
                throw new IOException("Response is empty or does not contain expected data");
            }

            return deepSeekChatResponse.getChoices().get(0).getMessage().getContent();
        } finally {
            response.close();
        }
    }

 完整代码地址满血DeepSeek调用案例https://gitee.com/java-lm/DeepSeekWebsocket

Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐