feat(actions): 增强音乐搜索,从LLM回复中提取确切歌名
为了提高音乐播放的准确性,引入了一种新机制,可根据 LLM 生成的回复动态更新音乐搜索动作。 在某些场景下,Planner 生成的 `music_search` 动作可能只包含一个模糊的意图,而 LLM 在其回复文本中会明确指出要播放的具体歌曲(例如,“好的,为您播放《晴天》”)。 此变更通过以下方式解决了这个问题: - 新增 `_extract_song_name_from_reply` 辅助函数,用于从回复文本中解析书名号《》内的歌名。 - 在执行并行附加动作前,系统会尝试从主回复中提取歌名。 - 如果成功提取,该歌名将被注入 `music_search` 动作的参数中,确保执行的搜索与 LLM 的意图完全一致。
This commit is contained in:
@@ -3,7 +3,7 @@ import time
|
|||||||
import traceback
|
import traceback
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
from typing import Dict, Any, Tuple
|
from typing import Dict, Any, Tuple, Optional
|
||||||
|
|
||||||
from src.chat.utils.timer_calculator import Timer
|
from src.chat.utils.timer_calculator import Timer
|
||||||
from src.common.logger import get_logger
|
from src.common.logger import get_logger
|
||||||
@@ -351,7 +351,16 @@ class CycleProcessor:
|
|||||||
# 2. 然后并行执行所有其他动作
|
# 2. 然后并行执行所有其他动作
|
||||||
if other_actions:
|
if other_actions:
|
||||||
logger.info(f"{self.log_prefix} 正在执行附加动作: {[a.get('action_type') for a in other_actions]}")
|
logger.info(f"{self.log_prefix} 正在执行附加动作: {[a.get('action_type') for a in other_actions]}")
|
||||||
other_action_tasks = [asyncio.create_task(execute_action(action)) for action in other_actions]
|
|
||||||
|
# 从回复文本中提取歌名
|
||||||
|
song_name_from_reply = self._extract_song_name_from_reply(reply_text_from_reply)
|
||||||
|
|
||||||
|
other_action_tasks = []
|
||||||
|
for action in other_actions:
|
||||||
|
if action.get("action_type") == "music_search" and song_name_from_reply:
|
||||||
|
action["action_data"]["song_name"] = song_name_from_reply
|
||||||
|
other_action_tasks.append(asyncio.create_task(execute_action(action)))
|
||||||
|
|
||||||
results = await asyncio.gather(*other_action_tasks, return_exceptions=True)
|
results = await asyncio.gather(*other_action_tasks, return_exceptions=True)
|
||||||
for i, result in enumerate(results):
|
for i, result in enumerate(results):
|
||||||
if isinstance(result, BaseException):
|
if isinstance(result, BaseException):
|
||||||
@@ -465,7 +474,7 @@ class CycleProcessor:
|
|||||||
if not action_handler:
|
if not action_handler:
|
||||||
logger.error(f"{self.context.log_prefix} 回退方案也失败,无法创建任何动作处理器")
|
logger.error(f"{self.context.log_prefix} 回退方案也失败,无法创建任何动作处理器")
|
||||||
return False, "", ""
|
return False, "", ""
|
||||||
|
|
||||||
# 执行动作
|
# 执行动作
|
||||||
success, reply_text = await action_handler.handle_action()
|
success, reply_text = await action_handler.handle_action()
|
||||||
return success, reply_text, ""
|
return success, reply_text, ""
|
||||||
@@ -473,3 +482,12 @@ class CycleProcessor:
|
|||||||
logger.error(f"{self.context.log_prefix} 处理{action}时出错: {e}")
|
logger.error(f"{self.context.log_prefix} 处理{action}时出错: {e}")
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return False, "", ""
|
return False, "", ""
|
||||||
|
|
||||||
|
def _extract_song_name_from_reply(self, reply_text: str) -> Optional[str]:
|
||||||
|
"""从回复文本中提取歌名"""
|
||||||
|
import re
|
||||||
|
# 匹配书名号中的内容
|
||||||
|
match = re.search(r"《(.*?)》", reply_text)
|
||||||
|
if match:
|
||||||
|
return match.group(1)
|
||||||
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user