feat(ai): 切换默认模型并新增流式对话接口- 将默认模型从 deepseek-chat 切换为 deepseek-reasoner
- 新增 /ai/generateStream 接口支持流式对话输出 - 新增 /v1/ai/generateStream 接口支持 reasoning 内容流式输出 - 引入 commons-lang3 依赖用于字符串处理- 添加 Flux 支持实现异步流式响应 - 完善 DeepSeekAssistantMessage 处理逻辑,区分推理内容与最终回答
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -15,6 +15,7 @@
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<spring-ai.version>1.0.3</spring-ai.version>
|
||||
<commons-lang3.version>3.19.0</commons-lang3.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@@ -39,6 +40,12 @@
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.hanserwei.airobot.controller;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.deepseek.DeepSeekChatModel;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/ai")
|
||||
@@ -25,4 +28,19 @@ public class DeepSeekChatController {
|
||||
return chatModel.call(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式对话
|
||||
* @param message 对话输入内容
|
||||
* @return 对话结果
|
||||
*/
|
||||
@GetMapping(value = "/generateStream", produces = "text/html;charset=utf-8")
|
||||
public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||
// 构建提示词
|
||||
Prompt prompt = new Prompt(new UserMessage(message));
|
||||
|
||||
// 流式输出
|
||||
return chatModel.stream(prompt)
|
||||
.mapNotNull(chatResponse -> chatResponse.getResult().getOutput().getText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.hanserwei.airobot.controller;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.ai.chat.messages.UserMessage;
|
||||
import org.springframework.ai.chat.prompt.Prompt;
|
||||
import org.springframework.ai.deepseek.DeepSeekAssistantMessage;
|
||||
import org.springframework.ai.deepseek.DeepSeekChatModel;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v1/ai")
|
||||
public class DeepSeekR1ChatController {
|
||||
|
||||
@Resource
|
||||
private DeepSeekChatModel chatModel;
|
||||
|
||||
/**
|
||||
* 流式对话
|
||||
* @param message 对话输入内容
|
||||
* @return 对话结果
|
||||
*/
|
||||
@GetMapping(value = "/generateStream", produces = "text/html;charset=utf-8")
|
||||
public Flux<String> generateStream(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||
// 构建提示词
|
||||
Prompt prompt = new Prompt(new UserMessage(message));
|
||||
|
||||
// 流式输出
|
||||
return chatModel.stream(prompt)
|
||||
.mapNotNull(chatResponse -> {
|
||||
// 获取响应内容
|
||||
DeepSeekAssistantMessage deepSeekAssistantMessage = (DeepSeekAssistantMessage) chatResponse.getResult().getOutput();
|
||||
// 推理内容
|
||||
String reasoningContent = deepSeekAssistantMessage.getReasoningContent();
|
||||
// 推理结束后的正式回答
|
||||
String text = deepSeekAssistantMessage.getText();
|
||||
|
||||
// 若推理内容有值,则响应推理内容,否则,说明推理结束了,响应正式回答
|
||||
return StringUtils.isNotBlank(reasoningContent) ? reasoningContent : text;
|
||||
});
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ spring:
|
||||
base-url: https://api.deepseek.com # DeepSeek 的请求 URL, 可不填,默认值为 api.deepseek.com
|
||||
chat:
|
||||
options:
|
||||
model: deepseek-chat # 使用哪个模型
|
||||
model: deepseek-reasoner # 使用哪个模型
|
||||
temperature: 0.8 # 温度值
|
||||
jasypt:
|
||||
encryptor:
|
||||
|
||||
Reference in New Issue
Block a user