Merge branch 'feat/demo' of github.com:Plaud-AI/live-agent into feat/demo

This commit is contained in:
Harold 2025-11-04 17:36:17 +08:00
commit 6f4b491300
4 changed files with 123 additions and 16 deletions

View file

@ -419,6 +419,15 @@ class ConnectionHandler:
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self.asr.open_audio_channels(self), self.loop self.asr.open_audio_channels(self), self.loop
) )
"""加载记忆"""
self._initialize_memory()
"""加载意图识别"""
self._initialize_intent()
"""更新系统提示词(必须在 TTS 初始化前,以便加载 role 的 TTS 配置)"""
self._init_prompt_enhancement()
# 初始化 TTS(在 prompt 初始化之后,以便使用 role 的 TTS 配置)
if self.tts is None: if self.tts is None:
self.tts = self._initialize_tts() self.tts = self._initialize_tts()
# 打开语音合成通道 # 打开语音合成通道
@ -426,14 +435,8 @@ class ConnectionHandler:
self.tts.open_audio_channels(self), self.loop self.tts.open_audio_channels(self), self.loop
) )
"""加载记忆"""
self._initialize_memory()
"""加载意图识别"""
self._initialize_intent()
"""初始化上报线程""" """初始化上报线程"""
self._init_report_threads() self._init_report_threads()
"""更新系统提示词"""
self._init_prompt_enhancement()
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).error(f"实例化组件失败: {e}") self.logger.bind(tag=TAG).error(f"实例化组件失败: {e}")
@ -453,14 +456,27 @@ class ConnectionHandler:
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).warning(f"获取用户画像失败: {e}") self.logger.bind(tag=TAG).warning(f"获取用户画像失败: {e}")
# 构建增强的系统提示词 # 构建增强的系统提示词(返回 (enhanced_prompt, role_tts_config))
enhanced_prompt = self.prompt_manager.build_enhanced_prompt( result = self.prompt_manager.build_enhanced_prompt(
self.config["prompt"], self.config["prompt"],
self.device_id, self.device_id,
self.client_ip, self.client_ip,
user_persona=user_persona, user_persona=user_persona,
) )
# 解包返回值
if isinstance(result, tuple):
enhanced_prompt, role_tts_config = result
# 保存 role 的 TTS 配置到 self.config(供 TTS 初始化使用)
if role_tts_config:
self.config["_role_tts_config"] = role_tts_config
self.logger.bind(tag=TAG).info(
f"保存 Role TTS 配置到 config: {role_tts_config}"
)
else:
# 兼容旧版本返回值(仅返回 prompt 字符串)
enhanced_prompt = result
if enhanced_prompt: if enhanced_prompt:
self.change_system_prompt(enhanced_prompt) self.change_system_prompt(enhanced_prompt)
self.logger.bind(tag=TAG).info("系统提示词已增强更新") self.logger.bind(tag=TAG).info("系统提示词已增强更新")
@ -479,9 +495,26 @@ class ConnectionHandler:
self.logger.bind(tag=TAG).info("TTS上报线程已启动") self.logger.bind(tag=TAG).info("TTS上报线程已启动")
def _initialize_tts(self): def _initialize_tts(self):
"""初始化TTS""" """
初始化TTS(支持三级优先级配置)
优先级:
1. API 下发的 TTS 配置(在 _initialize_private_config 中已应用)
2. Role 中的 TTS 配置
3. selected_module.TTS(兜底配置)
"""
tts = None tts = None
if not self.need_bind: if not self.need_bind:
# 检查是否有 role 的 TTS 配置(优先级2)
role_tts_config = self.config.get("_role_tts_config")
self.logger.bind(tag=TAG).info(f"🔍 检查 _role_tts_config: {role_tts_config}")
if role_tts_config:
self.logger.bind(tag=TAG).info("✅ 发现 Role TTS 配置,准备应用")
self._apply_role_tts_config(role_tts_config)
else:
self.logger.bind(tag=TAG).info("ℹ️ 没有 Role TTS 配置,使用默认配置")
# 初始化 TTS(优先级1和3在这里统一处理)
tts = initialize_tts(self.config) tts = initialize_tts(self.config)
if tts is None: if tts is None:
@ -489,6 +522,64 @@ class ConnectionHandler:
return tts return tts
def _apply_role_tts_config(self, role_tts_config: dict):
"""应用 role 中的 TTS 配置"""
self.logger.bind(tag=TAG).debug(f"开始应用 Role TTS 配置: {role_tts_config}")
provider = role_tts_config.get("provider")
voice_id = role_tts_config.get("voice_id")
if not provider or not voice_id:
self.logger.bind(tag=TAG).warning("Role TTS 配置不完整,跳过应用")
return
# 检查当前 selected_module.TTS 是否已经被 API 覆盖
# 如果 API 已经设置了 TTS,则不应用 role 配置(API 优先级更高)
current_tts = self.config["selected_module"]["TTS"]
if self.config.get("_api_tts_applied"):
self.logger.bind(tag=TAG).info(
f"API 已设置 TTS 配置(优先级1),跳过 Role TTS 配置: {provider}"
)
return
# 应用 role 的 TTS 配置
# 根据 provider 映射到实际的 TTS 模块名
tts_module_map = {
"elevenlabs": "ElevenLabsSDK",
"cartesia": "CartesiaSDK",
"edge": "EdgeTTS",
"doubao": "VolcanoStreamTTS",
# 可以继续添加更多映射...
}
tts_module = tts_module_map.get(provider.lower())
if not tts_module:
self.logger.bind(tag=TAG).warning(
f"未知的 TTS provider: {provider},使用默认配置"
)
return
# 检查该 TTS 模块是否在配置中存在
if tts_module not in self.config.get("TTS", {}):
self.logger.bind(tag=TAG).warning(
f"TTS 模块 {tts_module} 未在配置中定义,跳过应用"
)
return
# 更新 selected_module.TTS
self.config["selected_module"]["TTS"] = tts_module
# 更新 voice_id(如果该 TTS 模块支持)
if "voice_id" in self.config["TTS"][tts_module]:
self.config["TTS"][tts_module]["voice_id"] = voice_id
self.logger.bind(tag=TAG).info(
f"✅ 应用 Role TTS 配置: provider={provider}, "
f"module={tts_module}, voice_id={voice_id[:16]}..."
)
else:
self.logger.bind(tag=TAG).warning(
f"TTS 模块 {tts_module} 不支持 voice_id 配置"
)
def _initialize_asr(self): def _initialize_asr(self):
"""初始化ASR""" """初始化ASR"""
if self._asr.interface_type == InterfaceType.LOCAL: if self._asr.interface_type == InterfaceType.LOCAL:
@ -572,6 +663,8 @@ class ConnectionHandler:
self.config["selected_module"]["TTS"] = private_config["selected_module"][ self.config["selected_module"]["TTS"] = private_config["selected_module"][
"TTS" "TTS"
] ]
# 标记 API 已设置 TTS 配置(优先级1)
self.config["_api_tts_applied"] = True
if private_config.get("LLM", None) is not None: if private_config.get("LLM", None) is not None:
init_llm = True init_llm = True
self.config["LLM"] = private_config["LLM"] self.config["LLM"] = private_config["LLM"]

