Procházet zdrojové kódy

数字人对话加入语音播报状态控制

liyanbo před 1 měsícem
rodič
revize
f68ba007a7

+ 3 - 0
byzs-module-ai/src/main/java/cn/iocoder/byzs/module/ai/controller/admin/chat/vo/message/AiChatMessageSendReqVO.java

@@ -25,4 +25,7 @@ public class AiChatMessageSendReqVO {
     @Schema(description = "携带答案", example = "true")
     private String contentAnswer;
 
+    @Schema(description = "音频播放", example = "true")
+    private Boolean playAudio = Boolean.TRUE;
+
 }

+ 7 - 4
byzs-module-ai/src/main/java/cn/iocoder/byzs/module/ai/service/chat/AiChatMessageServiceImpl.java

@@ -190,19 +190,22 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
         // 1.2 校验模型
         AiModelDO model = modalService.validateModel(conversation.getModelId());
         StreamingChatModel chatModel = modalService.getChatModel(model.getId());
+        // 仅在请求开启音频播放时,才查询并初始化 TTS 相关配置和服务。
+        boolean playAudio = Boolean.TRUE.equals(sendReqVO.getPlayAudio());
+
         //角色
         AiChatRoleDO chatRole = null;
-        if (conversation.getRoleId() != null) {
+        if (playAudio && conversation.getRoleId() != null) {
             chatRole = chatRoleService.getChatRole(conversation.getRoleId());
         }
         //发声人
         AiTtsDO aiTtsDO = null;
-        if (chatRole != null) {
+        if (chatRole != null && chatRole.getTtsId() != null) {
             aiTtsDO = ttsMapper.selectById(chatRole.getTtsId());
         }
 
         // 添加useTts标志,判断是否使用TTS服务
-        boolean useTts = aiTtsDO != null;
+        boolean useTts = playAudio && aiTtsDO != null;
         // 创建final副本供lambda表达式使用
         final boolean finalUseTts = useTts;
         final AiTtsDO finalAiTtsDO = aiTtsDO;
@@ -802,4 +805,4 @@ public class AiChatMessageServiceImpl implements AiChatMessageService {
         return chatMessageMapper.selectPage(pageReqVO);
     }
 
-}
+}

+ 10 - 5
byzs-web/src/main/java/cn/iocoder/byzs/module/web/service/ai/WebAiServiceImpl.java

@@ -87,13 +87,18 @@ public class WebAiServiceImpl {
             throw exception(CHAT_CONVERSATION_NOT_EXISTS);
         }
 
-        // 2. 获取TTS配置
-        AiTtsDO aiTtsDO = getTtsConfig(conversation.getRoleId());
-
-        // 3. 获取回答内容
+        // 2. 获取回答内容
         String contentAnswer = sendReqVO.getContentAnswer();
         log.info("开始处理文本内容: {}", contentAnswer);
 
+        // 未开启音频播放时,仅返回文本,不加载或调用任何 TTS 资源。
+        if (!Boolean.TRUE.equals(sendReqVO.getPlayAudio())) {
+            return Flux.just(createFallbackTextResponse(sendReqVO, userId, conversation, contentAnswer));
+        }
+
+        // 3. 获取TTS配置
+        AiTtsDO aiTtsDO = getTtsConfig(conversation.getRoleId());
+
         // 4. 检查是否为豆包TTS
         boolean isDouBaoTts = aiTtsDO != null && "DouBao".equals(aiTtsDO.getPlatform());
 
@@ -974,4 +979,4 @@ public class WebAiServiceImpl {
         byte[] wavData = WavHeader.addWavHeader(pcmData, SampleRateEnum.SAMPLE_RATE_16K.value, 16, 1);
         return java.util.Base64.getEncoder().encodeToString(wavData);
     }
-}
+}