springboot接入deepseek深度求索 java

以下是在springboot中接入aideepseek的过程。官网并没有java的示例。

1. 创建 API keydeepseek API keys

点击创建API key,把创建的key值复制下来,以后就不能再查看了,只能重新创建。

2. 封装询问deepseek的工具方法

添加key值和询问路径。API_KEY为你创建的key值。

复制代码
    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "11111111"; // 替换为你的 API Key

传入的question就是要询问的问题

复制代码
public String askDeepSeek(String question) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();

        // 创建 HTTP POST 请求
        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}",
                question
        );
        request.setEntity(new StringEntity(requestBody));

        // 发送请求并获取响应
        try (CloseableHttpResponse response = client.execute(request)) {
            // 返回响应内容
            return EntityUtils.toString(response.getEntity());
        }
    }
3. 调用该询问ai的方法
复制代码
String  question1= "今天是星期几。 " ;
String answer = askDeepSeek(question);
System.out.println("answer = " + answer);
4. 成功返回示例
复制代码
answer = {"id":"88dbce49-2841-448d-a74f-a2d3180c5672","object":"chat.completion","created":1734525002,"model":"deepseek-chat","choices":[{"index":0,"message":{"role":"assistant","content":"当然,我很高兴!谢谢你的关心。??"},"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":12,"completion_tokens":11,"total_tokens":23,"prompt_cache_hit_tokens":0,"prompt_cache_miss_tokens":12},"system_fingerprint":"fp_1bcb2de9ac"}

不过我接入之后,他只能回答一些很简单的问题,有没有大佬会用啊~

相关推荐
java资料站5 分钟前
二、Spring AI Alibaba · ChatModel
java·windows·spring
用户69371750013847 分钟前
2026,程序员的时代拐点到了
android·前端·后端
惜鸟21 分钟前
从源码看 pi 的上下文管理:原始数据永久保留,模型视角按需裁剪
后端
量化分析码农28 分钟前
【Python量化策略评估体系 #05】极端行情扛得住吗?2008/2015/2020 三场景压力测试
后端
十年Java程序媛29 分钟前
Java 接口和抽象类对比|Java8 新特性,抛弃老旧八股,正确选型
java·spring boot·后端
caoerzhong31 分钟前
JeeWMS 开源 WMS 部署避坑指南:Java 仓库管理系统的环境基线、四类根因与可复现交付
java·开发语言·开源
用户37215742613531 分钟前
Java 合并 PDF 文件:完整合并、指定页面合并与流合并
java
IT_陈寒31 分钟前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端
用户83562907805144 分钟前
使用 Python 对 Excel 工作表进行排序
后端·python
程序员cxuan1 小时前
腾讯又来一王炸,开源版 WorkBuddy 太夯了!
人工智能·后端·程序员