java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java调用DeepSeek api

Java调用DeepSeek api实现方法记录

作者:baicai_li

这篇文章主要介绍了如何在Java中调用DeepSeek API,包括在官网获取APIKeys、创建API请求工具类以及处理返回结果的步骤,文中通过代码介绍的非常详细,需要的朋友可以参考下

官网上都没有实例自己测试一下

一、在官网创建DeepSeek的API Keys,只显示一次及时获取保存,不然得重新创建

二、创建api请求工具类

注意的是request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));

如果这句不加ContentType.APPLICATION_JSON 返回的都是英文

/**
 * @Author : L1
 * @Date: 2025/2/6 10:15
 * @Description:
 */
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
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;

import java.io.IOException;

public class DeepSeekClient {
    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "your API Keys"; //这里改成你自己的qpi keys
	// 传入提问
    public static String getDeepSeek(String problem) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost request = new HttpPost(API_URL);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Authorization", "Bearer " + API_KEY);
        // 请求体
        String requestBody = String.format(
                "{\"model\": \"deepseek-chat\", \"messages\": [{\"role\": \"user\", \"content\": \"%s\"}], \"stream\": false}",problem
        );
        request.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));
        // 发送请求并获取响应
        try (CloseableHttpResponse response = client.execute(request)) {
            // 返回响应内容
            return EntityUtils.toString(response.getEntity());
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println(getDeepSeek("红烧肉教程"));
    }
}

返回结果

{"id":"d5f40177-cdbe-4be9-bd0f-93dd96b0bab3","object":"chat.completion","created":1738812174,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"红烧肉是一道经典的中式家常菜,以其肥而不腻、入口即化的口感深受喜爱。以下是详细的红烧肉做法:\n\n### **材料:**\n- 五花肉:500克\n- 生姜:3-4片\n- 大葱:1根\n- 八角:2颗\n- 桂皮:1小块\n- 香叶:2片\n- 冰糖:30克(或白糖)\n- 生抽:2汤匙\n- 老抽:1汤匙\n- 料酒:2汤匙\n- 清水:适量\n- 盐:适量\n\n---\n\n### **步骤:**\n\n#### 1. **处理五花肉**\n   - 五花肉切成3-4厘米见方的块。\n   - 将五花肉放入冷水锅中,加入几片姜和1汤匙料酒,大火煮开后撇去浮沫,焯水2-3分钟,捞出沥干备用。\n\n#### 2. **炒糖色**\n   - 锅中放入少量油,加入冰糖(或白糖),用小火慢慢炒至冰糖融化,颜色变为琥珀色(注意不要炒糊)。\n   - 将焯好的五花肉倒入锅中,快速翻炒,使肉块均匀裹上糖色。\n\n#### 3. **调味炖煮**\n   - 加入姜片、葱段、八角、桂皮、香叶,翻炒出香味。\n   - 倒入2汤匙生抽、1汤匙老抽、1汤匙料酒,翻炒均匀。\n   - 加入适量清水,水量以刚好没过肉块为宜。\n   - 大火煮开后转小火,盖上锅盖炖煮40-50分钟。\n\n#### 4. **收汁**\n   - 炖至肉块软烂后,开大火收汁,汤汁变浓稠时加入少许盐调味。\n   - 收汁至汤汁裹在肉块上即可关火。\n\n#### 5. **装盘**\n   - 将红烧肉盛出,撒上少许葱花或香菜点缀即可。\n\n---\n\n### **小贴士:**\n1. **选肉**:五花肉最好选择肥瘦相间的,这样炖出来的肉口感更好。\n2. **炒糖色**:炒糖色时火候要小,避免糖色炒糊发苦。\n3. **炖煮时间**:根据肉块大小调整炖煮时间,确保肉块软烂入味。\n4. **收汁**:收汁时注意火候,避免汤汁收得太干。\n\n这样一道色香味俱全的红烧肉就完成了,配上一碗米饭,简直是人间美味!"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":6,"completion_tokens":546,"total_tokens":552,"prompt_tokens_details":{"cached_tokens":0},"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":6},"system_fingerprint":"fp_3a5770e1b4"}

总结

以上就是Java调用DeepSeep的方法简单过程

到此这篇关于Java调用DeepSeek api的文章就介绍到这了,更多相关Java调用DeepSeek api内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文