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

611 0
Honkers 2025-3-5 17:34:12 | 显示全部楼层 |阅读模式

目录

前言

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文件中引入相对应的依赖即可。 

  1. <dependencies>
  2. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  3. <dependency>
  4. <groupid>org.springframework.boot</groupid>
  5. <artifactid>spring-boot-starter-web</artifactid>
  6. <version>3.4.2</version>
  7. </dependency>
  8. <dependency>
  9. <groupid>org.springframework.boot</groupid>
  10. <artifactid>spring-boot-starter-test</artifactid>
  11. <version>3.4.2</version>
  12. </dependency>
  13. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  14. <dependency>
  15. <groupid>org.projectlombok</groupid>
  16. <artifactid>lombok</artifactid>
  17. <version>1.18.30</version>
  18. <scope>provided</scope>
  19. </dependency>
  20. <!-- HTTP客户端 -->
  21. <dependency>
  22. <groupid>com.squareup.okhttp3</groupid>
  23. <artifactid>okhttp</artifactid>
  24. <version>4.9.0</version>
  25. </dependency>
  26. <!-- JSON处理 -->
  27. <dependency>
  28. <groupid>com.google.code.gson</groupid>
  29. <artifactid>gson</artifactid>
  30. <version>2.10.1</version>
  31. </dependency>
  32. <dependency>
  33. <groupid>com.mashape.unirest</groupid>
  34. <artifactid>unirest-java</artifactid>
  35. <version>1.4.9</version>
  36. </dependency>
  37. <dependency>
  38. <groupid>org.apache.httpcomponents</groupid>
  39. <artifactid>httpclient</artifactid>
  40. <version>4.3.6</version>
  41. </dependency>
  42. <dependency>
  43. <groupid>org.apache.httpcomponents</groupid>
  44. <artifactid>httpasyncclient</artifactid>
  45. <version>4.0.2</version>
  46. </dependency>
  47. <dependency>
  48. <groupid>org.apache.httpcomponents</groupid>
  49. <artifactid>httpmime</artifactid>
  50. <version>4.3.6</version>
  51. </dependency>
  52. <dependency>
  53. <groupid>org.json</groupid>
  54. <artifactid>json</artifactid>
  55. <version>20140107</version>
  56. </dependency>
  57. </dependencies>
复制代码

3、创建实体类 

  1. @Data
  2. @Builder
  3. public class DeeseekRequest {
  4. private String model;
  5. private List<message> messages;
  6. @Data
  7. @Builder
  8. public static class Message {
  9. private String role;
  10. private String content;
  11. }
  12. }
复制代码

4、创建Controller层

  1. package com.wdc.dk;
  2. import com.google.gson.Gson;
  3. import com.mashape.unirest.http.HttpResponse;
  4. import com.mashape.unirest.http.Unirest;
  5. import com.mashape.unirest.http.exceptions.UnirestException;
  6. import okhttp3.MediaType;
  7. import okhttp3.OkHttpClient;
  8. import okhttp3.Request;
  9. import okhttp3.RequestBody;
  10. import okhttp3.Response;
  11. import org.junit.jupiter.api.Test;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.List;
  18. @RestController
  19. public class AIController {
  20. private final Gson gson = new Gson();
  21. @PostMapping("tall")
  22. public String tallQuestion(@org.springframework.web.bind.annotation.RequestBody String question) throws IOException, UnirestException {
  23. Unirest.setTimeouts(0, 0);
  24. //DeeseekRequest: 自己的实体类名称
  25. List<deeseekrequest.message> messages = new ArrayList<>();
  26. //给deepSeek一个角色
  27. messages.add(DeeseekRequest.Message.builder().role("system").content("你是一个语言学家").build());
  28. // question:说你自己想说的话
  29. messages.add(DeeseekRequest.Message.builder().role("user").content(question).build());
  30. DeeseekRequest requestBody = DeeseekRequest.builder()
  31. .model("deepseek-chat")
  32. .messages(messages)
  33. .build();
  34. HttpResponse<string> response = Unirest.post("https://api.deepseek.com/chat/completions")
  35. .header("Content-Type", "application/json")
  36. .header("Accept", "application/json")
  37. .header("Authorization", "Bearer "+"自己的key")
  38. .body(gson.toJson(requestBody))
  39. .asString();
  40. return response.getBody();
  41. }
  42. }
复制代码

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

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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4008
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行