找回密码
 立即注册
首页 业界区 安全 Spring Boot 整合AI大模型实战:手把手带你接入DeepSeek ...

Spring Boot 整合AI大模型实战:手把手带你接入DeepSeek API

秦晓曼 7 天前
前言

随着AI大模型的快速普及,越来越多的Java开发者希望将AI能力集成到自己的项目中。本文手把手带你用Spring Boot接入DeepSeek API,实现一个具备AI对话能力的后端服务。
一、环境准备


  • JDK 17+
  • Spring Boot 3.x
  • Maven 3.8+
  • DeepSeek API Key(到 platform.deepseek.com 免费申请)
二、添加依赖
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>org.springframework.boot</groupId>
  7.     spring-boot-starter-webflux</artifactId>
  8. </dependency>
  9. <dependency>
  10.     <groupId>com.fasterxml.jackson.core</groupId>
  11.     jackson-databind</artifactId>
  12. </dependency>
复制代码
三、配置 application.yml
  1. deepseek:
  2.   api-key: sk-xxxxxxxxxxxxxxxx
  3.   base-url: https://api.deepseek.com
  4.   model: deepseek-chat
  5.   max-tokens: 2048
复制代码
四、创建配置类
  1. @Configuration
  2. @ConfigurationProperties(prefix = "deepseek")
  3. @Data
  4. public class DeepSeekConfig {
  5.     private String apiKey;
  6.     private String baseUrl;
  7.     private String model;
  8.     private Integer maxTokens;
  9.     @Bean
  10.     public WebClient deepSeekWebClient() {
  11.         return WebClient.builder()
  12.             .baseUrl(baseUrl)
  13.             .defaultHeader("Authorization", "Bearer " + apiKey)
  14.             .defaultHeader("Content-Type", "application/json")
  15.             .build();
  16.     }
  17. }
复制代码
五、定义请求/响应实体
  1. // 请求体
  2. @Data
  3. public class ChatRequest {
  4.     private String model;
  5.     private List<Message> messages;
  6.     @JsonProperty("max_tokens")
  7.     private Integer maxTokens;
  8.     private Boolean stream = false;
  9.     @Data
  10.     @AllArgsConstructor
  11.     public static class Message {
  12.         private String role;  // system / user / assistant
  13.         private String content;
  14.     }
  15. }
  16. // 响应体
  17. @Data
  18. public class ChatResponse {
  19.     private String id;
  20.     private List<Choice> choices;
  21.     @Data
  22.     public static class Choice {
  23.         private Message message;
  24.     }
  25.     @Data
  26.     public static class Message {
  27.         private String role;
  28.         private String content;
  29.     }
  30. }
复制代码
六、实现Service层
  1. @Service
  2. @RequiredArgsConstructor
  3. public class DeepSeekService {
  4.     private final WebClient deepSeekWebClient;
  5.     private final DeepSeekConfig config;
  6.     public String chat(String userMessage) {
  7.         ChatRequest request = new ChatRequest();
  8.         request.setModel(config.getModel());
  9.         request.setMaxTokens(config.getMaxTokens());
  10.         request.setMessages(List.of(
  11.             new ChatRequest.Message("system", "你是一个专业Java开发助手"),
  12.             new ChatRequest.Message("user", userMessage)
  13.         ));
  14.         ChatResponse response = deepSeekWebClient
  15.             .post()
  16.             .uri("/v1/chat/completions")
  17.             .bodyValue(request)
  18.             .retrieve()
  19.             .bodyToMono(ChatResponse.class)
  20.             .block();
  21.         return response.getChoices().get(0).getMessage().getContent();
  22.     }
  23. }
复制代码
七、实现Controller层
  1. @RestController
  2. @RequestMapping("/api/ai")
  3. @RequiredArgsConstructor
  4. public class AiController {
  5.     private final DeepSeekService deepSeekService;
  6.     @PostMapping("/chat")
  7.     public ResponseEntity<String> chat(@RequestBody Map<String, String> body) {
  8.         String message = body.get("message");
  9.         String reply = deepSeekService.chat(message);
  10.         return ResponseEntity.ok(reply);
  11.     }
  12. }
复制代码
八、测试
  1. curl -X POST http://localhost:8080/api/ai/chat \
  2.   -H "Content-Type: application/json" \
  3.   -d '{"message": "帮我写一个Spring Boot分页查询的示例"}'
复制代码
总结

通过以上步骤,我们完成了Spring Boot整合DeepSeek API的全流程。后续可以进一步扩展:

  • 支持流式输出(SSE)
  • 加入对话历史管理
  • 集成Redis缓存常用回答
  • 接入其他大模型(通义千问、文心一言等)
本文由AI辅助创作。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

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