[超详细]JAVA接入DeepSeek保姆级教学[小白]

devtools/2025/2/21 18:23:36/

目录

前言

1、获取自己在DeepSeek上的token

2、引入依赖

3、创建实体类 

4、创建Controller层

5、启动项目、调用自己的接口


前言

        对于目前的DeepSeek大家应该都不是很陌生,目前也是最流行的一款AI软件了,所以为了让我们开发更全面,能够在自己的项目中融入AI那就会很全面了,所以这次的文章,将模拟一个基础案例,可以在这个基础案例迭代实现出你自己的AI。

        话不多说,也不介绍我的网站了,直接开始进行一下流程。

使用的:JDK 17     

1、获取自己在DeepSeek上的token

网站: DeepSeek | 深度求索  ,点击API开放平台找到API keys  获取自己的key,注意你的key一定要保存好了

2、引入依赖

这个就不多说了,在你的pom文件中引入相对应的依赖即可。 

<dependencies><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>3.4.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>3.4.2</version></dependency><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version><scope>provided</scope></dependency><!-- HTTP客户端 --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.9.0</version></dependency><!-- JSON处理 --><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.10.1</version></dependency><dependency><groupId>com.mashape.unirest</groupId><artifactId>unirest-java</artifactId><version>1.4.9</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.6</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpasyncclient</artifactId><version>4.0.2</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.3.6</version></dependency><dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20140107</version></dependency></dependencies>

3、创建实体类 

java">@Data
@Builder
public class DeeseekRequest {private String model;private List<Message> messages;@Data@Builderpublic static class Message {private String role;private String content;}
}

4、创建Controller层

java">package com.wdc.dk;import com.google.gson.Gson;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;@RestController
public class AIController {private final Gson gson = new Gson();@PostMapping("tall")public String tallQuestion(@org.springframework.web.bind.annotation.RequestBody String question) throws IOException, UnirestException {Unirest.setTimeouts(0, 0);//DeeseekRequest: 自己的实体类名称List<DeeseekRequest.Message> messages = new ArrayList<>();
//给deepSeek一个角色messages.add(DeeseekRequest.Message.builder().role("system").content("你是一个语言学家").build());// question:说你自己想说的话messages.add(DeeseekRequest.Message.builder().role("user").content(question).build());DeeseekRequest requestBody = DeeseekRequest.builder().model("deepseek-chat").messages(messages).build();HttpResponse<String> response = Unirest.post("https://api.deepseek.com/chat/completions").header("Content-Type", "application/json").header("Accept", "application/json").header("Authorization", "Bearer "+"自己的key").body(gson.toJson(requestBody)).asString();return  response.getBody();}
}

5、启动项目、调用自己的接口

你就会发现,你所需要的答案就会被AI回答出来,快去试试吧,像你的目标前进! 


http://www.ppmy.cn/devtools/160747.html

相关文章

java面试场景问题

还在补充&#xff0c;这几天工作忙&#xff0c;闲了会把答案附上去&#xff0c;也欢迎各位大佬评论区讨论 1.不用分布式锁如何防重复提交 方法 1&#xff1a;基于唯一请求 ID&#xff08;幂等 Token&#xff09; 思路&#xff1a;前端生成 一个唯一的 requestId&#xff08;…

使用Nginx本地部署Axure生成的HTML文件,局域网内浏览器通过IP和地址访问

在原型设计阶段&#xff0c;Axure是一款非常流行的工具&#xff0c;它能够帮助设计师快速创建交互原型。然而&#xff0c;有时候我们需要将这些原型分享给团队内的其他成员进行评审和测试。这时&#xff0c;通过Nginx将Axure生成的HTML文件部署到本地服务器&#xff0c;并在局域…

深入解析 VIE(Variable Interest Entity,可变利益实体)架构:中国公司如何在海外上市?

深入解析 VIE 架构&#xff1a;中国公司如何在海外上市&#xff1f; 在全球资本市场上&#xff0c;许多中国企业&#xff0c;尤其是科技、互联网和教育类公司&#xff0c;采用了一种特殊的法律架构在美国上市&#xff0c;即 VIE&#xff08;Variable Interest Entity&#xff…

JAVA EE初阶 JVM

JVM Java是一个跨平台的语言&#xff0c;可以不加修改在任何操作系统中运行。 JVM运行流程 代码编译之后生成了什么文件&#xff1f; java文本文件 —> class字节码文件&#xff0c;最终会在JVM中执行。 class文件怎么被JVM加载并运行&#xff1f; JVM运行时数据区 J…

优选算法的灵动之章:双指针专题(一)

个人主页&#xff1a;手握风云 专栏&#xff1a;算法 目录 一、双指针算法思想 二、算法题精讲 2.1. 查找总价格为目标值的两个商品 2.2. 盛最多水的容器 ​编辑 2.3. 移动零 2.4. 有效的三角形个数 一、双指针算法思想 双指针算法主要用于处理数组、链表等线性数据结构…

[C++]多态详解

目录 一、多态的概念 二、静态的多态 三、动态的多态 3.1多态的定义 3.2虚函数 四、虚函数的重写&#xff08;覆盖&#xff09; 4.1虚函数 4.2三同 4.3两种特殊情况 &#xff08;1&#xff09;协变 &#xff08;2&#xff09;析构函数的重写 五、C11中的final和over…

BSD实现:单播

分用单播数据报 如果程序执行到这里&#xff0c;说明程序并没有执行多播操作&#xff0c;那么大概率是单播。 维护缓存指针 udp_last_inpcb是上一次接收数据报的端口的控制块指针&#xff0c;维护该指针的依据是许多程序往往具有时间局部性&#xff0c;也就是&#xff1a;经…

Spring AI发布!让Java紧跟AI赛道!

1. 序言 在当今技术发展的背景下&#xff0c;人工智能&#xff08;AI&#xff09;已经成为各行各业中不可忽视的重要技术。无论是在互联网公司&#xff0c;还是传统行业&#xff0c;AI技术的应用都在大幅提升效率、降低成本、推动创新。从智能客服到个性化推荐&#xff0c;从语…