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

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

目录

前言

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、创建实体类 

@Data
@Builder
public class DeeseekRequest {
    private String model;
    private List<Message> messages;

    @Data
    @Builder
    public static class Message {
        private String role;
        private String content;
    }
}

4、创建Controller层

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回答出来,快去试试吧,像你的目标前进! 

相关文章:

  • 图论part3|101.孤岛的总面积、沉没孤岛、417. 太平洋大西洋水流问题
  • 考研408-数据结构完整代码 线性表的顺序存储结构 - 顺序表
  • Unity2D 井字棋
  • 双路快排--力扣215.数组中的第K个最大元素(java)
  • 车载以太网测试-9【网络层】-子网划分的子网掩码VLAN
  • 项目组织管理类型-职能式组织和矩阵式组织的区别
  • 开发策略选择:如何为项目找到最优路径?
  • Keytool常见问题全解析:从环境配置到公钥提取
  • 创新技术引领软件供应链安全,助力数字中国建设
  • 浅述WinForm 和 WPF 的前景
  • Unity 封装一个依赖于MonoBehaviour的计时器(下) 链式调用
  • 【文献阅读】Zotero 新手完全教程:安装、使用与插件
  • 使用1Panel一键搭建WordPress网站的详细教程(全)
  • 2025移动端软件供应链安全开源治理方案最佳实践
  • linux操作系统实战
  • Matlab实现RIME-CNN-LSTM-Multihead-Attention多变量多步时序预测
  • 作物移栽机器人的结构设计的介绍
  • 2025-03-13 学习记录--C/C++-PTA 练习2-12 输出华氏-摄氏温度转换表
  • 《算法笔记》8.2小节——搜索专题->广度优先搜索(BFS)问题 A: Jugs
  • 人工智能与人的智能,思维模型分享【3】直觉
  • 这款小孩子最爱的饮料,害处竟大到不敢想象
  • 外交部发言人就第78届世界卫生大会拒绝涉台提案发表谈话
  • 墨西哥海军一载两百余人帆船撞上纽约布鲁克林大桥,多人落水
  • 美国考虑让移民上真人秀竞逐公民权,制片人称非现实版《饥饿游戏》
  • 林诗栋/蒯曼混双取胜,国乒赢得多哈世乒赛开门红
  • 透视社会组织创新实践中的花开岭现象:与乡村发展的融合共进