使用Spring AI流式调用DeepSeek

1.新建MAVEN项目并引入依赖(MAVEN3.5+ JDK21+)

 1  <dependencies>
 2         <dependency>
 3             <groupId>org.springframework.boot</groupId>
 4             <artifactId>spring-boot-starter-web</artifactId>
 5             <version>3.4.11</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.mybatis.spring.boot</groupId>
 9             <artifactId>mybatis-spring-boot-starter</artifactId>
10             <version>3.0.5</version>
11         </dependency>
12         <dependency>
13             <groupId>com.mysql</groupId>
14             <artifactId>mysql-connector-j</artifactId>
15             <version>9.4.0</version>
16             <scope>runtime</scope>
17         </dependency>
18         <dependency>
19             <groupId>org.projectlombok</groupId>
20             <artifactId>lombok</artifactId>
21             <version>1.18.42</version>
22             <optional>true</optional>
23         </dependency>
24         <dependency>
25             <groupId>org.springframework.boot</groupId>
26             <artifactId>spring-boot-starter-test</artifactId>
27             <version>3.5.7</version>
28             <scope>test</scope>
29         </dependency>
30         <dependency>
31             <groupId>org.springframework.ai</groupId>
32             <artifactId>spring-ai-core</artifactId>
33             <version>1.0.0-M6</version>
34         </dependency>
35         <dependency>
36             <groupId>org.springframework.ai</groupId>
37             <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
38             <version>1.0.0-M6</version>
39         </dependency>
40     </dependencies>

2.application.yml配置文件,注意将api-key必须替换成你自己的真实key,数据库可暂时不替换成真实配置。

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver
  ai:
    openai:
      api-key: your_key
      base-url: https://api.deepseek.com
      chat:
        options:
          model: deepseek-reasoner
          temperature: 0.7
          stream: true

3.代码

 1 import lombok.extern.slf4j.Slf4j;
 2 import org.springframework.ai.chat.client.ChatClient;
 3 import org.springframework.ai.chat.messages.AssistantMessage;
 4 import org.springframework.ai.chat.messages.Message;
 5 import org.springframework.ai.chat.messages.UserMessage;
 6 import org.springframework.ai.chat.prompt.Prompt;
 7 import org.springframework.http.MediaType;
 8 import org.springframework.web.bind.annotation.GetMapping;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.bind.annotation.RestController;
12 import reactor.core.publisher.Flux;
13 
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 
18 @Slf4j
19 @RestController
20 @RequestMapping("/deepseek")
21 public class DeepSeekController {
22 
23     private final static List<Message> messageList = Collections.synchronizedList(new ArrayList<>());
24 
25     private final ChatClient chatClient;
26 
27     public DeepSeekController(ChatClient.Builder builder) {
28         this.chatClient = builder.build();
29     }
30 
31     @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
32     public Flux<String> chat(@RequestParam("prompt") String q) {
33         messageList.add(new UserMessage(q));
34         Prompt prompt = new Prompt(messageList);
35         StringBuilder result = new StringBuilder();
36         return chatClient.prompt(prompt).stream()
37                 .content()
38                 .doOnNext(content -> {
39                     log.info("content: {}", content);
40                     result.append(content);
41                 }).doOnComplete(() -> {
42                     log.info("result:" + System.lineSeparator() + " {}", result);
43                     messageList.add(new AssistantMessage(result.toString()));
44                 });
45     }
46 
47 }

 

posted @ 2025-10-30 14:27  墨竹丶蝉翼  阅读(17)  评论(0)    收藏  举报