设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间11 现在时间11 缓存数据 我本可以容忍黑暗,如果我不曾见过太阳.然而阳光已使我荒凉,成为更新的荒凉……我啜饮过生活的芳醇,付出了什么,告诉你吧,不多不少,整整一生.

我本可以容忍黑暗,如果我不曾见过太阳.然而阳光已使我荒凉,成为更新的荒凉……我啜饮过生活的芳醇,付出了什么,告诉你吧,不多不少,整整一生. -- 现世道

查看: 873|回复: 1

SpringBoot接入deepseek深度求索示例代码(jdk1.8)

[复制链接]

  离线 

TA的专栏

  • 打卡等级:热心大叔
  • 打卡总天数:228
  • 打卡月天数:0
  • 打卡总奖励:3489
  • 最近打卡:2025-04-12 13:54:36
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
388
主题
357
精华
0
金钱
4671
积分
805
注册时间
2023-1-5
最后登录
2025-4-12

发表于 2025-2-17 18:22:13 来自手机 | 显示全部楼层 |阅读模式
目录
  • 1. 创建 API key 
  • 2. 封装询问deepseek的工具方法
  • 3.调用测试
  • 4.运行结果
  • 总结 

以下是在SpringBoot中接入ai deepseek的过程。我这里的环境是jdk1.8,官网暂时没有java的示例。

1. 创建 API key 

deepseek API keys 

新用户会有500万的免费token额度

1.png

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

添加key值和询问路径。API_KEY为你创建的key值。这个在配置文件或者是写在常量文件都可以,我这是写在配置文件里了

2.png

接下来就是代码实例了

  1. import com.fasterxml.jackson.databind.JsonNode;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.zhiwonders.paperserver.service.IAuthService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.CloseableHttpClient;
  9. import org.apache.http.impl.client.HttpClients;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.http.MediaType;
  13. import org.springframework.web.bind.annotation.*;
  14. import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
  15. import java.io.BufferedReader;
  16. import java.io.InputStreamReader;
  17. import java.nio.charset.StandardCharsets;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.concurrent.ExecutorService;
  22. import java.util.concurrent.Executors;
  23. @RestController
  24. @RequestMapping("/api/v1")
  25. @Slf4j
  26. public class OpenAIController {
  27. @Value("${ai.config.deepseek.apiKey}")
  28. private String API_KEY;
  29. @Value("${ai.config.deepseek.baseUrl}")
  30. private String API_URL;
  31. @Autowired
  32. private IAuthService authService;
  33. private final ExecutorService executorService = Executors.newCachedThreadPool();
  34. private final ObjectMapper objectMapper = new ObjectMapper();
  35. @PostMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  36. public SseEmitter chat(
  37. // @RequestHeader("Authorization")String token,
  38. @RequestBody String question) {
  39. // String openid = authService.openid(token);
  40. // if (openid == null) {
  41. // throw new RuntimeException("用户未登录");
  42. // }
  43. SseEmitter emitter = new SseEmitter(-1L);
  44. executorService.execute(() -> {
  45. try {
  46. log.info("流式回答开始,问题:{}", question);
  47. try (CloseableHttpClient client = HttpClients.createDefault()) {
  48. HttpPost request = new HttpPost(API_URL);
  49. request.setHeader("Content-Type", "application/json");
  50. request.setHeader("Authorization", "Bearer " + API_KEY);
  51. Map<String, Object> message = new HashMap<>();
  52. message.put("role", "user");
  53. message.put("content", question);
  54. Map<String, Object> requestMap = new HashMap<>();
  55. requestMap.put("model", "deepseek-chat");
  56. requestMap.put("messages", Collections.singletonList(message));
  57. requestMap.put("stream", true);
  58. String requestBody = objectMapper.writeValueAsString(requestMap);
  59. request.setEntity(new StringEntity(requestBody, StandardCharsets.UTF_8));
  60. try (CloseableHttpResponse response = client.execute(request);
  61. BufferedReader reader = new BufferedReader(
  62. new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8))) {
  63. String line;
  64. while ((line = reader.readLine()) != null) {
  65. if (line.startsWith("data: ")) {
  66. String jsonData = line.substring(6);
  67. if ("[DONE]".equals(jsonData)) {
  68. break;
  69. }
  70. JsonNode node = objectMapper.readTree(jsonData);
  71. String content = node.path("choices")
  72. .path(0)
  73. .path("delta")
  74. .path("content")
  75. .asText("");
  76. if (!content.isEmpty()) {
  77. emitter.send(content);
  78. }
  79. }
  80. }
  81. log.info("流式回答结束,{}",question);
  82. emitter.complete();
  83. }
  84. } catch (Exception e) {
  85. log.error("处理 Deepseek 请求时发生错误", e);
  86. emitter.completeWithError(e);
  87. }
  88. } catch (Exception e) {
  89. log.error("处理 Deepseek 请求时发生错误", e);
  90. emitter.completeWithError(e);
  91. }
  92. });
  93. return emitter;
  94. }
  95. }
复制代码

3.调用测试

  1. curl -v -X POST \
  2. http://localhost:8091/api/v1/chat \
  3. -H "Content-Type: application/json" \
  4. -d '"帮我写一篇2000字的 我的祖国的文字"' \
  5. --no-buffer
复制代码

打开终端输入上述命令回车即可 但是端口必须要和你后端的一致 我这里是8091

4.运行结果

3.png

总结 

到此这篇关于SpringBoot接入deepseek深度求索的文章就介绍到这了,更多相关SpringBoot接入deepseek深度求索内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
12
积分
4
注册时间
2023-8-14
最后登录
2023-8-14

发表于 4 天前 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表