springboot接入deepseek深度求索代码示例(java版)
以下是在springboot中接入ai deepseek的过程。官网并没有java的示例。
1. 创建 API key
点击创建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"}
不过我接入之后,他只能回答一些很简单的问题,有没有大佬会用啊~
总结
到此这篇关于springboot接入deepseek深度求索的文章就介绍到这了,更多相关springboot接入deepseek深度求索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
解决创建springboot后启动报错:Failed to bind properties under‘spri
在Spring Boot项目中,application.properties和application.yml是用于配置参数的两种文件格式,properties格式简洁但不支持层次结构,而yml格式支持层次性,可读性更好,在yml文件中,要注意细节,比如冒号后面需要空格2024-10-10
解析使用jdbc,hibernate处理clob/blob字段的详解
本篇是对使用jdbc,hibernate处理clob/blob字段进行了详细的分析介绍,需要的朋友参考下2013-05-05
Java AQS中ReentrantReadWriteLock读写锁的使用
ReentrantReadWriteLock称为读写锁,它提供一个读锁,支持多个线程共享同一把锁。这篇文章主要讲解一下ReentrantReadWriteLock的使用和应用场景,感兴趣的可以了解一下2023-02-02
java源码解析之String类的compareTo(String otherString)方法
这篇文章主要给大家介绍了关于java源码解析之String类的compareTo(String otherString)方法的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧2018-09-09


最新评论