当前位置: 首页 > news >正文

【使用JAVA调用deepseek】实现自能回复

在Spring Boot系统中接入DeepSeek服务,并将其提供给用户使用,通常需要以下步骤:

一、准备工作
(1)注册DeepSeek开发者账号

访问DeepSeek官网,注册并创建应用,获取API Key。

API文档:DeepSeek文档

在这里插入图片描述
(2)查阅DeepSeek的API文档,了解接口地址、请求参数和返回格式。

(3)确保已有一个Spring Boot项目,或者创建一个新的Spring Boot项目。

(4)确保已有一个Spring Boot项目,或者创建一个新的Spring Boot项目。

(5)JDK版本必须17版本以上。

二、集成步骤

引入POM包

  <!-- deepseek 开始 --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.12.0</version> <!-- 用最新稳定版 --></dependency><!-- JSON处理 --><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10.1</version> <!-- 使用最新稳定版本 --></dependency><!-- deepseek 结束 -->

三、创建请求类

import lombok.Data;
import java.util.List;@Data
public class ChatRequest {private String model;private List<Message> messages;@Datapublic static class Message {private String role;private String content;// 添加构造函数public Message(String role, String content) {this.role = role;this.content = content;}}
}
import lombok.Data;import java.util.List;@Data
public class DeepSeekResponse {private List<Choice> choices;@Datapublic static class Choice {private Message message;@Datapublic static class Message {private String role;private String content;}}
}

四、请求参数类

import lombok.Data;/*** 返回结果*/
@Data
public class ResultMess
{//输入咨询内容private String message;}

五、实现调用

1.需要注意传入的api 需要换成自己再deepseek平台申请的api。
2.发送请求调用的地址是: https://api.deepseek.com/v1/chat/completions

import com.aigc.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import java.util.List;/*** deepseek 控制器*/
@RestController
@RequestMapping("/system/deepseek")
public class DeepseekController
{private final RestTemplate restTemplate = new RestTemplate();public String callDeepSeek(String userMessage) {// 记录请求开始时间long startTime = System.currentTimeMillis();// 设置请求头HttpHeaders headers = new HttpHeaders();headers.set("Content-Type", "application/json");//todo 传入对应的api keyheaders.set("Authorization", "Bearer " + "sk-074d2b621ac6ef292");// 构建请求体ChatRequest request = new ChatRequest();request.setModel("deepseek-chat");request.setMessages(List.of(new ChatRequest.Message("user", userMessage)));// 发送请求//todo https://api.deepseek.com/v1/chat/completions  调用地址HttpEntity<ChatRequest> entity = new HttpEntity<>(request, headers);ResponseEntity<DeepSeekResponse> response = restTemplate.exchange("https://api.deepseek.com/v1/chat/completions", HttpMethod.POST, entity, DeepSeekResponse.class);// 解析响应String result;if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) {result =  response.getBody().getChoices().get(0).getMessage().getContent();} else {throw new RuntimeException("Failed to call DeepSeek API: " + response.getStatusCode());}// 记录请求结束时间long endTime = System.currentTimeMillis();// 计算接口执行耗时long duration = endTime - startTime;System.out.println("调用 DeepSeek API 耗时: " + duration + " 毫秒");return result;}/*** 调用 deepseek 接口* @param resultMess*/@PostMapping("/chat")public AjaxResult chat(@RequestBody ResultMess resultMess) {AjaxResult ajaxResult = new AjaxResult();//返回的结果ajaxResult.put("message", this.callDeepSeek(resultMess.getMessage()));ajaxResult.put("success", 200);return ajaxResult;}}

六、通过调用接口返回内容
在这里插入图片描述

七、调用之后想法

1.总体来说还可以把。
2.就是返回内容特别慢:调用 DeepSeek API 耗时: 38154 毫秒。
3.每次调用是0.01元

http://www.lqws.cn/news/117505.html

相关文章:

  • 嵌入式链表操作原理详解
  • 使用 systemctl 实现程序自启动与自动重启
  • Attention Is All You Need:抛弃循环神经网络的时代来了!
  • [AI Claude] 软件测试1
  • 【深度学习优化算法】02:凸性
  • 使用 SseEmitter 实现 Spring Boot 后端的流式传输和前端的数据接收
  • 《最近公共祖先》题集
  • DeepSeek本地部署及WebUI可视化教程
  • AI智能体,为美业后端供应链注入“智慧因子”(4/6)
  • 华为云Flexus+DeepSeek征文|Flexus云服务器单机部署+CCE容器高可用部署快速搭建生产级的生成式AI应用
  • vue项目中beforeDestroy或destroyed使用this.$notify.closeAll()失效
  • 华为云Flexus+DeepSeek征文|华为云Flexus服务器dify平台通过自然语言转sql并执行实现电商数据分析
  • 洛谷 单源最短路径 Dijkstra算法+优先队列
  • Flask框架详解:轻量高效的Python Web开发利器
  • 固定ip和非固定ip的区别是什么?如何固定ip地址
  • 搭建强化推荐的决策服务架构
  • OSPF域间路由
  • 企业的业务活动和管理活动是什么?-中小企实战运营和营销工作室博客
  • react+taro 开发第五个小程序,解决拼音的学习
  • 链路状态路由协议-OSPF
  • SpringAI集成DeepSeek实战
  • 近几年字节飞书测开部分面试题整理
  • 二极管MOS管选型
  • 【AI学习笔记】Coze工作流写入飞书多维表格(即:多维表格飞书官方插件使用教程)
  • Spring AI Tool Calling
  • Spring 中注入 Bean 有几种方式?
  • 通用寄存器的 “不通用“ 陷阱:AX/CX/DX 的寻址禁区与突围之道
  • 质检 LIMS 系统数据防护指南 三级等保认证与金融级加密方案设计
  • 设计模式-迪米特法则
  • 【Linux】自动化构建-Make/Makefile