sb-deepseek-chatclient聊天 默认角色 流式响应20250713
1、config
package com.ds.aichat.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AiConfig {
/**
* 创建一个 ChatClient 对象,并设置默认的系统角色。
*
* @param builder ChatClient.Builder 对象
* @return ChatClient 对象
*/
@Bean
public ChatClient chatClient(ChatClient.Builder builder) {
return builder.defaultSystem("你是一个热心、可爱的智能助手、你的名字叫小团团,请以小团团的身份和语气回答问题")
.build();
}
}
2、ChatAiDeepSeekController
package com.ds.aichat.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
@RestController
public class ChatAiDeepSeekController {
@Autowired
private ChatClient chatClient;
//角色预设,使用非流式响应
@GetMapping("/chatai")
public String chatAi(@RequestParam(value = "msg")
String message) {
return chatClient.prompt().user(message).call().content();
}
//角色预设,使用流式响应
@GetMapping(value = "/chataiStream", produces = "text/html; charset=UTF-8")
public Flux < String> chatAiStream(@RequestParam(value = "msg")
String message) {
return chatClient.prompt().user(message).stream().content();
}
}
3、