View file

@ -190,7 +190,8 @@ class TTSProvider(TTSProviderBase):
) )
except Exception as e: except Exception as e:
logger.bind(tag=TAG).error(f"TTS 合成失败: {e}", exc_info=True) error_msg = str(e).replace("{", "{{").replace("}", "}}")
logger.bind(tag=TAG).error(f"TTS 合成失败: {error_msg}", exc_info=True)
raise raise
async def close(self): async def close(self):

View file

@ -1,4 +1,4 @@
id: hospital_assistant id: default
version: 4 version: 4
wake_word: Alice wake_word: Alice
language: en language: en
@ -61,6 +61,6 @@ profile: |
# voice_id: 7a5d4663-88ae-47b7-808e-8f9b9ee4127b # voice_id: 7a5d4663-88ae-47b7-808e-8f9b9ee4127b
# provider: cartesia # provider: cartesia
tts: tts:
voice_id: f786b574-daa5-4673-aa0c-cbe3e8534c02 voice_id: ByhETIclHirOlWnWKhHc
provider: elevenlabs provider: elevenlabs
timezone: Asia/Shanghai timezone: Asia/Shanghai

View file

@ -165,6 +165,7 @@ class PromptManager:
from core.roles import get_role_config_loader from core.roles import get_role_config_loader
# 优先级:API 传入的 user_prompt > 本地 role 配置 > 空字符串 # 优先级:API 传入的 user_prompt > 本地 role 配置 > 空字符串
role_tts_config = None # 保存 role 的 TTS 配置供后续使用
if user_prompt and user_prompt.strip(): if user_prompt and user_prompt.strip():
# 优先使用 API 传入的 prompt(正式部署场景) # 优先使用 API 传入的 prompt(正式部署场景)
profile = user_prompt profile = user_prompt
@ -181,6 +182,16 @@ class PromptManager:
profile = role_config.profile profile = role_config.profile
timezone = role_config.timezone timezone = role_config.timezone
language = role_config.language language = role_config.language
# 提取 TTS 配置(如果有)
if role_config.tts:
role_tts_config = {
"voice_id": role_config.tts.voice_id,
"provider": role_config.tts.provider
}
self.logger.bind(tag=TAG).info(
f"检测到 Role TTS 配置: provider={role_config.tts.provider}, "
f"voice_id={role_config.tts.voice_id[:16]}..."
)
self.logger.bind(tag=TAG).info(f"使用本地 role 配置: {role_id}") self.logger.bind(tag=TAG).info(f"使用本地 role 配置: {role_id}")
except Exception as e: except Exception as e:
# 兜底方案:使用空 profile # 兜底方案:使用空 profile
@ -235,8 +246,10 @@ class PromptManager:
self.logger.bind(tag=TAG).info( self.logger.bind(tag=TAG).info(
f"构建增强提示词成功,长度: {len(enhanced_prompt)}" f"构建增强提示词成功,长度: {len(enhanced_prompt)}"
) )
return enhanced_prompt
# 返回 (enhanced_prompt, role_tts_config)
return (enhanced_prompt, role_tts_config)
except Exception as e: except Exception as e:
self.logger.bind(tag=TAG).error(f"构建增强提示词失败: {e}", exc_info=True) self.logger.bind(tag=TAG).error(f"构建增强提示词失败: {e}", exc_info=True)
return user_prompt return (user_prompt, None)